Skip to content

Commit 716d545

Browse files
committed
Fix keywords not being recognized as table alias
Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent a447cc8 commit 716d545

File tree

8 files changed

+56
-80
lines changed

8 files changed

+56
-80
lines changed

src/Components/AlterOperation.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,7 @@ class AlterOperation extends Component
237237
'ON COMPLETION PRESERVE' => 5,
238238
'ON COMPLETION NOT PRESERVE' => 5,
239239
'RENAME' => 6,
240-
'TO' => [
241-
7,
242-
'expr',
243-
['parseField' => 'table'],
244-
],
240+
'TO' => [7, 'expr', ['parseField' => 'table', 'breakOnAlias' => true]],
245241
'ENABLE' => 8,
246242
'DISABLE' => 8,
247243
'DISABLE ON SLAVE' => 8,

src/Components/Expression.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
273273
}
274274

275275
$isExpr = true;
276-
} elseif ($brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias) {
276+
} elseif (
277+
$brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias
278+
&& ($ret->table === null || $ret->table === '')
279+
) {
277280
/* End of expression */
278281
break;
279282
}

src/Statements/LoadStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public function parse(Parser $parser, TokensList $list)
293293
}
294294

295295
++$list->idx;
296-
$this->table = Expression::parse($parser, $list, ['parseField' => 'table']);
296+
$this->table = Expression::parse($parser, $list, ['parseField' => 'table', 'breakOnAlias' => true]);
297297
$state = 3;
298298
} elseif ($state >= 3 && $state <= 7) {
299299
if ($token->type === Token::TYPE_KEYWORD) {

tests/Utils/ErrorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ public function testGetWithNullToken(): void
3838
{
3939
$lexer = new Lexer('LOCK TABLES table1 AS `t1` LOCAL');
4040
$parser = new Parser($lexer->list);
41-
$this->assertEquals(
42-
[['Unexpected keyword.', 0, 'LOCAL', 27], ['Unexpected end of LOCK expression.', 0, null, null]],
41+
$this->assertSame(
42+
[
43+
['An alias was previously found.', 0, 'LOCAL', 27],
44+
['Unexpected keyword.', 0, 'LOCAL', 27],
45+
['Unexpected end of LOCK expression.', 0, '', null],
46+
],
4347
Error::get([$lexer, $parser])
4448
);
4549
}

tests/data/bugs/gh496.out

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,22 @@
384384
"table": "two",
385385
"column": null,
386386
"expr": "two",
387-
"alias": null,
387+
"alias": "io",
388388
"function": null,
389389
"subquery": null
390390
},
391-
"on": null,
391+
"on": [
392+
{
393+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
394+
"identifiers": [
395+
"io",
396+
"id",
397+
"i"
398+
],
399+
"isOperator": false,
400+
"expr": "io.id = i.id"
401+
}
402+
],
392403
"using": null
393404
}
394405
],
@@ -399,17 +410,7 @@
399410
"options": []
400411
},
401412
"first": 0,
402-
"last": 33,
403-
"table": {
404-
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
405-
"database": "io",
406-
"table": "id",
407-
"column": null,
408-
"expr": "io.id",
409-
"alias": null,
410-
"function": null,
411-
"subquery": null
412-
}
413+
"last": 33
413414
}
414415
],
415416
"brackets": 0,
@@ -418,42 +419,6 @@
418419
},
419420
"errors": {
420421
"lexer": [],
421-
"parser": [
422-
[
423-
"Unrecognized keyword.",
424-
{
425-
"@type": "@23"
426-
},
427-
0
428-
],
429-
[
430-
"Unexpected token.",
431-
{
432-
"@type": "@31"
433-
},
434-
0
435-
],
436-
[
437-
"Unexpected token.",
438-
{
439-
"@type": "@33"
440-
},
441-
0
442-
],
443-
[
444-
"Unexpected token.",
445-
{
446-
"@type": "@34"
447-
},
448-
0
449-
],
450-
[
451-
"Unexpected token.",
452-
{
453-
"@type": "@35"
454-
},
455-
0
456-
]
457-
]
422+
"parser": []
458423
}
459424
}

tests/data/parser/parseLockErr2.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@
162162
"errors": {
163163
"lexer": [],
164164
"parser": [
165+
[
166+
"An alias was previously found.",
167+
{
168+
"@type": "@12"
169+
},
170+
0
171+
],
165172
[
166173
"Unexpected keyword.",
167174
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
begin;
2-
SELECT * FROM `tablename`
2+
SELECT * FROM `tablename`;
33
commit;

tests/data/parser/parseTransaction3.out

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"query": "begin;\nSELECT * FROM `tablename`\ncommit;\n",
2+
"query": "begin;\nSELECT * FROM `tablename`;\ncommit;\n",
33
"lexer": {
44
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5-
"str": "begin;\nSELECT * FROM `tablename`\ncommit;\n",
6-
"len": 41,
7-
"last": 41,
5+
"str": "begin;\nSELECT * FROM `tablename`;\ncommit;\n",
6+
"len": 42,
7+
"last": 42,
88
"list": {
99
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
1010
"tokens": [
@@ -98,14 +98,23 @@
9898
"flags": 2,
9999
"position": 21
100100
},
101+
{
102+
"@type": "PhpMyAdmin\\SqlParser\\Token",
103+
"token": ";",
104+
"value": ";",
105+
"keyword": null,
106+
"type": 9,
107+
"flags": 0,
108+
"position": 32
109+
},
101110
{
102111
"@type": "PhpMyAdmin\\SqlParser\\Token",
103112
"token": "\n",
104113
"value": " ",
105114
"keyword": null,
106115
"type": 3,
107116
"flags": 0,
108-
"position": 32
117+
"position": 33
109118
},
110119
{
111120
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -114,7 +123,7 @@
114123
"keyword": "COMMIT",
115124
"type": 1,
116125
"flags": 1,
117-
"position": 33
126+
"position": 34
118127
},
119128
{
120129
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -123,7 +132,7 @@
123132
"keyword": null,
124133
"type": 9,
125134
"flags": 0,
126-
"position": 39
135+
"position": 40
127136
},
128137
{
129138
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -132,7 +141,7 @@
132141
"keyword": null,
133142
"type": 3,
134143
"flags": 0,
135-
"position": 40
144+
"position": 41
136145
},
137146
{
138147
"@type": "PhpMyAdmin\\SqlParser\\Token",
@@ -144,8 +153,8 @@
144153
"position": null
145154
}
146155
],
147-
"count": 15,
148-
"idx": 15
156+
"count": 16,
157+
"idx": 16
149158
},
150159
"delimiter": ";",
151160
"delimiterLen": 1,
@@ -202,7 +211,7 @@
202211
"options": []
203212
},
204213
"first": 1,
205-
"last": 10
214+
"last": 9
206215
}
207216
],
208217
"brackets": 0,
@@ -211,14 +220,6 @@
211220
},
212221
"errors": {
213222
"lexer": [],
214-
"parser": [
215-
[
216-
"A new statement was found, but no delimiter between it and the previous one.",
217-
{
218-
"@type": "@13"
219-
},
220-
0
221-
]
222-
]
223+
"parser": []
223224
}
224225
}

0 commit comments

Comments
 (0)