Skip to content

Commit 8383942

Browse files
committed
Expression: Refactored parsing options.
1 parent de8b3ef commit 8383942

15 files changed

+93
-62
lines changed

src/Components/AlterOperation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
172172
$parser,
173173
$list,
174174
array(
175-
'noAlias' => true,
176-
'noBrackets' => true,
175+
'breakOnAlias' => true,
176+
'parseField' => 'column',
177177
)
178178
);
179179
if ($ret->field === null) {

src/Components/CreateDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CreateDefinition extends Component
5555

5656
// Generated columns options.
5757
'GENERATED ALWAYS' => 8,
58-
'AS' => array(9, 'expr', array('bracketsDelimited' => true)),
58+
'AS' => array(9, 'expr', array('parenthesesDelimited' => true)),
5959
'VIRTUAL' => 10,
6060
'PERSISTENT' => 11,
6161
'STORED' => 11,

src/Components/Expression.php

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ public function __construct($database = null, $table = null, $column = null, $al
119119
}
120120

121121
/**
122+
* Possible options:
123+
*
124+
* `field`
125+
*
126+
* First field to be filled.
127+
* If this is not specified, it takes the value of `parseField`.
128+
*
129+
* `parseField`
130+
*
131+
* Specifies the type of the field parsed. It may be `database`,
132+
* `table` or `column`. These expressions may not include
133+
* parentheses.
134+
*
135+
* `breakOnAlias`
136+
*
137+
* If not empty, breaks when the alias occurs (it is not included).
138+
*
139+
* `breakOnParentheses`
140+
*
141+
* If not empty, breaks when the first parentheses occurs.
142+
*
143+
* `parenthesesDelimited`
144+
*
145+
* If not empty, breaks after last parentheses occurred.
146+
*
122147
* @param Parser $parser The parser that serves as context.
123148
* @param TokensList $list The list of tokens that are being parsed.
124149
* @param array $options Parameters for parsing.
@@ -164,6 +189,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
164189
*/
165190
$prev = array(null, null);
166191

192+
// When a field is parsed, no parentheses are expected.
193+
if (!empty($options['parseField'])) {
194+
$options['breakOnParentheses'] = true;
195+
$options['field'] = $options['parseField'];
196+
}
197+
167198
for (; $list->idx < $list->count; ++$list->idx) {
168199

169200
/**
@@ -195,7 +226,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
195226
// A `(` was previously found and this keyword is the
196227
// beginning of a statement, so this is a subquery.
197228
$ret->subquery = $token->value;
198-
} elseif ($token->flags & Token::FLAG_KEYWORD_FUNCTION) {
229+
} elseif (($token->flags & Token::FLAG_KEYWORD_FUNCTION)
230+
&& (empty($options['parseField']))
231+
) {
199232
$isExpr = true;
200233
} elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED)
201234
&& ($brackets === 0)
@@ -207,7 +240,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
207240
break;
208241
}
209242
if ($token->value === 'AS') {
210-
if (!empty($options['noAlias'])) {
243+
if (!empty($options['breakOnAlias'])) {
211244
break;
212245
}
213246
if (!empty($ret->alias)) {
@@ -224,8 +257,24 @@ public static function parse(Parser $parser, TokensList $list, array $options =
224257
}
225258
}
226259

260+
if (($token->type === Token::TYPE_NUMBER)
261+
|| ($token->type === Token::TYPE_BOOL)
262+
|| (($token->type === Token::TYPE_SYMBOL)
263+
&& ($token->flags & Token::FLAG_SYMBOL_VARIABLE))
264+
|| (($token->type === Token::TYPE_OPERATOR)
265+
&& ($token->value !== '.'))
266+
) {
267+
if (!empty($options['parseField'])) {
268+
break;
269+
}
270+
271+
// Numbers, booleans and operators (except dot) are usually part
272+
// of expressions.
273+
$isExpr = true;
274+
}
275+
227276
if ($token->type === Token::TYPE_OPERATOR) {
228-
if ((!empty($options['noBrackets']))
277+
if ((!empty($options['breakOnParentheses']))
229278
&& (($token->value === '(') || ($token->value === ')'))
230279
) {
231280
// No brackets were expected.
@@ -244,7 +293,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
244293
} elseif ($token->value === ')') {
245294
--$brackets;
246295
if ($brackets === 0) {
247-
if (!empty($options['bracketsDelimited'])) {
296+
if (!empty($options['parenthesesDelimited'])) {
248297
// The current token is the last bracket, the next
249298
// one will be outside the expression.
250299
$ret->expr .= $token->token;
@@ -264,19 +313,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
264313
}
265314
}
266315

267-
if (($token->type === Token::TYPE_NUMBER)
268-
|| ($token->type === Token::TYPE_BOOL)
269-
|| (($token->type === Token::TYPE_SYMBOL)
270-
&& ($token->flags & Token::FLAG_SYMBOL_VARIABLE))
271-
|| (($token->type === Token::TYPE_OPERATOR)
272-
&& ($token->value !== '.'))
273-
) {
274-
// Numbers, booleans and operators (except dot) are usually part
275-
// of expressions.
276-
$isExpr = true;
277-
}
278-
279-
// Saving the previous token.
316+
// Saving the previous tokens.
280317
$prev[0] = $prev[1];
281318
$prev[1] = $token;
282319

@@ -323,14 +360,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
323360
$dot = true;
324361
$ret->expr .= $token->token;
325362
} else {
326-
$field = (!empty($options['skipColumn'])) ? 'table' : 'column';
363+
$field = empty($options['field']) ? 'column' : $options['field'];
327364
if (empty($ret->$field)) {
328365
$ret->$field = $token->value;
329366
$ret->expr .= $token->token;
330367
$dot = false;
331368
} else {
332369
// No alias is expected.
333-
if (!empty($options['noAlias'])) {
370+
if (!empty($options['breakOnAlias'])) {
334371
break;
335372
}
336373
if (!empty($ret->alias)) {

src/Components/IntoKeyword.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
107107
$parser,
108108
$list,
109109
array(
110-
'noAlias' => true,
111-
'noBrackets' => true,
112-
'skipColumn' => true,
110+
'parseField' => 'table',
111+
'breakOnAlias' => true,
113112
)
114113
);
115114
$state = 1;

src/Components/JoinKeyword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
128128
break;
129129
}
130130
} elseif ($state === 1) {
131-
$expr->expr = Expression::parse($parser, $list, array('skipColumn' => true));
131+
$expr->expr = Expression::parse($parser, $list, array('field' => 'table'));
132132
$state = 2;
133133
} elseif ($state === 2) {
134134
if (($token->type === Token::TYPE_KEYWORD) && ($token->value === 'ON')) {

src/Components/PartitionDefinition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
169169
$parser,
170170
$list,
171171
array(
172-
'bracketsDelimited' => true,
173-
'noAlias' => true,
172+
'parenthesesDelimited' => true,
173+
'breakOnAlias' => true,
174174
)
175175
);
176176
}

src/Components/Reference.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
121121
$parser,
122122
$list,
123123
array(
124-
'noAlias' => true,
125-
'skipColumn' => true,
126-
'noBrackets' => true,
124+
'parseField' => 'table',
125+
'breakOnAlias' => true,
127126
)
128127
);
129128
$state = 1;

src/Components/RenameOperation.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9393
$parser,
9494
$list,
9595
array(
96-
'noAlias' => true,
97-
'noBrackets' => true,
98-
'skipColumn' => true,
96+
'breakOnAlias' => true,
97+
'parseField' => 'table',
9998
)
10099
);
101100
if (empty($expr->old)) {
@@ -120,9 +119,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
120119
$parser,
121120
$list,
122121
array(
123-
'noBrackets' => true,
124-
'skipColumn' => true,
125-
'noAlias' => true,
122+
'breakOnAlias' => true,
123+
'parseField' => 'table',
126124
)
127125
);
128126
if (empty($expr->new)) {

src/Components/SetOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
100100
$parser,
101101
$list,
102102
array(
103-
'noAlias' => true,
103+
'breakOnAlias' => true,
104104
)
105105
);
106106
if ($tmp == null) {

src/Parser.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ class Parser
125125
'ALTER' => array(
126126
'class' => 'SqlParser\\Components\\Expression',
127127
'field' => 'table',
128-
'options' => array('skipColumn' => true),
128+
'options' => array('parseField' => 'table'),
129129
),
130130
'ANALYZE' => array(
131131
'class' => 'SqlParser\\Components\\ExpressionArray',
132132
'field' => 'tables',
133-
'options' => array('skipColumn' => true),
133+
'options' => array('parseField' => 'table'),
134134
),
135135
'BACKUP' => array(
136136
'class' => 'SqlParser\\Components\\ExpressionArray',
137137
'field' => 'tables',
138-
'options' => array('skipColumn' => true),
138+
'options' => array('parseField' => 'table'),
139139
),
140140
'CALL' => array(
141141
'class' => 'SqlParser\\Components\\FunctionCall',
@@ -144,22 +144,22 @@ class Parser
144144
'CHECK' => array(
145145
'class' => 'SqlParser\\Components\\ExpressionArray',
146146
'field' => 'tables',
147-
'options' => array('skipColumn' => true),
147+
'options' => array('parseField' => 'table'),
148148
),
149149
'CHECKSUM' => array(
150150
'class' => 'SqlParser\\Components\\ExpressionArray',
151151
'field' => 'tables',
152-
'options' => array('skipColumn' => true),
152+
'options' => array('parseField' => 'table'),
153153
),
154154
'DROP' => array(
155155
'class' => 'SqlParser\\Components\\ExpressionArray',
156156
'field' => 'fields',
157-
'options' => array('skipColumn' => true),
157+
'options' => array('parseField' => 'table'),
158158
),
159159
'FROM' => array(
160160
'class' => 'SqlParser\\Components\\ExpressionArray',
161161
'field' => 'from',
162-
'options' => array('skipColumn' => true),
162+
'options' => array('parseField' => 'table'),
163163
),
164164
'GROUP BY' => array(
165165
'class' => 'SqlParser\\Components\\OrderKeyword',
@@ -212,7 +212,7 @@ class Parser
212212
'OPTIMIZE' => array(
213213
'class' => 'SqlParser\\Components\\ExpressionArray',
214214
'field' => 'tables',
215-
'options' => array('skipColumn' => true),
215+
'options' => array('parseField' => 'table'),
216216
),
217217
'ORDER BY' => array(
218218
'class' => 'SqlParser\\Components\\OrderKeyword',
@@ -233,12 +233,12 @@ class Parser
233233
'REPAIR' => array(
234234
'class' => 'SqlParser\\Components\\ExpressionArray',
235235
'field' => 'tables',
236-
'options' => array('skipColumn' => true),
236+
'options' => array('parseField' => 'table'),
237237
),
238238
'RESTORE' => array(
239239
'class' => 'SqlParser\\Components\\ExpressionArray',
240240
'field' => 'tables',
241-
'options' => array('skipColumn' => true),
241+
'options' => array('parseField' => 'table'),
242242
),
243243
'SET' => array(
244244
'class' => 'SqlParser\\Components\\SetOperation',
@@ -251,12 +251,12 @@ class Parser
251251
'TRUNCATE' => array(
252252
'class' => 'SqlParser\\Components\\Expression',
253253
'field' => 'table',
254-
'options' => array('skipColumn' => true),
254+
'options' => array('parseField' => 'table'),
255255
),
256256
'UPDATE' => array(
257257
'class' => 'SqlParser\\Components\\ExpressionArray',
258258
'field' => 'tables',
259-
'options' => array('skipColumn' => true),
259+
'options' => array('parseField' => 'table'),
260260
),
261261
'VALUE' => array(
262262
'class' => 'SqlParser\\Components\\Array2d',

src/Statements/AlterStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function parse(Parser $parser, TokensList $list)
7676
$parser,
7777
$list,
7878
array(
79-
'noAlias' => true,
80-
'noBrackets' => true,
79+
'parseField' => 'column',
80+
'breakOnAlias' => true,
8181
)
8282
);
8383
++$list->idx; // Skipping field.

src/Statements/CreateStatement.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,8 @@ public function parse(Parser $parser, TokensList $list)
342342
$parser,
343343
$list,
344344
array(
345-
'noAlias' => true,
346-
'noBrackets' => true,
347-
'skipColumn' => true,
345+
'parseField' => 'table',
346+
'breakOnAlias' => true,
348347
)
349348
);
350349

@@ -538,9 +537,8 @@ public function parse(Parser $parser, TokensList $list)
538537
$parser,
539538
$list,
540539
array(
541-
'noAlias' => true,
542-
'noBrackets' => true,
543-
'skipColumn' => true,
540+
'parseField' => 'table',
541+
'breakOnAlias' => true,
544542
)
545543
);
546544
++$list->idx;

tests/Components/ArrayObjTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testParseType()
3030
array(
3131
'type' => 'SqlParser\\Components\\Expression',
3232
'typeOptions' => array(
33-
'noBrackets' => true,
33+
'breakOnParentheses' => true,
3434
),
3535
)
3636
);

tests/Components/ExpressionArrayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testParse()
1616
new Parser(),
1717
$this->getTokensList('(expr)'),
1818
array(
19-
'noBrackets' => true,
19+
'breakOnParentheses' => true,
2020
)
2121
);
2222
$this->assertEquals(array(), $component);
@@ -28,7 +28,7 @@ public function testParse2()
2828
new Parser(),
2929
$this->getTokensList('(expr) +'),
3030
array(
31-
'bracketsDelimited' => true,
31+
'parenthesesDelimited' => true,
3232
)
3333
);
3434
$this->assertEquals(1, count($component));

tests/Components/OptionsArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testParseExpr()
4242
new Parser(),
4343
$this->getTokensList('SUM = (3 + 5) RESULT = 8'),
4444
array(
45-
'SUM' => array(1, 'expr', array('bracketsDelimited' => true)),
45+
'SUM' => array(1, 'expr', array('parenthesesDelimited' => true)),
4646
'RESULT' => array(2, 'var'),
4747
)
4848
);

0 commit comments

Comments
 (0)