|
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 testParseKeyExpressionWithoutOptions(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$component = Key::parse( |
|
148
|
|
|
new Parser(), |
|
149
|
|
|
$this->getTokensList( |
|
150
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))),' |
|
151
|
|
|
) |
|
152
|
|
|
); |
|
153
|
|
|
$this->assertEquals('KEY', $component->type); |
|
154
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
|
155
|
|
|
$this->assertEquals(new OptionsArray(), $component->options); |
|
156
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
|
157
|
|
|
$expr->function = 'convert_tz'; |
|
158
|
|
|
$this->assertEquals($expr, $component->expr); |
|
159
|
|
|
$this->assertSame([], $component->columns); |
|
160
|
|
|
$this->assertSame( |
|
161
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))) ', |
|
162
|
|
|
Key::build($component) |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testParseKeyExpressionWithOptions(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$component = Key::parse( |
|
169
|
|
|
new Parser(), |
|
170
|
|
|
$this->getTokensList( |
|
171
|
|
|
'KEY `updated_tz_ind2`' |
|
172
|
|
|
. ' ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
|
173
|
|
|
. ' COMMENT \'my comment\',' |
|
174
|
|
|
) |
|
175
|
|
|
); |
|
176
|
|
|
$this->assertEquals('KEY', $component->type); |
|
177
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
|
178
|
|
|
$this->assertEquals(new OptionsArray( |
|
179
|
|
|
[ |
|
180
|
|
|
4 => [ |
|
181
|
|
|
'name' => 'COMMENT', |
|
182
|
|
|
'equals' => false, |
|
183
|
|
|
'expr' => '\'my comment\'', |
|
184
|
|
|
'value' => 'my comment', |
|
185
|
|
|
], |
|
186
|
|
|
] |
|
187
|
|
|
), $component->options); |
|
188
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
|
189
|
|
|
$expr->function = 'convert_tz'; |
|
190
|
|
|
$this->assertEquals($expr, $component->expr); |
|
191
|
|
|
$this->assertSame([], $component->columns); |
|
192
|
|
|
$this->assertSame( |
|
193
|
|
|
'KEY `updated_tz_ind2`' |
|
194
|
|
|
. ' ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
|
195
|
|
|
. ' COMMENT \'my comment\'', |
|
196
|
|
|
Key::build($component) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testParseKeyExpressionWithOptionsError(): void |
|
201
|
|
|
{ |
|
202
|
|
|
$parser = new Parser(); |
|
203
|
|
|
$component = Key::parse( |
|
204
|
|
|
$parser, |
|
205
|
|
|
$this->getTokensList( |
|
206
|
|
|
'KEY `updated_tz_ind2` (()convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')))' |
|
207
|
|
|
. ' COMMENT \'my comment\',' |
|
208
|
|
|
) |
|
209
|
|
|
); |
|
210
|
|
|
$this->assertEquals('KEY', $component->type); |
|
211
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
|
212
|
|
|
$this->assertEquals(new OptionsArray( |
|
213
|
|
|
[] |
|
214
|
|
|
), $component->options); |
|
215
|
|
|
$t = new Token('convert_tz', Token::TYPE_KEYWORD, 33); |
|
216
|
|
|
$t->position = 25; |
|
217
|
|
|
|
|
218
|
|
|
$this->assertEquals([ |
|
219
|
|
|
new ParserException( |
|
220
|
|
|
'Unexpected token.', |
|
221
|
|
|
$t |
|
222
|
|
|
), |
|
223
|
|
|
], $parser->errors); |
|
224
|
|
|
$expr = new Expression('(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\'))'); |
|
225
|
|
|
$expr->function = 'convert_tz'; |
|
226
|
|
|
$this->assertEquals('()(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')', $component->expr); |
|
227
|
|
|
$this->assertSame([], $component->columns); |
|
228
|
|
|
$this->assertSame( |
|
229
|
|
|
'KEY `updated_tz_ind2` (()(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')) ', |
|
230
|
|
|
Key::build($component) |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function testParseKeyOneExpressionWithOptions(): void |
|
235
|
|
|
{ |
|
236
|
|
|
$parser = new Parser(); |
|
237
|
|
|
$component = Key::parse( |
|
238
|
|
|
$parser, |
|
239
|
|
|
$this->getTokensList( |
|
240
|
|
|
'KEY `updated_tz_ind2`' |
|
241
|
|
|
. ' (' |
|
242
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')), ' |
|
243
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
|
244
|
|
|
. ')' |
|
245
|
|
|
. ' COMMENT \'my comment\',' |
|
246
|
|
|
) |
|
247
|
|
|
); |
|
248
|
|
|
$this->assertEquals('KEY', $component->type); |
|
249
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
|
250
|
|
|
$this->assertEquals(new OptionsArray( |
|
251
|
|
|
[ |
|
252
|
|
|
4 => [ |
|
253
|
|
|
'name' => 'COMMENT', |
|
254
|
|
|
'equals' => false, |
|
255
|
|
|
'expr' => '\'my comment\'', |
|
256
|
|
|
'value' => 'my comment', |
|
257
|
|
|
], |
|
258
|
|
|
] |
|
259
|
|
|
), $component->options); |
|
260
|
|
|
$this->assertSame([], $parser->errors); |
|
261
|
|
|
$expr = new Expression( |
|
262
|
|
|
'(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
|
263
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
|
264
|
|
|
); |
|
265
|
|
|
$expr->function = 'convert_tz'; |
|
266
|
|
|
$this->assertEquals($expr, $component->expr); |
|
267
|
|
|
$this->assertSame([], $component->columns); |
|
268
|
|
|
$this->assertSame( |
|
269
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
|
270
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\'))' |
|
271
|
|
|
. ') COMMENT \'my comment\'', |
|
272
|
|
|
Key::build($component) |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function testParseKeyMultipleExpressionsWithOptions(): void |
|
277
|
|
|
{ |
|
278
|
|
|
$parser = new Parser(); |
|
279
|
|
|
$component = Key::parse( |
|
280
|
|
|
$parser, |
|
281
|
|
|
$this->getTokensList( |
|
282
|
|
|
'KEY `updated_tz_ind2`' |
|
283
|
|
|
. ' (' |
|
284
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')), ' |
|
285
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')), ' |
|
286
|
|
|
. '(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
|
287
|
|
|
. ')' |
|
288
|
|
|
. ' COMMENT \'my comment\',' |
|
289
|
|
|
) |
|
290
|
|
|
); |
|
291
|
|
|
$this->assertEquals('KEY', $component->type); |
|
292
|
|
|
$this->assertEquals('updated_tz_ind2', $component->name); |
|
293
|
|
|
$this->assertEquals(new OptionsArray( |
|
294
|
|
|
[ |
|
295
|
|
|
4 => [ |
|
296
|
|
|
'name' => 'COMMENT', |
|
297
|
|
|
'equals' => false, |
|
298
|
|
|
'expr' => '\'my comment\'', |
|
299
|
|
|
'value' => 'my comment', |
|
300
|
|
|
], |
|
301
|
|
|
] |
|
302
|
|
|
), $component->options); |
|
303
|
|
|
$expr = new Expression( |
|
304
|
|
|
'(convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
|
305
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')),' |
|
306
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
|
307
|
|
|
); |
|
308
|
|
|
$expr->function = 'convert_tz'; |
|
309
|
|
|
$this->assertEquals($expr, $component->expr); |
|
310
|
|
|
$this->assertSame([], $component->columns); |
|
311
|
|
|
$this->assertSame([], $parser->errors); |
|
312
|
|
|
$this->assertSame( |
|
313
|
|
|
'KEY `updated_tz_ind2` ((convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'GB\')),' |
|
314
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'FR\')),' |
|
315
|
|
|
. ' (convert_tz(`cache_updated`,_utf8mb4\'GMT\',_utf8mb4\'RU\'))' |
|
316
|
|
|
. ') COMMENT \'my comment\'', |
|
317
|
|
|
Key::build($component) |
|
318
|
|
|
); |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|