1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\FunctionCall; |
8
|
|
|
use PhpMyAdmin\SqlParser\Components\OptionsArray; |
9
|
|
|
use Stringable; |
10
|
|
|
|
11
|
|
|
use function array_flip; |
12
|
|
|
use function array_keys; |
13
|
|
|
use function count; |
14
|
|
|
use function in_array; |
15
|
|
|
use function stripos; |
16
|
|
|
use function trim; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The result of the parser is an array of statements are extensions of the class defined here. |
20
|
|
|
* |
21
|
|
|
* A statement represents the result of parsing the lexemes. |
22
|
|
|
* |
23
|
|
|
* Abstract statement definition. |
24
|
|
|
*/ |
25
|
|
|
#[\AllowDynamicProperties] |
26
|
|
|
abstract class Statement implements Stringable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Options for this statement. |
30
|
|
|
* |
31
|
|
|
* The option would be the key and the value can be an integer or an array. |
32
|
|
|
* |
33
|
|
|
* The integer represents only the index used. |
34
|
|
|
* |
35
|
|
|
* The array may have two keys: `0` is used to represent the index used and |
36
|
|
|
* `1` is the type of the option (which may be 'var' or 'var='). Both |
37
|
|
|
* options mean they expect a value after the option (e.g. `A = B` or `A B`, |
38
|
|
|
* in which case `A` is the key and `B` is the value). The only difference |
39
|
|
|
* is in the building process. `var` options are built as `A B` and `var=` |
40
|
|
|
* options are built as `A = B` |
41
|
|
|
* |
42
|
|
|
* Two options that can be used together must have different values for |
43
|
|
|
* indexes, else, when they will be used together, an error will occur. |
44
|
|
|
* |
45
|
|
|
* @var array<string, int|array<int, int|string>> |
46
|
|
|
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})> |
47
|
|
|
*/ |
48
|
|
|
public static $OPTIONS = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The clauses of this statement, in order. |
52
|
|
|
* |
53
|
|
|
* The value attributed to each clause is used by the builder and it may |
54
|
|
|
* have one of the following values: |
55
|
|
|
* |
56
|
|
|
* - 1 = 01 - add the clause only |
57
|
|
|
* - 2 = 10 - add the keyword |
58
|
|
|
* - 3 = 11 - add both the keyword and the clause |
59
|
|
|
* |
60
|
|
|
* @var array<string, array<int, int|string>> |
61
|
|
|
* @psalm-var array<string, array{non-empty-string, (1|2|3)}> |
62
|
|
|
*/ |
63
|
|
|
public static $CLAUSES = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Options that can be given to GROUP BY component. |
67
|
|
|
* |
68
|
|
|
* @var array<string, int|array<int, int|string>> |
69
|
|
|
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})> |
70
|
|
|
*/ |
71
|
|
|
public static $GROUP_OPTIONS = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array<string, int|array<int, int|string>> |
75
|
|
|
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})> |
76
|
|
|
*/ |
77
|
|
|
public static $END_OPTIONS = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The options of this query. |
81
|
|
|
* |
82
|
|
|
* @see static::$OPTIONS |
83
|
|
|
* |
84
|
|
|
* @var OptionsArray|null |
85
|
|
|
*/ |
86
|
|
|
public $options; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* The index of the first token used in this statement. |
90
|
|
|
* |
91
|
|
|
* @var int|null |
92
|
|
|
*/ |
93
|
|
|
public $first; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The index of the last token used in this statement. |
97
|
|
|
* |
98
|
|
|
* @var int|null |
99
|
|
|
*/ |
100
|
|
|
public $last; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Parser|null $parser the instance that requests parsing |
104
|
|
|
* @param TokensList|null $list the list of tokens to be parsed |
105
|
|
|
*/ |
106
|
1880 |
|
public function __construct(?Parser $parser = null, ?TokensList $list = null) |
107
|
|
|
{ |
108
|
1880 |
|
if (($parser === null) || ($list === null)) { |
109
|
12 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
1872 |
|
$this->parse($parser, $list); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Builds the string representation of this statement. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
128 |
|
public function build() |
121
|
|
|
{ |
122
|
|
|
/** |
123
|
|
|
* Query to be returned. |
124
|
|
|
* |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
128 |
|
$query = ''; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Clauses which were built already. |
131
|
|
|
* |
132
|
|
|
* It is required to keep track of built clauses because some fields, |
133
|
|
|
* for example `join` is used by multiple clauses (`JOIN`, `LEFT JOIN`, |
134
|
|
|
* `LEFT OUTER JOIN`, etc.). The same happens for `VALUE` and `VALUES`. |
135
|
|
|
* |
136
|
|
|
* A clause is considered built just after fields' value |
137
|
|
|
* (`$this->field`) was used in building. |
138
|
|
|
*/ |
139
|
128 |
|
$built = []; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Statement's clauses. |
143
|
|
|
*/ |
144
|
128 |
|
$clauses = $this->getClauses(); |
145
|
|
|
|
146
|
128 |
|
foreach ($clauses as $clause) { |
147
|
|
|
/** |
148
|
|
|
* The name of the clause. |
149
|
|
|
* |
150
|
|
|
* @var string |
151
|
|
|
*/ |
152
|
128 |
|
$name = $clause[0]; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* The type of the clause. |
156
|
|
|
* |
157
|
|
|
* @see self::$CLAUSES |
158
|
|
|
* |
159
|
|
|
* @var int |
160
|
|
|
*/ |
161
|
128 |
|
$type = $clause[1]; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* The builder (parser) of this clause. |
165
|
|
|
* |
166
|
|
|
* @var Component |
167
|
|
|
*/ |
168
|
128 |
|
$class = Parser::$KEYWORD_PARSERS[$name]['class']; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* The name of the field that is used as source for the builder. |
172
|
|
|
* Same field is used to store the result of parsing. |
173
|
|
|
* |
174
|
|
|
* @var string |
175
|
|
|
*/ |
176
|
128 |
|
$field = Parser::$KEYWORD_PARSERS[$name]['field']; |
177
|
|
|
|
178
|
|
|
// The field is empty, there is nothing to be built. |
179
|
128 |
|
if (empty($this->$field)) { |
180
|
128 |
|
continue; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// Checking if this field was already built. |
184
|
128 |
|
if ($type & 1) { |
185
|
128 |
|
if (! empty($built[$field])) { |
186
|
52 |
|
continue; |
187
|
|
|
} |
188
|
|
|
|
189
|
128 |
|
$built[$field] = true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Checking if the name of the clause should be added. |
193
|
128 |
|
if ($type & 2) { |
194
|
128 |
|
$query = trim($query) . ' ' . $name; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Checking if the result of the builder should be added. |
198
|
128 |
|
if (! ($type & 1)) { |
199
|
128 |
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
128 |
|
$query = trim($query) . ' ' . $class::build($this->$field); |
203
|
|
|
} |
204
|
|
|
|
205
|
128 |
|
return $query; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Parses the statements defined by the tokens list. |
210
|
|
|
* |
211
|
|
|
* @param Parser $parser the instance that requests parsing |
212
|
|
|
* @param TokensList $list the list of tokens to be parsed |
213
|
|
|
* |
214
|
|
|
* @return void |
215
|
|
|
* |
216
|
|
|
* @throws Exceptions\ParserException |
217
|
|
|
*/ |
218
|
932 |
|
public function parse(Parser $parser, TokensList $list) |
219
|
|
|
{ |
220
|
|
|
/** |
221
|
|
|
* Array containing all list of clauses parsed. |
222
|
|
|
* This is used to check for duplicates. |
223
|
|
|
*/ |
224
|
932 |
|
$parsedClauses = []; |
225
|
|
|
|
226
|
|
|
// This may be corrected by the parser. |
227
|
932 |
|
$this->first = $list->idx; |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Whether options were parsed or not. |
231
|
|
|
* For statements that do not have any options this is set to `true` by |
232
|
|
|
* default. |
233
|
|
|
*/ |
234
|
932 |
|
$parsedOptions = empty(static::$OPTIONS); |
235
|
|
|
|
236
|
932 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
237
|
|
|
/** |
238
|
|
|
* Token parsed at this moment. |
239
|
|
|
*/ |
240
|
932 |
|
$token = $list->tokens[$list->idx]; |
241
|
|
|
|
242
|
|
|
// End of statement. |
243
|
932 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
244
|
888 |
|
break; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// Checking if this closing bracket is the pair for a bracket |
248
|
|
|
// outside the statement. |
249
|
932 |
|
if (($token->value === ')') && ($parser->brackets > 0)) { |
250
|
28 |
|
--$parser->brackets; |
251
|
28 |
|
continue; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// Only keywords are relevant here. Other parts of the query are |
255
|
|
|
// processed in the functions below. |
256
|
932 |
|
if ($token->type !== Token::TYPE_KEYWORD) { |
257
|
72 |
|
if (($token->type !== Token::TYPE_COMMENT) && ($token->type !== Token::TYPE_WHITESPACE)) { |
258
|
40 |
|
$parser->error('Unexpected token.', $token); |
259
|
|
|
} |
260
|
|
|
|
261
|
72 |
|
continue; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Unions are parsed by the parser because they represent more than |
265
|
|
|
// one statement. |
266
|
|
|
if ( |
267
|
932 |
|
($token->keyword === 'UNION') || |
268
|
932 |
|
($token->keyword === 'UNION ALL') || |
269
|
932 |
|
($token->keyword === 'UNION DISTINCT') || |
270
|
932 |
|
($token->keyword === 'EXCEPT') || |
271
|
932 |
|
($token->keyword === 'INTERSECT') |
272
|
|
|
) { |
273
|
80 |
|
break; |
274
|
|
|
} |
275
|
|
|
|
276
|
932 |
|
$lastIdx = $list->idx; |
277
|
|
|
|
278
|
|
|
// ON DUPLICATE KEY UPDATE ... |
279
|
|
|
// has to be parsed in parent statement (INSERT or REPLACE) |
280
|
|
|
// so look for it and break |
281
|
932 |
|
if ($this instanceof Statements\SelectStatement && $token->value === 'ON') { |
282
|
12 |
|
++$list->idx; // Skip ON |
283
|
|
|
|
284
|
|
|
// look for ON DUPLICATE KEY UPDATE |
285
|
12 |
|
$first = $list->getNextOfType(Token::TYPE_KEYWORD); |
286
|
12 |
|
$second = $list->getNextOfType(Token::TYPE_KEYWORD); |
287
|
12 |
|
$third = $list->getNextOfType(Token::TYPE_KEYWORD); |
288
|
|
|
|
289
|
|
|
if ( |
290
|
12 |
|
$first && $second && $third |
291
|
12 |
|
&& $first->value === 'DUPLICATE' |
292
|
12 |
|
&& $second->value === 'KEY' |
293
|
12 |
|
&& $third->value === 'UPDATE' |
294
|
|
|
) { |
295
|
12 |
|
$list->idx = $lastIdx; |
296
|
12 |
|
break; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
932 |
|
$list->idx = $lastIdx; |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* The name of the class that is used for parsing. |
304
|
|
|
* |
305
|
|
|
* @var Component |
306
|
|
|
*/ |
307
|
932 |
|
$class = null; |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* The name of the field where the result of the parsing is stored. |
311
|
|
|
* |
312
|
|
|
* @var string |
313
|
|
|
*/ |
314
|
932 |
|
$field = null; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Parser's options. |
318
|
|
|
*/ |
319
|
932 |
|
$options = []; |
320
|
|
|
|
321
|
|
|
// Looking for duplicated clauses. |
322
|
|
|
if ( |
323
|
932 |
|
! empty(Parser::$KEYWORD_PARSERS[$token->value]) |
324
|
932 |
|
|| ! empty(Parser::$STATEMENT_PARSERS[$token->value]) |
325
|
|
|
) { |
326
|
932 |
|
if (! empty($parsedClauses[$token->value])) { |
327
|
32 |
|
$parser->error('This type of clause was previously parsed.', $token); |
328
|
32 |
|
break; |
329
|
|
|
} |
330
|
|
|
|
331
|
932 |
|
$parsedClauses[$token->value] = true; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
// Checking if this is the beginning of a clause. |
335
|
|
|
// Fix Issue #221: As `truncate` is not a keyword |
336
|
|
|
// but it might be the beginning of a statement of truncate, |
337
|
|
|
// so let the value use the keyword field for truncate type. |
338
|
932 |
|
$tokenValue = in_array($token->keyword, ['TRUNCATE']) ? $token->keyword : $token->value; |
339
|
932 |
|
if (! empty(Parser::$KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) { |
340
|
928 |
|
$class = Parser::$KEYWORD_PARSERS[$tokenValue]['class']; |
341
|
928 |
|
$field = Parser::$KEYWORD_PARSERS[$tokenValue]['field']; |
342
|
928 |
|
if (! empty(Parser::$KEYWORD_PARSERS[$tokenValue]['options'])) { |
343
|
728 |
|
$options = Parser::$KEYWORD_PARSERS[$tokenValue]['options']; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// Checking if this is the beginning of the statement. |
348
|
932 |
|
if (! empty(Parser::$STATEMENT_PARSERS[$token->keyword])) { |
349
|
|
|
if ( |
350
|
932 |
|
! empty(static::$CLAUSES) // Undefined for some statements. |
351
|
932 |
|
&& empty(static::$CLAUSES[$token->value]) |
352
|
|
|
) { |
353
|
|
|
// Some keywords (e.g. `SET`) may be the beginning of a |
354
|
|
|
// statement and a clause. |
355
|
|
|
// If such keyword was found and it cannot be a clause of |
356
|
|
|
// this statement it means it is a new statement, but no |
357
|
|
|
// delimiter was found between them. |
358
|
12 |
|
$parser->error( |
359
|
12 |
|
'A new statement was found, but no delimiter between it and the previous one.', |
360
|
12 |
|
$token |
361
|
12 |
|
); |
362
|
12 |
|
break; |
363
|
|
|
} |
364
|
|
|
|
365
|
932 |
|
if (! $parsedOptions) { |
366
|
876 |
|
if (empty(static::$OPTIONS[$token->value])) { |
367
|
|
|
// Skipping keyword because if it is not a option. |
368
|
872 |
|
++$list->idx; |
369
|
|
|
} |
370
|
|
|
|
371
|
876 |
|
$this->options = OptionsArray::parse($parser, $list, static::$OPTIONS); |
372
|
932 |
|
$parsedOptions = true; |
373
|
|
|
} |
374
|
644 |
|
} elseif ($class === null) { |
375
|
60 |
|
if ($this instanceof Statements\SelectStatement && $token->value === 'WITH ROLLUP') { |
376
|
|
|
// Handle group options in Select statement |
377
|
|
|
// See Statements\SelectStatement::$GROUP_OPTIONS |
378
|
|
|
$this->group_options = OptionsArray::parse($parser, $list, static::$GROUP_OPTIONS); |
379
|
|
|
} elseif ( |
380
|
60 |
|
$this instanceof Statements\SelectStatement |
381
|
60 |
|
&& ($token->value === 'FOR UPDATE' |
382
|
60 |
|
|| $token->value === 'LOCK IN SHARE MODE') |
383
|
|
|
) { |
384
|
|
|
// Handle special end options in Select statement |
385
|
|
|
// See Statements\SelectStatement::$END_OPTIONS |
386
|
16 |
|
$this->end_options = OptionsArray::parse($parser, $list, static::$END_OPTIONS); |
387
|
|
|
} elseif ( |
388
|
44 |
|
$this instanceof Statements\SetStatement |
389
|
44 |
|
&& ($token->value === 'COLLATE' |
390
|
44 |
|
|| $token->value === 'DEFAULT') |
391
|
|
|
) { |
392
|
|
|
// Handle special end options in SET statement |
393
|
|
|
// See Statements\SetStatement::$END_OPTIONS |
394
|
4 |
|
$this->end_options = OptionsArray::parse($parser, $list, static::$END_OPTIONS); |
395
|
|
|
} else { |
396
|
|
|
// There is no parser for this keyword and isn't the beginning |
397
|
|
|
// of a statement (so no options) either. |
398
|
40 |
|
$parser->error('Unrecognized keyword.', $token); |
399
|
40 |
|
continue; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
932 |
|
$this->before($parser, $list, $token); |
404
|
|
|
|
405
|
|
|
// Parsing this keyword. |
406
|
932 |
|
if ($class !== null) { |
407
|
|
|
// We can't parse keyword at the end of statement |
408
|
928 |
|
if ($list->idx >= $list->count) { |
409
|
4 |
|
$parser->error('Keyword at end of statement.', $token); |
410
|
4 |
|
continue; |
411
|
|
|
} |
412
|
|
|
|
413
|
924 |
|
++$list->idx; // Skipping keyword or last option. |
414
|
924 |
|
$this->$field = $class::parse($parser, $list, $options); |
415
|
|
|
} |
416
|
|
|
|
417
|
928 |
|
$this->after($parser, $list, $token); |
418
|
|
|
|
419
|
|
|
// #223 Here may make a patch, if last is delimiter, back one |
420
|
928 |
|
if ($class !== FunctionCall::class || $list->offsetGet($list->idx)->type !== Token::TYPE_DELIMITER) { |
421
|
924 |
|
continue; |
422
|
|
|
} |
423
|
|
|
|
424
|
4 |
|
--$list->idx; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
// This may be corrected by the parser. |
428
|
932 |
|
$this->last = --$list->idx; // Go back to last used token. |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Function called before the token is processed. |
433
|
|
|
* |
434
|
|
|
* @param Parser $parser the instance that requests parsing |
435
|
|
|
* @param TokensList $list the list of tokens to be parsed |
436
|
|
|
* @param Token $token the token that is being parsed |
437
|
|
|
* |
438
|
|
|
* @return void |
439
|
|
|
*/ |
440
|
896 |
|
public function before(Parser $parser, TokensList $list, Token $token) |
|
|
|
|
441
|
|
|
{ |
442
|
896 |
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Function called after the token was processed. |
446
|
|
|
* |
447
|
|
|
* @param Parser $parser the instance that requests parsing |
448
|
|
|
* @param TokensList $list the list of tokens to be parsed |
449
|
|
|
* @param Token $token the token that is being parsed |
450
|
|
|
* |
451
|
|
|
* @return void |
452
|
|
|
*/ |
453
|
912 |
|
public function after(Parser $parser, TokensList $list, Token $token) |
|
|
|
|
454
|
|
|
{ |
455
|
912 |
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Gets the clauses of this statement. |
459
|
|
|
* |
460
|
|
|
* @return array<string, array<int, int|string>> |
461
|
|
|
* @psalm-return array<string, array{non-empty-string, (1|2|3)}> |
462
|
|
|
*/ |
463
|
1344 |
|
public function getClauses() |
464
|
|
|
{ |
465
|
1344 |
|
return static::$CLAUSES; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Builds the string representation of this statement. |
470
|
|
|
* |
471
|
|
|
* @see static::build |
472
|
|
|
* |
473
|
|
|
* @return string |
474
|
|
|
*/ |
475
|
32 |
|
public function __toString() |
476
|
|
|
{ |
477
|
32 |
|
return $this->build(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Validates the order of the clauses in parsed statement |
482
|
|
|
* Ideally this should be called after successfully |
483
|
|
|
* completing the parsing of each statement. |
484
|
|
|
* |
485
|
|
|
* @param Parser $parser the instance that requests parsing |
486
|
|
|
* @param TokensList $list the list of tokens to be parsed |
487
|
|
|
* |
488
|
|
|
* @return bool |
489
|
|
|
* |
490
|
|
|
* @throws Exceptions\ParserException |
491
|
|
|
*/ |
492
|
1872 |
|
public function validateClauseOrder($parser, $list) |
493
|
|
|
{ |
494
|
1872 |
|
$clauses = array_flip(array_keys($this->getClauses())); |
495
|
|
|
|
496
|
1872 |
|
if (empty($clauses) || count($clauses) === 0) { |
497
|
1096 |
|
return true; |
498
|
|
|
} |
499
|
|
|
|
500
|
888 |
|
$minIdx = -1; |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* For tracking JOIN clauses in a query |
504
|
|
|
* = 0 - JOIN not found till now |
505
|
|
|
* > 0 - Index of first JOIN clause in the statement. |
506
|
|
|
* |
507
|
|
|
* @var int |
508
|
|
|
*/ |
509
|
888 |
|
$minJoin = 0; |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* For tracking JOIN clauses in a query |
513
|
|
|
* = 0 - JOIN not found till now |
514
|
|
|
* > 0 - Index of last JOIN clause |
515
|
|
|
* (which appears together with other JOINs) |
516
|
|
|
* in the statement. |
517
|
|
|
* |
518
|
|
|
* @var int |
519
|
|
|
*/ |
520
|
888 |
|
$maxJoin = 0; |
521
|
|
|
|
522
|
888 |
|
$error = 0; |
523
|
888 |
|
$lastIdx = 0; |
524
|
888 |
|
foreach ($clauses as $clauseType => $index) { |
525
|
888 |
|
$clauseStartIdx = Utils\Query::getClauseStartOffset($this, $list, $clauseType); |
526
|
|
|
|
527
|
|
|
if ( |
528
|
888 |
|
$clauseStartIdx !== -1 |
529
|
888 |
|
&& $this instanceof Statements\SelectStatement |
530
|
888 |
|
&& ($clauseType === 'FORCE' |
531
|
888 |
|
|| $clauseType === 'IGNORE' |
532
|
888 |
|
|| $clauseType === 'USE') |
533
|
|
|
) { |
534
|
|
|
// TODO: ordering of clauses in a SELECT statement with |
535
|
|
|
// Index hints is not supported |
536
|
28 |
|
return true; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
// Handle ordering of Multiple Joins in a query |
540
|
888 |
|
if ($clauseStartIdx !== -1) { |
541
|
872 |
|
if ($minJoin === 0 && stripos($clauseType, 'JOIN')) { |
542
|
|
|
// First JOIN clause is detected |
543
|
88 |
|
$minJoin = $maxJoin = $clauseStartIdx; |
544
|
872 |
|
} elseif ($minJoin !== 0 && ! stripos($clauseType, 'JOIN')) { |
545
|
|
|
// After a previous JOIN clause, a non-JOIN clause has been detected |
546
|
48 |
|
$maxJoin = $lastIdx; |
547
|
872 |
|
} elseif ($maxJoin < $clauseStartIdx && stripos($clauseType, 'JOIN')) { |
548
|
4 |
|
$error = 1; |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
552
|
888 |
|
if ($clauseStartIdx !== -1 && $clauseStartIdx < $minIdx) { |
553
|
24 |
|
if ($minJoin === 0 || $error === 1) { |
554
|
20 |
|
$token = $list->tokens[$clauseStartIdx]; |
555
|
20 |
|
$parser->error('Unexpected ordering of clauses.', $token); |
556
|
|
|
|
557
|
20 |
|
return false; |
558
|
|
|
} |
559
|
|
|
|
560
|
4 |
|
$minIdx = $clauseStartIdx; |
561
|
888 |
|
} elseif ($clauseStartIdx !== -1) { |
562
|
872 |
|
$minIdx = $clauseStartIdx; |
563
|
|
|
} |
564
|
|
|
|
565
|
888 |
|
$lastIdx = $clauseStartIdx !== -1 ? $clauseStartIdx : $lastIdx; |
566
|
|
|
} |
567
|
|
|
|
568
|
844 |
|
return true; |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.