Skip to content

Commit

Permalink
Merge pull request #542 from MauricioFauth/fix/issue-496
Browse files Browse the repository at this point in the history
Fix keywords not being recognized as table alias
  • Loading branch information
MauricioFauth committed Jan 20, 2024
2 parents daffc4e + 716d545 commit c644e93
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 30 deletions.
6 changes: 1 addition & 5 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ class AlterOperation extends Component
'ON COMPLETION PRESERVE' => 5,
'ON COMPLETION NOT PRESERVE' => 5,
'RENAME' => 6,
'TO' => [
7,
'expr',
['parseField' => 'table'],
],
'TO' => [7, 'expr', ['parseField' => 'table', 'breakOnAlias' => true]],
'ENABLE' => 8,
'DISABLE' => 8,
'DISABLE ON SLAVE' => 8,
Expand Down
5 changes: 4 additions & 1 deletion src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

$isExpr = true;
} elseif ($brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias) {
} elseif (
$brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias
&& ($ret->table === null || $ret->table === '')
) {
/* End of expression */
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Statements/LoadStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function parse(Parser $parser, TokensList $list)
}

++$list->idx;
$this->table = Expression::parse($parser, $list, ['parseField' => 'table']);
$this->table = Expression::parse($parser, $list, ['parseField' => 'table', 'breakOnAlias' => true]);
$state = 3;
} elseif ($state >= 3 && $state <= 7) {
if ($token->type === Token::TYPE_KEYWORD) {
Expand Down
1 change: 1 addition & 0 deletions tests/Misc/BugsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function bugProvider(): array
['bugs/gh412'],
['bugs/gh478'],
['bugs/gh492'],
['bugs/gh496'],
['bugs/gh498'],
['bugs/gh499'],
['bugs/gh508'],
Expand Down
8 changes: 6 additions & 2 deletions tests/Utils/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ public function testGetWithNullToken(): void
{
$lexer = new Lexer('LOCK TABLES table1 AS `t1` LOCAL');
$parser = new Parser($lexer->list);
$this->assertEquals(
[['Unexpected keyword.', 0, 'LOCAL', 27], ['Unexpected end of LOCK expression.', 0, null, null]],
$this->assertSame(
[
['An alias was previously found.', 0, 'LOCAL', 27],
['Unexpected keyword.', 0, 'LOCAL', 27],
['Unexpected end of LOCK expression.', 0, '', null],
],
Error::get([$lexer, $parser])
);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/data/bugs/gh496.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT COUNT(*) AS amount
FROM one i
JOIN two io ON io.id = i.id
Loading

0 comments on commit c644e93

Please sign in to comment.