1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
8
|
|
|
use PhpMyAdmin\SqlParser\Components\Key; |
9
|
|
|
use PhpMyAdmin\SqlParser\Components\OptionsArray; |
10
|
|
|
use PhpMyAdmin\SqlParser\Exceptions\ParserException; |
11
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
12
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
13
|
|
|
use PhpMyAdmin\SqlParser\Token; |
14
|
|
|
|
15
|
|
|
class KeyTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testParse(): void |
18
|
|
|
{ |
19
|
|
|
$component = Key::parse( |
20
|
|
|
new Parser(), |
21
|
|
|
$this->getTokensList('') |
22
|
|
|
); |
23
|
|
|
$this->assertNull($component->type); |
24
|
|
|
$this->assertNull($component->options); |
25
|
|
|
$this->assertNull($component->name); |
26
|
|
|
$this->assertNull($component->expr); |
27
|
|
|
$this->assertSame([], $component->columns); |
28
|
|
|
$this->assertSame( |
29
|
|
|
'()', |
30
|
|
|
Key::build($component) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testParseKeyWithoutOptions(): void |
35
|
|
|
{ |
36
|
|
|
$component = Key::parse( |
37
|
|
|
new Parser(), |
38
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type`),') |
39
|
|
|
); |
40
|
|
|
$this->assertEquals('KEY', $component->type); |
41
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
42
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
43
|
|
|
$this->assertNull($component->expr); |
44
|
|
|
$this->assertSame([['name' => 'alias_type']], $component->columns); |
45
|
|
|
$this->assertSame( |
46
|
|
|
'KEY `alias_type_idx` (`alias_type`)', |
47
|
|
|
Key::build($component) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testParseKeyWithLengthWithoutOptions(): void |
52
|
|
|
{ |
53
|
|
|
$component = Key::parse( |
54
|
|
|
new Parser(), |
55
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type`(10)),') |
56
|
|
|
); |
57
|
|
|
$this->assertEquals('KEY', $component->type); |
58
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
59
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
60
|
|
|
$this->assertNull($component->expr); |
61
|
|
|
$this->assertSame([['name' => 'alias_type', 'length' => 10]], $component->columns); |
62
|
|
|
$this->assertSame( |
63
|
|
|
'KEY `alias_type_idx` (`alias_type`(10))', |
64
|
|
|
Key::build($component) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testParseKeyWithLengthWithoutOptionsWithOrder(): void |
69
|
|
|
{ |
70
|
|
|
$component = Key::parse( |
71
|
|
|
new Parser(), |
72
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type`(10) ASC),') |
73
|
|
|
); |
74
|
|
|
$this->assertEquals('KEY', $component->type); |
75
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
76
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
77
|
|
|
$this->assertNull($component->expr); |
78
|
|
|
$this->assertSame([['name' => 'alias_type', 'length' => 10, 'order' => 'ASC']], $component->columns); |
79
|
|
|
$this->assertSame( |
80
|
|
|
'KEY `alias_type_idx` (`alias_type`(10) ASC)', |
81
|
|
|
Key::build($component) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testParseKeyWithoutOptionsWithOrderLowercase(): void |
86
|
|
|
{ |
87
|
|
|
$component = Key::parse( |
88
|
|
|
new Parser(), |
89
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type` desc),') |
90
|
|
|
); |
91
|
|
|
$this->assertEquals('KEY', $component->type); |
92
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
93
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
94
|
|
|
$this->assertNull($component->expr); |
95
|
|
|
$this->assertSame([['name' => 'alias_type', 'order' => 'DESC']], $component->columns); |
96
|
|
|
$this->assertSame( |
97
|
|
|
'KEY `alias_type_idx` (`alias_type` DESC)', |
98
|
|
|
Key::build($component) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testParseKeyWithoutOptionsWithOrder(): void |
103
|
|
|
{ |
104
|
|
|
$component = Key::parse( |
105
|
|
|
new Parser(), |
106
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type` DESC),') |
107
|
|
|
); |
108
|
|
|
$this->assertEquals('KEY', $component->type); |
109
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
110
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
111
|
|
|
$this->assertNull($component->expr); |
112
|
|
|
$this->assertSame([['name' => 'alias_type', 'order' => 'DESC']], $component->columns); |
113
|
|
|
$this->assertSame( |
114
|
|
|
'KEY `alias_type_idx` (`alias_type` DESC)', |
115
|
|
|
Key::build($component) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testParseKeyWithLengthWithOptions(): void |
120
|
|
|
{ |
121
|
|
|
$component = Key::parse( |
122
|
|
|
new Parser(), |
123
|
|
|
$this->getTokensList('KEY `alias_type_idx` (`alias_type`(10)) COMMENT \'my comment\',') |
124
|
|
|
); |
125
|
|
|
$this->assertEquals('KEY', $component->type); |
126
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
127
|
|
|
$this->assertEquals(new OptionsArray( |
128
|
|
|
[ |
129
|
|
|
4 => [ |
130
|
|
|
'name' => 'COMMENT', |
131
|
|
|
'equals' => false, |
132
|
|
|
'expr' => '\'my comment\'', |
133
|
|
|
'value' => 'my comment', |
134
|
|
|
], |
135
|
|
|
] |
136
|
|
|
), $component->options); |
137
|
|
|
$this->assertNull($component->expr); |
138
|
|
|
$this->assertSame([['name' => 'alias_type', 'length' => 10]], $component->columns); |
139
|
|
|
$this->assertSame( |
140
|
|
|
'KEY `alias_type_idx` (`alias_type`(10)) COMMENT \'my comment\'', |
141
|
|
|
Key::build($component) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testParseKeyWithLengthWithAllOptions(): void |
146
|
|
|
{ |
147
|
|
|
$component = Key::parse( |
148
|
|
|
new Parser(), |
149
|
|
|
$this->getTokensList( |
150
|
|
|
// This is not a vary plausible example but it runs |
151
|
|
|
// Only ENGINE_ATTRIBUTE gives a not supported error but is still a valid syntax |
152
|
|
|
'KEY `alias_type_idx` (`alias_type`(10))' |
153
|
|
|
. ' COMMENT \'my comment\' VISIBLE KEY_BLOCK_SIZE=1' |
154
|
|
|
. ' INVISIBLE ENGINE_ATTRIBUTE \'foo\' SECONDARY_ENGINE_ATTRIBUTE=\'bar\' USING BTREE,' |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
$this->assertEquals('KEY', $component->type); |
158
|
|
|
$this->assertEquals('alias_type_idx', $component->name); |
159
|
|
|
$this->assertEquals(new OptionsArray( |
160
|
|
|
[ |
161
|
|
|
1 => [ |
162
|
|
|
'name' => 'KEY_BLOCK_SIZE', |
163
|
|
|
'equals' => true, |
164
|
|
|
'expr' => '1', |
165
|
|
|
'value' => '1', |
166
|
|
|
], |
167
|
|
|
2 => [ |
168
|
|
|
'name' => 'USING', |
169
|
|
|
'equals' => false, |
170
|
|
|
'expr' => 'BTREE', |
171
|
|
|
'value' => 'BTREE', |
172
|
|
|
], |
173
|
|
|
4 => [ |
174
|
|
|
'name' => 'COMMENT', |
175
|
|
|
'equals' => false, |
176
|
|
|
'expr' => '\'my comment\'', |
177
|
|
|
'value' => 'my comment', |
178
|
|
|
], |
179
|
|
|
5 => [ |
180
|
|
|
'name' => 'ENGINE_ATTRIBUTE', |
181
|
|
|
'equals' => true, |
182
|
|
|
'expr' => '\'foo\'', |
183
|
|
|
'value' => 'foo', |
184
|
|
|
], |
185
|
|
|
6 => 'VISIBLE', |
186
|
|
|
12 => 'INVISIBLE', |
187
|
|
|
13 => [ |
188
|
|
|
'name' => 'SECONDARY_ENGINE_ATTRIBUTE', |
189
|
|
|
'equals' => true, |
190
|
|
|
'expr' => '\'bar\'', |
191
|
|
|
'value' => 'bar', |
192
|
|
|
], |
193
|
|
|
] |
194
|
|
|
), $component->options); |
195
|
|
|
$this->assertNull($component->expr); |
196
|
|
|
$this->assertSame([['name' => 'alias_type', 'length' => 10]], $component->columns); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testParseKeyExpressionWithoutOptions(): void |
200
|
|
|
{ |
201
|
|
|
$component = Key::parse( |
202
|
|
|
new Parser(), |
203
|
|
|
$this->getTokensList( |
204
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))),' |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
$this->assertEquals('KEY', $component->type); |
208
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
209
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
210
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
211
|
|
|
$expr->function = 'convert_tz'; |
212
|
|
|
$this->assertEquals($expr, $component->expr); |
213
|
|
|
$this->assertSame([], $component->columns); |
214
|
|
|
$this->assertSame( |
215
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))) ', |
216
|
|
|
Key::build($component) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testParseKeyExpressionWithOptions(): void |
221
|
|
|
{ |
222
|
|
|
$component = Key::parse( |
223
|
|
|
new Parser(), |
224
|
|
|
$this->getTokensList( |
225
|
|
|
'KEY `updated_tz_ind2`' |
226
|
|
|
. ' ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
227
|
|
|
. ' COMMENT \'my comment\',' |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
$this->assertEquals('KEY', $component->type); |
231
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
232
|
|
|
$this->assertEquals(new OptionsArray( |
233
|
|
|
[ |
234
|
|
|
4 => [ |
235
|
|
|
'name' => 'COMMENT', |
236
|
|
|
'equals' => false, |
237
|
|
|
'expr' => '\'my comment\'', |
238
|
|
|
'value' => 'my comment', |
239
|
|
|
], |
240
|
|
|
] |
241
|
|
|
), $component->options); |
242
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
243
|
|
|
$expr->function = 'convert_tz'; |
244
|
|
|
$this->assertEquals($expr, $component->expr); |
245
|
|
|
$this->assertSame([], $component->columns); |
246
|
|
|
$this->assertSame( |
247
|
|
|
'KEY `updated_tz_ind2`' |
248
|
|
|
. ' ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
249
|
|
|
. ' COMMENT \'my comment\'', |
250
|
|
|
Key::build($component) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testParseKeyExpressionWithOptionsError(): void |
255
|
|
|
{ |
256
|
|
|
$parser = new Parser(); |
257
|
|
|
$component = Key::parse( |
258
|
|
|
$parser, |
259
|
|
|
$this->getTokensList( |
260
|
|
|
'KEY `updated_tz_ind2` (()convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
261
|
|
|
. ' COMMENT \'my comment\',' |
262
|
|
|
) |
263
|
|
|
); |
264
|
|
|
$this->assertEquals('KEY', $component->type); |
265
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
266
|
|
|
$this->assertEquals(new OptionsArray( |
267
|
|
|
[] |
268
|
|
|
), $component->options); |
269
|
|
|
$t = new Token('convert_tz', Token::TYPE_KEYWORD, 33); |
270
|
|
|
$t->position = 25; |
271
|
|
|
|
272
|
|
|
$this->assertEquals([ |
273
|
|
|
new ParserException( |
274
|
|
|
'Unexpected token.', |
275
|
|
|
$t |
276
|
|
|
), |
277
|
|
|
], $parser->errors); |
278
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
279
|
|
|
$expr->function = 'convert_tz'; |
280
|
|
|
$this->assertEquals('()(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')', $component->expr); |
281
|
|
|
$this->assertSame([], $component->columns); |
282
|
|
|
$this->assertSame( |
283
|
|
|
'KEY `updated_tz_ind2` (()(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')) ', |
284
|
|
|
Key::build($component) |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testParseKeyOneExpressionWithOptions(): void |
289
|
|
|
{ |
290
|
|
|
$parser = new Parser(); |
291
|
|
|
$component = Key::parse( |
292
|
|
|
$parser, |
293
|
|
|
$this->getTokensList( |
294
|
|
|
'KEY `updated_tz_ind2`' |
295
|
|
|
. ' (' |
296
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')), ' |
297
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
298
|
|
|
. ')' |
299
|
|
|
. ' COMMENT \'my comment\',' |
300
|
|
|
) |
301
|
|
|
); |
302
|
|
|
$this->assertEquals('KEY', $component->type); |
303
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
304
|
|
|
$this->assertEquals(new OptionsArray( |
305
|
|
|
[ |
306
|
|
|
4 => [ |
307
|
|
|
'name' => 'COMMENT', |
308
|
|
|
'equals' => false, |
309
|
|
|
'expr' => '\'my comment\'', |
310
|
|
|
'value' => 'my comment', |
311
|
|
|
], |
312
|
|
|
] |
313
|
|
|
), $component->options); |
314
|
|
|
$this->assertSame([], $parser->errors); |
315
|
|
|
$expr = new Expression( |
316
|
|
|
'(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
317
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
318
|
|
|
); |
319
|
|
|
$expr->function = 'convert_tz'; |
320
|
|
|
$this->assertEquals($expr, $component->expr); |
321
|
|
|
$this->assertSame([], $component->columns); |
322
|
|
|
$this->assertSame( |
323
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
324
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
325
|
|
|
. ') COMMENT \'my comment\'', |
326
|
|
|
Key::build($component) |
327
|
|
|
); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testParseKeyMultipleExpressionsWithOptions(): void |
331
|
|
|
{ |
332
|
|
|
$parser = new Parser(); |
333
|
|
|
$component = Key::parse( |
334
|
|
|
$parser, |
335
|
|
|
$this->getTokensList( |
336
|
|
|
'KEY `updated_tz_ind2`' |
337
|
|
|
. ' (' |
338
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')), ' |
339
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')), ' |
340
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
341
|
|
|
. ')' |
342
|
|
|
. ' COMMENT \'my comment\',' |
343
|
|
|
) |
344
|
|
|
); |
345
|
|
|
$this->assertEquals('KEY', $component->type); |
346
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
347
|
|
|
$this->assertEquals(new OptionsArray( |
348
|
|
|
[ |
349
|
|
|
4 => [ |
350
|
|
|
'name' => 'COMMENT', |
351
|
|
|
'equals' => false, |
352
|
|
|
'expr' => '\'my comment\'', |
353
|
|
|
'value' => 'my comment', |
354
|
|
|
], |
355
|
|
|
] |
356
|
|
|
), $component->options); |
357
|
|
|
$expr = new Expression( |
358
|
|
|
'(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
359
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')),' |
360
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
361
|
|
|
); |
362
|
|
|
$expr->function = 'convert_tz'; |
363
|
|
|
$this->assertEquals($expr, $component->expr); |
364
|
|
|
$this->assertSame([], $component->columns); |
365
|
|
|
$this->assertSame([], $parser->errors); |
366
|
|
|
$this->assertSame( |
367
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
368
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')),' |
369
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
370
|
|
|
. ') COMMENT \'my comment\'', |
371
|
|
|
Key::build($component) |
372
|
|
|
); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|