-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathWithStatement.php
More file actions
330 lines (278 loc) · 11.2 KB
/
WithStatement.php
File metadata and controls
330 lines (278 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Statements;
use PhpMyAdmin\SqlParser\Components\WithKeyword;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Parsers\Array2d;
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use PhpMyAdmin\SqlParser\Translator;
use function array_slice;
use function implode;
use function preg_match;
/**
* `WITH` statement.
* WITH [RECURSIVE] query_name [ (column_name [,...]) ] AS (SELECT ...) [, ...]
*/
final class WithStatement extends Statement
{
/**
* Options for `WITH` statements and their slot ID.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static array $statementOptions = ['RECURSIVE' => 1];
/**
* The clauses of this statement, in order.
*
* @see Statement::$clauses
*
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
*/
public static array $clauses = [
'WITH' => [
'WITH',
Statement::ADD_KEYWORD,
],
// Used for options.
'_OPTIONS' => [
'_OPTIONS',
Statement::ADD_CLAUSE,
],
'AS' => [
'AS',
Statement::ADD_KEYWORD,
],
];
/** @var WithKeyword[] */
public array $withers = [];
/**
* holds the CTE parser.
*/
public Parser|null $cteStatementParser = null;
/**
* @param Parser $parser the instance that requests parsing
* @param TokensList $list the list of tokens to be parsed
*/
public function parse(Parser $parser, TokensList $list): void
{
/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ---------------- [ name ] -----------------> 1
*
* 1 ------------------ [ ( ] ------------------> 2
*
* 2 ------------------ [ AS ] -----------------> 3
*
* 3 ------------------ [ ( ] ------------------> 4
*
* 4 ------------------ [ , ] ------------------> 1
*
* 4 ----- [ SELECT/UPDATE/DELETE/INSERT ] -----> 5
*/
$state = 0;
$wither = null;
++$list->idx; // Skipping `WITH`.
// parse any options if provided
$this->options = OptionsArrays::parse($parser, $list, static::$statementOptions);
++$list->idx;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// Skipping whitespaces and comments.
if ($token->type === TokenType::Whitespace || $token->type === TokenType::Comment) {
continue;
}
if ($state === 0) {
if ($token->type !== TokenType::None || ! preg_match('/^[a-zA-Z0-9_$]+$/', $token->token)) {
$parser->error('The name of the CTE was expected.', $token);
break;
}
$wither = $token->value;
$this->withers[$wither] = new WithKeyword($wither);
$state = 1;
} elseif ($state === 1) {
if ($token->type === TokenType::Operator && $token->value === '(') {
$columns = Array2d::parse($parser, $list);
if ($parser->errors !== []) {
break;
}
$this->withers[$wither]->columns = $columns;
$state = 2;
} elseif ($token->type === TokenType::Keyword && $token->keyword === 'AS') {
$state = 3;
} else {
$parser->error('Unexpected token.', $token);
break;
}
} elseif ($state === 2) {
if (! ($token->type === TokenType::Keyword && $token->keyword === 'AS')) {
$parser->error('AS keyword was expected.', $token);
break;
}
$state = 3;
} elseif ($state === 3) {
$idxBeforeGetNext = $list->idx;
$list->idx++; // Ignore the current token
$nextKeyword = $list->getNext();
if (! ($token->value === '(' && ($nextKeyword && $nextKeyword->value === 'SELECT'))) {
$parser->error('Subquery of the CTE was expected.', $token);
$list->idx = $idxBeforeGetNext;
break;
}
// Restore the index
$list->idx = $idxBeforeGetNext;
++$list->idx;
$subList = $this->getSubTokenList($list);
if ($subList instanceof ParserException) {
$parser->errors[] = $subList;
break;
}
$subParser = new Parser($subList);
if ($subParser->errors !== []) {
foreach ($subParser->errors as $error) {
$parser->errors[] = $error;
}
break;
}
$this->withers[$wither]->statement = $subParser;
$state = 4;
} elseif ($state === 4) {
if ($token->value === ',') {
// There's another WITH expression to parse, go back to state=0
$state = 0;
continue;
}
if (
$token->type === TokenType::Keyword && (
$token->value === 'SELECT'
|| $token->value === 'INSERT'
|| $token->value === 'UPDATE'
|| $token->value === 'DELETE'
)
) {
$state = 5;
--$list->idx;
continue;
}
$parser->error('An expression was expected.', $token);
break;
} elseif ($state === 5) {
/**
* We need to parse all of the remaining tokens becuase mostly, they are only the CTE expression
* which's mostly is SELECT, or INSERT, UPDATE, or delete statement.
* e.g: INSERT .. ( SELECT 1 ) SELECT col1 FROM cte ON DUPLICATE KEY UPDATE col_name = 3.
* The issue is that, `ON DUPLICATE KEY UPDATE col_name = 3` is related to the main INSERT query
* not the cte expression (SELECT col1 FROM cte) we need to determine the end of the expression
* to parse `ON DUPLICATE KEY UPDATE` from the InsertStatement parser instead.
*/
// Index of the last parsed token by default would be the last token in the $list, because we're
// assuming that all remaining tokens at state 4, are related to the expression.
$idxOfLastParsedToken = $list->count - 1;
// Index before search to be able to restore the index.
$idxBeforeSearch = $list->idx;
// Length of expression tokens is null by default, in order for the $subList to start
// from $list->idx to the end of the $list.
$lengthOfExpressionTokens = null;
if ($list->getNextOfTypeAndValue(TokenType::Keyword, 'ON')) {
// (-1) because getNextOfTypeAndValue returned ON and increased the index.
$idxOfOn = $list->idx - 1;
// We want to make sure that it's `ON DUPLICATE KEY UPDATE`
$dubplicateToken = $list->getNext();
$keyToken = $list->getNext();
$updateToken = $list->getNext();
if (
$dubplicateToken && $dubplicateToken->keyword === 'DUPLICATE'
&& ($keyToken && $keyToken->keyword === 'KEY')
&& ($updateToken && $updateToken->keyword === 'UPDATE')
) {
// Index of the last parsed token will be the token before the ON Keyword
$idxOfLastParsedToken = $idxOfOn - 1;
// The length of the expression tokens would be the difference
// between the first unrelated token `ON` and the idx
// before skipping the CTE tokens.
$lengthOfExpressionTokens = $idxOfOn - $idxBeforeSearch;
}
}
// Restore the index
$list->idx = $idxBeforeSearch;
$subList = new TokensList(array_slice($list->tokens, $list->idx, $lengthOfExpressionTokens));
$subParser = new Parser($subList);
if ($subParser->errors !== []) {
foreach ($subParser->errors as $error) {
$parser->errors[] = $error;
}
break;
}
$this->cteStatementParser = $subParser;
$list->idx = $idxOfLastParsedToken;
break;
}
}
// 5 is the only valid end state
if ($state !== 5) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
$parser->error('Unexpected end of the WITH CTE.', $token);
}
--$list->idx;
}
public function build(): string
{
$str = 'WITH ';
if ($this->options !== null && $this->options->options !== []) {
$str .= $this->options->build() . ' ';
}
if ($this->withers !== []) {
$str .= implode(', ', $this->withers) . ' ';
}
if ($this->cteStatementParser) {
$str .= implode('', $this->cteStatementParser->statements);
}
return $str;
}
/**
* Get tokens within the WITH expression to use them in another parser
*/
private function getSubTokenList(TokensList $list): ParserException|TokensList
{
$idx = $list->idx;
$token = $list->tokens[$list->idx];
$openParenthesis = 0;
while ($list->idx < $list->count) {
if ($token->value === '(') {
++$openParenthesis;
} elseif ($token->value === ')') {
if (--$openParenthesis === -1) {
break;
}
}
++$list->idx;
if (! isset($list->tokens[$list->idx])) {
break;
}
$token = $list->tokens[$list->idx];
}
// performance improvement: return the error to avoid a try/catch in the loop
if ($list->idx === $list->count) {
--$list->idx;
return new ParserException(
Translator::gettext('A closing bracket was expected.'),
$token,
);
}
$length = $list->idx - $idx;
return new TokensList(array_slice($list->tokens, $idx, $length));
}
}