Skip to content

Commit c644e93

Browse files
Merge pull request #542 from MauricioFauth/fix/issue-496
Fix keywords not being recognized as table alias
2 parents daffc4e + 716d545 commit c644e93

File tree

10 files changed

+469
-30
lines changed

10 files changed

+469
-30
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/Misc/BugsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function bugProvider(): array
3535
['bugs/gh412'],
3636
['bugs/gh478'],
3737
['bugs/gh492'],
38+
['bugs/gh496'],
3839
['bugs/gh498'],
3940
['bugs/gh499'],
4041
['bugs/gh508'],

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.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT COUNT(*) AS amount
2+
FROM one i
3+
JOIN two io ON io.id = i.id

0 commit comments

Comments
 (0)