Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Statements/InsertStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public function parse(Parser $parser, TokensList $list): void
*/
$miniState = 0;

$openBrackets = 0;
$firstBracket = 0;

for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
Expand Down Expand Up @@ -190,6 +193,22 @@ public function parse(Parser $parser, TokensList $list): void

$state = 1;
} elseif ($state === 1) {
// The `SELECT` might be wrapped in brackets, the closing one is
// handled by the statement through the bracket counter.
if ($token->type === TokenType::Operator && $token->value === '(') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this bracket every branch below is still open, not only SELECT. So some broken queries now parse with no error, where master reported one:

query master this PR
INSERT INTO a (b) (SET b=1) Unexpected token. no errors, $parser->brackets left at 1
INSERT INTO a (b) (SET b=1); SELECT 1 ) Unexpected token. no errors (the leftover bracket also hides the stray ) in the next statement)
INSERT INTO a (b) ( Unexpected token. no errors

SET and VALUES never look at $parser->brackets; only Statement::parse (so SELECT) takes the closing one back.

Letting only SELECT follow the bracket fixes the two SET rows, and the suite still passes (1032 tests):

++$parser->brackets;
++$openBrackets; // = 0 before the loop
continue;
}

if ($openBrackets > 0 && $token->keyword !== 'SELECT') {
    $parser->error('Unexpected token.', $token);
    break;
}

The unclosed ( at the end still needs its own check after the loop.

Also, REPLACE INTO a (b) (SELECT 1) has the same error as #484 and is valid in MySQL too. ReplaceStatement has the same keyword check, so it may be worth doing it here as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks. only SELECT can follow the bracket now, anything else (and the unclosed one) errors at the bracket like on master, with the counter put back. did REPLACE too and added fixtures for all of these

if ($openBrackets === 0) {
$firstBracket = $list->idx;
}

++$parser->brackets;
++$openBrackets;
continue;
}

if ($openBrackets > 0 && $token->keyword !== 'SELECT') {
break;
}

if ($token->type !== TokenType::Keyword) {
$parser->error('Unexpected token.', $token);
break;
Expand Down Expand Up @@ -240,6 +259,12 @@ public function parse(Parser $parser, TokensList $list): void
}
}

if ($openBrackets > 0 && $this->select === null) {
$parser->brackets -= $openBrackets;
$list->idx = $firstBracket;
$parser->error('Unexpected token.', $list->tokens[$firstBracket]);
}

--$list->idx;
}
}
25 changes: 25 additions & 0 deletions src/Statements/ReplaceStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public function parse(Parser $parser, TokensList $list): void
*/
$state = 0;

$openBrackets = 0;
$firstBracket = 0;

for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
Expand Down Expand Up @@ -151,6 +154,22 @@ public function parse(Parser $parser, TokensList $list): void

$state = 1;
} elseif ($state === 1) {
// The `SELECT` might be wrapped in brackets, the closing one is
// handled by the statement through the bracket counter.
if ($token->type === TokenType::Operator && $token->value === '(') {
if ($openBrackets === 0) {
$firstBracket = $list->idx;
}

++$parser->brackets;
++$openBrackets;
continue;
}

if ($openBrackets > 0 && $token->keyword !== 'SELECT') {
break;
}

if ($token->type !== TokenType::Keyword) {
$parser->error('Unexpected token.', $token);
break;
Expand All @@ -175,6 +194,12 @@ public function parse(Parser $parser, TokensList $list): void
}
}

if ($openBrackets > 0 && $this->select === null) {
$parser->brackets -= $openBrackets;
$list->idx = $firstBracket;
$parser->error('Unexpected token.', $list->tokens[$firstBracket]);
}

--$list->idx;
}
}
3 changes: 3 additions & 0 deletions tests/Parser/InsertStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static function insertProvider(): array
['parser/parseInsert'],
['parser/parseInsertFunction'],
['parser/parseInsertSelect'],
['parser/parseInsertSelectWithParenthesis'],
['parser/parseInsertSelectWithParenthesisErr'],
['parser/parseInsertSelectWithParenthesisErr2'],
['parser/parseInsertOnDuplicateKey'],
['parser/parseInsertSetOnDuplicateKey'],
['parser/parseInsertSelectOnDuplicateKey'],
Expand Down
2 changes: 2 additions & 0 deletions tests/Parser/ReplaceStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static function replaceProvider(): array
['parser/parseReplaceValues'],
['parser/parseReplaceSet'],
['parser/parseReplaceSelect'],
['parser/parseReplaceSelectWithParenthesis'],
['parser/parseReplaceSelectWithParenthesisErr'],
['parser/parseReplaceErr'],
['parser/parseReplaceErr2'],
['parser/parseReplaceErr3'],
Expand Down
1 change: 1 addition & 0 deletions tests/data/parser/parseInsertSelectWithParenthesis.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `a` (`value`) (SELECT `b`.`value` FROM `b`)
Loading