1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
|
9
|
|
|
use function array_splice; |
10
|
|
|
use function count; |
11
|
|
|
use function end; |
12
|
|
|
use function in_array; |
13
|
|
|
use function mb_substr; |
14
|
|
|
use function reset; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SqlToken represents SQL tokens produced by {@see SqlTokenizer} or its child classes. |
18
|
|
|
* |
19
|
|
|
* @property SqlToken[] $children Child tokens. |
20
|
|
|
* @property bool $hasChildren Whether the token has children. This property is read-only. |
21
|
|
|
* @property bool $isCollection Whether the token represents a collection of tokens. This property is |
22
|
|
|
* read-only. |
23
|
|
|
* @property string $sql SQL code. This property is read-only. |
24
|
|
|
*/ |
25
|
|
|
final class SqlToken implements ArrayAccess |
26
|
|
|
{ |
27
|
|
|
public const TYPE_CODE = 0; |
28
|
|
|
public const TYPE_STATEMENT = 1; |
29
|
|
|
public const TYPE_TOKEN = 2; |
30
|
|
|
public const TYPE_PARENTHESIS = 3; |
31
|
|
|
public const TYPE_KEYWORD = 4; |
32
|
|
|
public const TYPE_OPERATOR = 5; |
33
|
|
|
public const TYPE_IDENTIFIER = 6; |
34
|
|
|
public const TYPE_STRING_LITERAL = 7; |
35
|
|
|
|
36
|
|
|
private int $type = self::TYPE_TOKEN; |
37
|
|
|
private ?string $content = null; |
38
|
|
|
private ?int $startOffset = null; |
39
|
|
|
private ?int $endOffset = null; |
40
|
|
|
private ?SqlToken $parent = null; |
41
|
|
|
private array $children = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the SQL code representing the token. |
45
|
|
|
* |
46
|
|
|
* @return string SQL code. |
47
|
|
|
*/ |
48
|
2 |
|
public function __toString(): string |
49
|
|
|
{ |
50
|
2 |
|
return $this->getSql(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns whether there is a child token at the specified offset. |
55
|
|
|
* |
56
|
|
|
* This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something |
57
|
|
|
* like `isset($token[$offset])`. |
58
|
|
|
* |
59
|
|
|
* @param int $offset child token offset. |
60
|
|
|
* |
61
|
|
|
* @return bool whether the token exists. |
62
|
|
|
*/ |
63
|
17 |
|
public function offsetExists($offset): bool |
64
|
|
|
{ |
65
|
17 |
|
return isset($this->children[$this->calculateOffset($offset)]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns a child token at the specified offset. |
70
|
|
|
* |
71
|
|
|
* This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something |
72
|
|
|
* like `$child = $token[$offset];`. |
73
|
|
|
* |
74
|
|
|
* @param int $offset child token offset. |
75
|
|
|
* |
76
|
|
|
* @return SqlToken|null the child token at the specified offset, `null` if there's no token. |
77
|
|
|
*/ |
78
|
22 |
|
public function offsetGet($offset): ?self |
79
|
|
|
{ |
80
|
22 |
|
$offset = $this->calculateOffset($offset); |
81
|
|
|
|
82
|
22 |
|
return $this->children[$offset] ?? null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds a child token to the token. |
87
|
|
|
* |
88
|
|
|
* This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something |
89
|
|
|
* like `$token[$offset] = $child;`. |
90
|
|
|
* |
91
|
|
|
* @param int|null $offset child token offset. |
92
|
|
|
* @param SqlToken $token token to be added. |
93
|
|
|
*/ |
94
|
22 |
|
public function offsetSet($offset, $token): void |
95
|
|
|
{ |
96
|
22 |
|
$token->parent = $this; |
97
|
|
|
|
98
|
22 |
|
if ($offset === null) { |
99
|
22 |
|
$this->children[] = $token; |
100
|
|
|
} else { |
101
|
|
|
$this->children[$this->calculateOffset($offset)] = $token; |
102
|
|
|
} |
103
|
|
|
|
104
|
22 |
|
$this->updateCollectionOffsets(); |
105
|
22 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Removes a child token at the specified offset. |
109
|
|
|
* |
110
|
|
|
* This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something |
111
|
|
|
* like `unset($token[$offset])`. |
112
|
|
|
* |
113
|
|
|
* @param int $offset child token offset. |
114
|
|
|
*/ |
115
|
5 |
|
public function offsetUnset($offset): void |
116
|
|
|
{ |
117
|
5 |
|
$offset = $this->calculateOffset($offset); |
118
|
|
|
|
119
|
5 |
|
if (isset($this->children[$offset])) { |
120
|
5 |
|
array_splice($this->children, $offset, 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
$this->updateCollectionOffsets(); |
124
|
5 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns child tokens. |
128
|
|
|
* |
129
|
|
|
* @return SqlToken[] child tokens. |
130
|
|
|
*/ |
131
|
7 |
|
public function getChildren(): array |
132
|
|
|
{ |
133
|
7 |
|
return $this->children; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets a list of child tokens. |
138
|
|
|
* |
139
|
|
|
* @param SqlToken[] $children child tokens. |
140
|
|
|
*/ |
141
|
|
|
public function setChildren(array $children): void |
142
|
|
|
{ |
143
|
|
|
$this->children = []; |
144
|
|
|
|
145
|
|
|
foreach ($children as $child) { |
146
|
|
|
$child->parent = $this; |
147
|
|
|
$this->children[] = $child; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->updateCollectionOffsets(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns whether the token represents a collection of tokens. |
155
|
|
|
* |
156
|
|
|
* @return bool whether the token represents a collection of tokens. |
157
|
|
|
*/ |
158
|
22 |
|
public function getIsCollection(): bool |
159
|
|
|
{ |
160
|
22 |
|
return in_array($this->type, [ |
161
|
22 |
|
self::TYPE_CODE, |
162
|
22 |
|
self::TYPE_STATEMENT, |
163
|
22 |
|
self::TYPE_PARENTHESIS, |
164
|
22 |
|
], true); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns whether the token represents a collection of tokens and has non-zero number of children. |
169
|
|
|
* |
170
|
|
|
* @return bool whether the token has children. |
171
|
|
|
*/ |
172
|
22 |
|
public function getHasChildren(): bool |
173
|
|
|
{ |
174
|
22 |
|
return $this->getIsCollection() && !empty($this->children); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the SQL code representing the token. |
179
|
|
|
* |
180
|
|
|
* @return string SQL code. |
181
|
|
|
*/ |
182
|
13 |
|
public function getSql(): string |
183
|
|
|
{ |
184
|
13 |
|
$code = $this; |
185
|
|
|
|
186
|
13 |
|
while ($code->parent !== null) { |
187
|
13 |
|
$code = $code->parent; |
188
|
|
|
} |
189
|
|
|
|
190
|
13 |
|
return mb_substr($code->content, $this->startOffset, $this->endOffset - $this->startOffset, 'UTF-8'); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns whether this token (including its children) matches the specified "pattern" SQL code. |
195
|
|
|
* |
196
|
|
|
* Usage Example: |
197
|
|
|
* |
198
|
|
|
* ```php |
199
|
|
|
* $patternToken = (new \Yiisoft\Db\Sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize(); |
200
|
|
|
* if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) { |
201
|
|
|
* // ... |
202
|
|
|
* } |
203
|
|
|
* ``` |
204
|
|
|
* |
205
|
|
|
* @param SqlToken $patternToken tokenized SQL code to match against. In addition to normal SQL, the `any` keyword |
206
|
|
|
* is supported which will match any number of keywords, identifiers, whitespaces. |
207
|
|
|
* @param int $offset token children offset to start lookup with. |
208
|
|
|
* @param int|null $firstMatchIndex token children offset where a successful match begins. |
209
|
|
|
* @param int|null $lastMatchIndex token children offset where a successful match ends. |
210
|
|
|
* |
211
|
|
|
* @return bool whether this token matches the pattern SQL code. |
212
|
|
|
*/ |
213
|
17 |
|
public function matches( |
214
|
|
|
self $patternToken, |
215
|
|
|
int $offset = 0, |
216
|
|
|
?int &$firstMatchIndex = null, |
217
|
|
|
?int &$lastMatchIndex = null |
218
|
|
|
): bool { |
219
|
17 |
|
if (!$patternToken->getHasChildren()) { |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
17 |
|
$patternToken = $patternToken[0]; |
224
|
|
|
|
225
|
17 |
|
return $this->tokensMatch($patternToken, $this, $offset, $firstMatchIndex, $lastMatchIndex); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Tests the given token to match the specified pattern token. |
230
|
|
|
* |
231
|
|
|
* @param SqlToken $patternToken |
232
|
|
|
* @param SqlToken $token |
233
|
|
|
* @param int $offset |
234
|
|
|
* @param int|null $firstMatchIndex |
235
|
|
|
* @param int|null $lastMatchIndex |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
17 |
|
private function tokensMatch( |
240
|
|
|
self $patternToken, |
241
|
|
|
self $token, |
242
|
|
|
int $offset = 0, |
243
|
|
|
?int &$firstMatchIndex = null, |
244
|
|
|
?int &$lastMatchIndex = null |
245
|
|
|
): bool { |
246
|
17 |
|
if ($patternToken->getIsCollection() !== $token->getIsCollection() || (!$patternToken->getIsCollection() && $patternToken->content !== $token->content)) { |
247
|
17 |
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
17 |
|
if ($patternToken->children === $token->children) { |
251
|
17 |
|
$firstMatchIndex = $lastMatchIndex = $offset; |
252
|
|
|
|
253
|
17 |
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
17 |
|
$firstMatchIndex = $lastMatchIndex = null; |
257
|
17 |
|
$wildcard = false; |
258
|
|
|
|
259
|
17 |
|
for ($index = 0, $count = count($patternToken->children); $index < $count; $index++) { |
260
|
|
|
/** |
261
|
|
|
* Here we iterate token by token with an exception of "any" that toggles an iteration until we matched |
262
|
|
|
* with a next pattern token or EOF. |
263
|
|
|
*/ |
264
|
17 |
|
if ($patternToken[$index]->content === 'any') { |
265
|
17 |
|
$wildcard = true; |
266
|
17 |
|
continue; |
267
|
|
|
} |
268
|
|
|
|
269
|
17 |
|
for ($limit = $wildcard ? count($token->children) : $offset + 1; $offset < $limit; $offset++) { |
270
|
17 |
|
if (!$wildcard && !isset($token[$offset])) { |
271
|
|
|
break; |
272
|
|
|
} |
273
|
|
|
|
274
|
17 |
|
if (!$this->tokensMatch($patternToken[$index], $token[$offset])) { |
|
|
|
|
275
|
17 |
|
continue; |
276
|
|
|
} |
277
|
|
|
|
278
|
17 |
|
if ($firstMatchIndex === null) { |
279
|
17 |
|
$firstMatchIndex = $offset; |
280
|
17 |
|
$lastMatchIndex = $offset; |
281
|
|
|
} else { |
282
|
17 |
|
$lastMatchIndex = $offset; |
283
|
|
|
} |
284
|
|
|
|
285
|
17 |
|
$wildcard = false; |
286
|
17 |
|
$offset++; |
287
|
|
|
|
288
|
17 |
|
continue 2; |
289
|
|
|
} |
290
|
|
|
|
291
|
12 |
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
17 |
|
return true; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Returns an absolute offset in the children array. |
299
|
|
|
* |
300
|
|
|
* @param int $offset |
301
|
|
|
* |
302
|
|
|
* @return int |
303
|
|
|
*/ |
304
|
22 |
|
private function calculateOffset(int $offset): int |
305
|
|
|
{ |
306
|
22 |
|
if ($offset >= 0) { |
307
|
22 |
|
return $offset; |
308
|
|
|
} |
309
|
|
|
|
310
|
22 |
|
return count($this->children) + $offset; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Updates token SQL code start and end offsets based on its children. |
315
|
|
|
*/ |
316
|
22 |
|
private function updateCollectionOffsets(): void |
317
|
|
|
{ |
318
|
22 |
|
if (!empty($this->children)) { |
319
|
22 |
|
$this->startOffset = reset($this->children)->startOffset; |
320
|
22 |
|
$this->endOffset = end($this->children)->endOffset; |
321
|
|
|
} |
322
|
|
|
|
323
|
22 |
|
if ($this->parent !== null) { |
324
|
22 |
|
$this->parent->updateCollectionOffsets(); |
325
|
|
|
} |
326
|
22 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Set token type. It has to be one of the following constants: |
330
|
|
|
* |
331
|
|
|
* - {@see TYPE_CODE} |
332
|
|
|
* - {@see TYPE_STATEMENT} |
333
|
|
|
* - {@see TYPE_TOKEN} |
334
|
|
|
* - {@see TYPE_PARENTHESIS} |
335
|
|
|
* - {@see TYPE_KEYWORD} |
336
|
|
|
* - {@see TYPE_OPERATOR} |
337
|
|
|
* - {@see TYPE_IDENTIFIER} |
338
|
|
|
* - {@see TYPE_STRING_LITERAL} |
339
|
|
|
* |
340
|
|
|
* @param int $value token type. It has to be one of the following constants: |
341
|
|
|
* |
342
|
|
|
* @return self |
343
|
|
|
*/ |
344
|
22 |
|
public function type(int $value): self |
345
|
|
|
{ |
346
|
22 |
|
$this->type = $value; |
347
|
|
|
|
348
|
22 |
|
return $this; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Set token content. |
353
|
|
|
* |
354
|
|
|
* @param string|null $value |
355
|
|
|
* |
356
|
|
|
* @return self |
357
|
|
|
*/ |
358
|
22 |
|
public function content(?string $value): self |
359
|
|
|
{ |
360
|
22 |
|
$this->content = $value; |
361
|
|
|
|
362
|
22 |
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Set original SQL token start position. |
367
|
|
|
* |
368
|
|
|
* @param int $value original SQL token start position. |
369
|
|
|
* |
370
|
|
|
* @return self |
371
|
|
|
*/ |
372
|
22 |
|
public function startOffset(int $value): self |
373
|
|
|
{ |
374
|
22 |
|
$this->startOffset = $value; |
375
|
|
|
|
376
|
22 |
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Set original SQL token end position. |
381
|
|
|
* |
382
|
|
|
* @param int $value original SQL token end position. |
383
|
|
|
* |
384
|
|
|
* @return self |
385
|
|
|
*/ |
386
|
22 |
|
public function endOffset(int $value): self |
387
|
|
|
{ |
388
|
22 |
|
$this->endOffset = $value; |
389
|
|
|
|
390
|
22 |
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Set parent token. |
395
|
|
|
* |
396
|
|
|
* @param SqlToken $value parent token. |
397
|
|
|
* |
398
|
|
|
* @return self |
399
|
|
|
*/ |
400
|
|
|
public function parent(self $value): self |
401
|
|
|
{ |
402
|
|
|
$this->parent = $value; |
403
|
|
|
|
404
|
|
|
return $this; |
405
|
|
|
} |
406
|
|
|
|
407
|
2 |
|
public function getContent(): ?string |
408
|
|
|
{ |
409
|
2 |
|
return $this->content; |
410
|
|
|
} |
411
|
|
|
|
412
|
2 |
|
public function getType(): int |
413
|
|
|
{ |
414
|
2 |
|
return $this->type; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|