Skip to content

Commit cd49677

Browse files
committed
Explicitly check ON DUPLICATE KEY UPDATE and adding a test case
Signed-off-by: Fawzi E. Abdulfattah <[email protected]>
1 parent 199ba66 commit cd49677

File tree

4 files changed

+1568
-6
lines changed

4 files changed

+1568
-6
lines changed

src/Statements/WithStatement.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,22 @@ public function parse(Parser $parser, TokensList $list)
216216
if ($list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'ON')) {
217217
// (-1) because getNextOfTypeAndValue returned ON and increased the index.
218218
$idxOfOn = $list->idx - 1;
219-
// Index of the last parsed token will be the token before the ON Keyword, therefore $idxOfOn - 1.
220-
$idxOfLastParsedToken = $idxOfOn - 1;
221-
// The length of the expression tokens would be the difference
222-
// between the first unrelated token `ON` and the idx
223-
// before skipping the CTE tokens.
224-
$lengthOfExpressionTokens = $idxOfOn - $idxBeforeSearch;
219+
// We want to make sure that it's `ON DUPLICATE KEY UPDATE`
220+
$dubplicateToken = $list->getNext();
221+
$keyToken = $list->getNext();
222+
$updateToken = $list->getNext();
223+
if (
224+
$dubplicateToken && $dubplicateToken->keyword === 'DUPLICATE'
225+
&& ($keyToken && $keyToken->keyword === 'KEY')
226+
&& ($updateToken && $updateToken->keyword === 'UPDATE')
227+
) {
228+
// Index of the last parsed token will be the token before the ON Keyword
229+
$idxOfLastParsedToken = $idxOfOn - 1;
230+
// The length of the expression tokens would be the difference
231+
// between the first unrelated token `ON` and the idx
232+
// before skipping the CTE tokens.
233+
$lengthOfExpressionTokens = $idxOfOn - $idxBeforeSearch;
234+
}
225235
}
226236

227237
// Restore the index

tests/Parser/WithStatementTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function parseWith(): array
3333
['parser/parseWithStatement4'],
3434
['parser/parseWithStatement5'],
3535
['parser/parseWithStatement6'],
36+
['parser/parseWithStatement7'],
3637
['parser/parseWithStatementErr'],
3738
['parser/parseWithStatementErr1'],
3839
['parser/parseWithStatementErr2'],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WITH cte (col1) AS ( SELECT 1 UNION ALL SELECT 2 ) SELECT * FROM cte INNER JOIN table2 ON table2.col1=cte.col1;

0 commit comments

Comments
 (0)