diff --git a/src/Components/SetOperation.php b/src/Components/SetOperation.php index 47396fc7a..63bc66574 100644 --- a/src/Components/SetOperation.php +++ b/src/Components/SetOperation.php @@ -111,19 +111,27 @@ public static function parse(Parser $parser, TokensList $list, array $options = $commaLastSeenAt = $token; } } elseif ($state === 1) { - $tmp = Expression::parse( - $parser, - $list, - ['breakOnAlias' => true] - ); - if ($tmp === null) { - $parser->error('Missing expression.', $token); - break; + if ($token->flags & Token::FLAG_SYMBOL_PARAMETER) { + $expr->column = trim($expr->column); + $expr->value = $token->value; + $ret[] = $expr; + } else { + $tmp = Expression::parse( + $parser, + $list, + ['breakOnAlias' => true] + ); + + if ($tmp === null) { + $parser->error('Missing expression.', $token); + break; + } + + $expr->column = trim($expr->column); + $expr->value = $tmp->expr; + $ret[] = $expr; } - $expr->column = trim($expr->column); - $expr->value = $tmp->expr; - $ret[] = $expr; $expr = new static(); $state = 0; $commaLastSeenAt = null; diff --git a/tests/Parser/UpdateStatementTest.php b/tests/Parser/UpdateStatementTest.php index fa00ff081..ef12ab0f6 100644 --- a/tests/Parser/UpdateStatementTest.php +++ b/tests/Parser/UpdateStatementTest.php @@ -28,6 +28,7 @@ public static function updateProvider(): array ['parser/parseUpdate5'], ['parser/parseUpdate6'], ['parser/parseUpdate7'], + ['parser/parseUpdate8'], ['parser/parseUpdateErr'], ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index fc196ae24..45a4e8816 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -93,7 +93,7 @@ public function getErrorsAsArray($obj): array */ public function getData(string $name): array { - $serializedData = file_get_contents('tests/data/' . $name . '.out'); + $serializedData = file_get_contents(__DIR__ . '/data/' . $name . '.out'); $this->assertIsString($serializedData); $serializer = new CustomJsonSerializer(); diff --git a/tests/data/parser/parseUpdate8.in b/tests/data/parser/parseUpdate8.in new file mode 100644 index 000000000..ff1582c96 --- /dev/null +++ b/tests/data/parser/parseUpdate8.in @@ -0,0 +1,11 @@ +UPDATE + users +SET + username = ?, + id=155; + +UPDATE + users +SET + username = :user_name, + id=155; \ No newline at end of file diff --git a/tests/data/parser/parseUpdate8.out b/tests/data/parser/parseUpdate8.out new file mode 100644 index 000000000..7e29ba7e2 --- /dev/null +++ b/tests/data/parser/parseUpdate8.out @@ -0,0 +1,440 @@ +{ + "query": "UPDATE\n users\nSET\n username = ?,\n id=155;\n\nUPDATE\n users\nSET\n username = :user_name,\n id=155;", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "UPDATE\n users\nSET\n username = ?,\n id=155;\n\nUPDATE\n users\nSET\n username = :user_name,\n id=155;", + "len": 111, + "last": 111, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "UPDATE", + "value": "UPDATE", + "keyword": "UPDATE", + "type": 1, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 6 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "users", + "value": "users", + "keyword": null, + "type": 0, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 16 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "SET", + "value": "SET", + "keyword": "SET", + "type": 1, + "flags": 11, + "position": 17 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 20 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "username", + "value": "username", + "keyword": null, + "type": 0, + "flags": 0, + "position": 25 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 33 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 34 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 35 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "?", + "value": "?", + "keyword": null, + "type": 8, + "flags": 16, + "position": 36 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 37 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 38 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "id", + "value": "id", + "keyword": null, + "type": 0, + "flags": 0, + "position": 43 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 45 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "155", + "value": 155, + "keyword": null, + "type": 6, + "flags": 0, + "position": 46 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ";", + "value": ";", + "keyword": null, + "type": 9, + "flags": 0, + "position": 49 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 50 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "UPDATE", + "value": "UPDATE", + "keyword": "UPDATE", + "type": 1, + "flags": 3, + "position": 52 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 58 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "users", + "value": "users", + "keyword": null, + "type": 0, + "flags": 0, + "position": 63 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 68 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "SET", + "value": "SET", + "keyword": "SET", + "type": 1, + "flags": 11, + "position": 69 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 72 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "username", + "value": "username", + "keyword": null, + "type": 0, + "flags": 0, + "position": 77 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 85 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 86 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 87 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ":user_name", + "value": "user_name", + "keyword": null, + "type": 8, + "flags": 16, + "position": 88 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 98 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 99 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "id", + "value": "id", + "keyword": null, + "type": 0, + "flags": 0, + "position": 104 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 106 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "155", + "value": 155, + "keyword": null, + "type": 6, + "flags": 0, + "position": 107 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ";", + "value": ";", + "keyword": null, + "type": 9, + "flags": 0, + "position": 110 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": null, + "value": null, + "keyword": null, + "type": 9, + "flags": 0, + "position": null + } + ], + "count": 36, + "idx": 36 + }, + "delimiter": ";", + "delimiterLen": 1, + "strict": false, + "errors": [] + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\UpdateStatement", + "tables": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": "users", + "column": null, + "expr": "users", + "alias": null, + "function": null, + "subquery": null + } + ], + "set": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation", + "column": "username", + "value": "?" + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation", + "column": "id", + "value": "155" + } + ], + "where": null, + "order": null, + "limit": null, + "join": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 0, + "last": 15 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\UpdateStatement", + "tables": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": "users", + "column": null, + "expr": "users", + "alias": null, + "function": null, + "subquery": null + } + ], + "set": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation", + "column": "username", + "value": "user_name" + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation", + "column": "id", + "value": "155" + } + ], + "where": null, + "order": null, + "limit": null, + "join": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 16, + "last": 33 + } + ], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [ + [ + "Variable name was expected.", + "?", + 36, + 0 + ] + ], + "parser": [] + } +} \ No newline at end of file