Skip to content

Commit 1b2988f

Browse files
committed
Fixed minor style issues in generated queries.
Introduced options for builders. Data types are lower case in CREATE TABLE statements, but in any other case they continue to be upper case. Formatter uses 2 spaces instead of 4 for indentation as specified in the MySQL Coding Guidelines. https://dev.mysql.com/doc/internals/en/indentation-spacing.html.
1 parent 14c54da commit 1b2988f

24 files changed

+62
-34
lines changed

src/Component.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ public static function parse(
7070
* `static::parse`.
7171
*
7272
* @param mixed $component The component to be built.
73+
* @param array $options Parameters for building.
7374
*
7475
* @throws \Exception Not implemented yet.
7576
*
7677
* @return string
7778
*/
78-
public static function build($component)
79+
public static function build($component, array $options = array())
7980
{
8081
// This method should be abstract, but it can't be both static and
8182
// abstract.

src/Components/AlterOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
210210

211211
/**
212212
* @param AlterOperation $component The component to be built.
213+
* @param array $options Parameters for building.
213214
*
214215
* @return string
215216
*/
216-
public static function build($component)
217+
public static function build($component, array $options = array())
217218
{
218219
$ret = $component->options . ' ';
219220
if ((isset($component->field)) && ($component->field !== '')) {

src/Components/Array2d.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
124124

125125
/**
126126
* @param ArrayObj[] $component The component to be built.
127+
* @param array $options Parameters for building.
127128
*
128129
* @return string
129130
*/
130-
public static function build($component)
131+
public static function build($component, array $options = array())
131132
{
132133
return ArrayObj::build($component);
133134
}

src/Components/ArrayObj.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
143143

144144
/**
145145
* @param ArrayObj|ArrayObj[] $component The component to be built.
146+
* @param array $options Parameters for building.
146147
*
147148
* @return string
148149
*/
149-
public static function build($component)
150+
public static function build($component, array $options = array())
150151
{
151152
if (is_array($component)) {
152153
return implode(', ', $component);

src/Components/Condition.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
197197

198198
/**
199199
* @param Condition[] $component The component to be built.
200+
* @param array $options Parameters for building.
200201
*
201202
* @return string
202203
*/
203-
public static function build($component)
204+
public static function build($component, array $options = array())
204205
{
205206
if (is_array($component)) {
206207
return implode(' ', $component);

src/Components/CreateDefinition.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
272272

273273
/**
274274
* @param CreateDefinition|CreateDefinition[] $component The component to be built.
275+
* @param array $options Parameters for building.
275276
*
276277
* @return string
277278
*/
278-
public static function build($component)
279+
public static function build($component, array $options = array())
279280
{
280281
if (is_array($component)) {
281-
return "(\n" . implode(",\n", $component) . "\n)";
282+
return "(\n " . implode(",\n ", $component) . "\n)";
282283
} else {
283284
$tmp = '';
284285

@@ -291,7 +292,10 @@ public static function build($component)
291292
}
292293

293294
if (!empty($component->type)) {
294-
$tmp .= $component->type . ' ';
295+
$tmp .= DataType::build(
296+
$component->type,
297+
array('lowercase' => true)
298+
) . ' ';
295299
}
296300

297301
if (!empty($component->key)) {

src/Components/DataType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =
151151

152152
/**
153153
* @param DataType $component The component to be built.
154+
* @param array $options Parameters for building.
154155
*
155156
* @return string
156157
*/
157-
public static function build($component)
158+
public static function build($component, array $options = array())
158159
{
159-
$tmp = '';
160+
$name = (empty($options['lowercase'])) ?
161+
$component->name : strtolower($component->name);
162+
163+
$parameters = '';
160164
if (!empty($component->parameters)) {
161-
$tmp = '(' . implode(', ', $component->parameters) . ')';
165+
$parameters = '(' . implode(',', $component->parameters) . ')';
162166
}
163167

164-
return trim($component->name . $tmp . ' ' . $component->options);
168+
return trim($name . $parameters . ' ' . $component->options);
165169
}
166170
}

src/Components/Expression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
351351

352352
/**
353353
* @param Expression|Expression[] $component The component to be built.
354+
* @param array $options Parameters for building.
354355
*
355356
* @return string
356357
*/
357-
public static function build($component)
358+
public static function build($component, array $options = array())
358359
{
359360
if (is_array($component)) {
360361
return implode($component, ', ');

src/Components/ExpressionArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
106106

107107
/**
108108
* @param Expression[] $component The component to be built.
109+
* @param array $options Parameters for building.
109110
*
110111
* @return string
111112
*/
112-
public static function build($component)
113+
public static function build($component, array $options = array())
113114
{
114115
$ret = array();
115116
foreach ($component as $frag) {

src/Components/FunctionCall.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
115115

116116
/**
117117
* @param FunctionCall $component The component to be built.
118+
* @param array $options Parameters for building.
118119
*
119120
* @return string
120121
*/
121-
public static function build($component)
122+
public static function build($component, array $options = array())
122123
{
123124
return $component->name . $component->parameters;
124125
}

src/Components/IntoKeyword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
133133

134134
/**
135135
* @param IntoKeyword $component The component to be built.
136+
* @param array $options Parameters for building.
136137
*
137138
* @return string
138139
*/
139-
public static function build($component)
140+
public static function build($component, array $options = array())
140141
{
141142
if ($component->dest instanceof Expression) {
142143
$columns = !empty($component->columns) ?

src/Components/JoinKeyword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
151151

152152
/**
153153
* @param JoinKeyword[] $component The component to be built.
154+
* @param array $options Parameters for building.
154155
*
155156
* @return string
156157
*/
157-
public static function build($component)
158+
public static function build($component, array $options = array())
158159
{
159160
$ret = array();
160161
foreach ($component as $c) {

src/Components/Key.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
152152
}
153153

154154
/**
155-
* @param Key $component The component to be built.
155+
* @param Key $component The component to be built.
156+
* @param array $options Parameters for building.
156157
*
157158
* @return string
158159
*/
159-
public static function build($component)
160+
public static function build($component, array $options = array())
160161
{
161162
$ret = $component->type . ' ';
162163
if (!empty($component->name)) {
163164
$ret .= Context::escape($component->name) . ' ';
164165
}
165-
$ret .= '(' . implode(', ', Context::escape($component->columns)) . ') '
166+
$ret .= '(' . implode(',', Context::escape($component->columns)) . ') '
166167
. $component->options;
167168
return trim($ret);
168169
}

src/Components/Limit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
122122

123123
/**
124124
* @param Limit $component The component to be built.
125+
* @param array $options Parameters for building.
125126
*
126127
* @return string
127128
*/
128-
public static function build($component)
129+
public static function build($component, array $options = array())
129130
{
130131
if (empty($component->offset)) {
131132
return $component->rowCount;

src/Components/OptionsArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
251251

252252
/**
253253
* @param OptionsArray $component The component to be built.
254+
* @param array $options Parameters for building.
254255
*
255256
* @return string
256257
*/
257-
public static function build($component)
258+
public static function build($component, array $options = array())
258259
{
259260
if (empty($component->options)) {
260261
return '';

src/Components/OrderKeyword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
127127

128128
/**
129129
* @param OrderKeyword|OrderKeyword[] $component The component to be built.
130+
* @param array $options Parameters for building.
130131
*
131132
* @return string
132133
*/
133-
public static function build($component)
134+
public static function build($component, array $options = array())
134135
{
135136
if (is_array($component)) {
136137
return implode(', ', $component);

src/Components/ParameterDefinition.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
140140

141141
/**
142142
* @param ParameterDefinition[] $component The component to be built.
143+
* @param array $options Parameters for building.
143144
*
144145
* @return string
145146
*/
146-
public static function build($component)
147+
public static function build($component, array $options = array())
147148
{
148149
if (is_array($component)) {
149150
return '(' . implode(', ', $component) . ')';

src/Components/PartitionDefinition.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
192192

193193
/**
194194
* @param PartitionDefinition|PartitionDefinition[] $component The component to be built.
195+
* @param array $options Parameters for building.
195196
*
196197
* @return string
197198
*/
198-
public static function build($component)
199+
public static function build($component, array $options = array())
199200
{
200201
if (is_array($component)) {
201202
return "(\n" . implode(",\n", $component) . "\n)";

src/Components/Reference.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
137137

138138
/**
139139
* @param Reference $component The component to be built.
140+
* @param array $options Parameters for building.
140141
*
141142
* @return string
142143
*/
143-
public static function build($component)
144+
public static function build($component, array $options = array())
144145
{
145146
return trim(
146147
Context::escape($component->table)

src/Components/RenameOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
162162

163163
/**
164164
* @param RenameOperation $component The component to be built.
165+
* @param array $options Parameters for building.
165166
*
166167
* @return string
167168
*/
168-
public static function build($component)
169+
public static function build($component, array $options = array())
169170
{
170171
if (is_array($component)) {
171172
return implode(', ', $component);

src/Components/SetOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
122122

123123
/**
124124
* @param SetOperation|SetOperation[] $component The component to be built.
125+
* @param array $options Parameters for building.
125126
*
126127
* @return string
127128
*/
128-
public static function build($component)
129+
public static function build($component, array $options = array())
129130
{
130131
if (is_array($component)) {
131132
return implode(', ', $component);

src/Components/UnionKeyword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class UnionKeyword extends Component
2525

2626
/**
2727
* @param SelectStatement[] $component The component to be built.
28+
* @param array $options Parameters for building.
2829
*
2930
* @return string
3031
*/
31-
public static function build($component)
32+
public static function build($component, array $options = array())
3233
{
3334
return implode(' UNION ', $component);
3435
}

src/Utils/Formatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(array $options = array())
8080
*
8181
* @var string
8282
*/
83-
'indentation' => " ",
83+
'indentation' => " ",
8484

8585
/**
8686
* Whether comments should be removed or not.

tests/Builder/CreateStatementTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function testBuilderTable()
5858

5959
$this->assertEquals(
6060
"CREATE TABLE `test` (\n" .
61-
"`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n" .
62-
"PRIMARY KEY (`id`)\n" .
61+
" `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n" .
62+
" PRIMARY KEY (`id`)\n" .
6363
") ",
6464
$stmt->build()
6565
);
@@ -68,8 +68,8 @@ public function testBuilderTable()
6868
public function testBuilderPartitions()
6969
{
7070
$query = 'CREATE TABLE ts (' . "\n"
71-
. '`id` INT,' . "\n"
72-
. '`purchased` DATE' . "\n"
71+
. ' `id` int,' . "\n"
72+
. ' `purchased` date' . "\n"
7373
. ') ' . "\n"
7474
. 'PARTITION BY RANGE(YEAR(purchased))' . "\n"
7575
. 'PARTITIONS 3' . "\n"

0 commit comments

Comments
 (0)