From 50ba60b928b8f2e10a9650598817e9c86a90bd4a Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Mon, 23 Nov 2026 20:16:56 +0000 Subject: [PATCH] Store the database name in the database field for ALTER and DROP DATABASE AlterStatement and DropStatement parsed the name of a database as a table name, so `ALTER DATABASE db` and `DROP DATABASE db` had the name in `$expr->table`. Query::getTables() also listed the database of `ALTER DATABASE db` as a table. Parse the name with `parseField => database` when the statement has the DATABASE or SCHEMA option, as CreateStatement already does. For DROP, the generic Statement::parse() looked up the clause parser options before the statement options were known; Statement now gets them from the new getKeywordParserOptions() method, which DropStatement overrides. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Kamil Tekiela --- src/Statement.php | 22 +- src/Statements/AlterStatement.php | 6 +- src/Statements/DropStatement.php | 14 ++ tests/Parser/DropStatementTest.php | 2 + tests/Utils/QueryTest.php | 4 + tests/data/parser/parseAlter5.out | 4 +- tests/data/parser/parseDropDatabase.in | 1 + tests/data/parser/parseDropDatabase.out | 219 +++++++++++++++++++ tests/data/parser/parseDropSchema.in | 1 + tests/data/parser/parseDropSchema.out | 272 ++++++++++++++++++++++++ 10 files changed, 534 insertions(+), 11 deletions(-) create mode 100644 tests/data/parser/parseDropDatabase.in create mode 100644 tests/data/parser/parseDropDatabase.out create mode 100644 tests/data/parser/parseDropSchema.in create mode 100644 tests/data/parser/parseDropSchema.out diff --git a/src/Statement.php b/src/Statement.php index 6dcc1902..2c929436 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -241,9 +241,9 @@ public function parse(Parser $parser, TokensList $list): void $field = null; /** - * Parser's options. + * The keyword of the clause, a key of `Parser::KEYWORD_PARSERS`. */ - $options = []; + $keyword = ''; // Looking for duplicated clauses. if ( @@ -270,11 +270,9 @@ public function parse(Parser $parser, TokensList $list): void // so let the value use the keyword field for truncate type. $tokenValue = $token->keyword === 'TRUNCATE' ? $token->keyword : $token->value; if (is_string($tokenValue) && isset(Parser::KEYWORD_PARSERS[$tokenValue]) && $list->idx < $list->count) { + $keyword = $tokenValue; $class = Parser::KEYWORD_PARSERS[$tokenValue]['class']; $field = Parser::KEYWORD_PARSERS[$tokenValue]['field']; - if (isset(Parser::KEYWORD_PARSERS[$tokenValue]['options'])) { - $options = Parser::KEYWORD_PARSERS[$tokenValue]['options']; - } } // Checking if this is the beginning of the statement. @@ -327,7 +325,7 @@ public function parse(Parser $parser, TokensList $list): void } ++$list->idx; // Skipping keyword or last option. - $this->$field = $class::parse($parser, $list, $options); + $this->$field = $class::parse($parser, $list, $this->getKeywordParserOptions($keyword)); } $this->after($parser, $list, $token); @@ -359,6 +357,18 @@ public function after(Parser $parser, TokensList $list, Token $token): void { } + /** + * Gets the options for the parser of a clause. The options of the statement are already parsed. + * + * @param string $keyword the keyword of the clause, a key of `Parser::KEYWORD_PARSERS` + * + * @return array + */ + protected function getKeywordParserOptions(string $keyword): array + { + return Parser::KEYWORD_PARSERS[$keyword]['options'] ?? []; + } + /** * Gets the clauses of this statement. * diff --git a/src/Statements/AlterStatement.php b/src/Statements/AlterStatement.php index 40cda95f..abb3ca29 100644 --- a/src/Statements/AlterStatement.php +++ b/src/Statements/AlterStatement.php @@ -23,7 +23,7 @@ class AlterStatement extends Statement { /** - * Table affected. + * The altered object. A database name is in `database`, the name of other objects in `table`. */ public Expression|null $table = null; @@ -77,12 +77,12 @@ public function parse(Parser $parser, TokensList $list): void $this->options = $parsedOptions; ++$list->idx; - // Parsing affected table. + // Parsing the name of the altered object. $this->table = Expressions::parse( $parser, $list, [ - 'parseField' => 'table', + 'parseField' => $parsedOptions->has('DATABASE') ? 'database' : 'table', 'breakOnAlias' => true, ], ); diff --git a/src/Statements/DropStatement.php b/src/Statements/DropStatement.php index 26b6123f..cc7e0dd7 100644 --- a/src/Statements/DropStatement.php +++ b/src/Statements/DropStatement.php @@ -76,4 +76,18 @@ class DropStatement extends Statement * Table of the dropped index. */ public Expression|null $table = null; + + /** @return array */ + protected function getKeywordParserOptions(string $keyword): array + { + if ( + $keyword === 'DROP' + && $this->options !== null + && ($this->options->has('DATABASE') || $this->options->has('SCHEMA')) + ) { + return ['parseField' => 'database']; + } + + return parent::getKeywordParserOptions($keyword); + } } diff --git a/tests/Parser/DropStatementTest.php b/tests/Parser/DropStatementTest.php index 5685c39d..4104bdf3 100644 --- a/tests/Parser/DropStatementTest.php +++ b/tests/Parser/DropStatementTest.php @@ -21,6 +21,8 @@ public static function dropProvider(): array return [ ['parser/parseDrop'], ['parser/parseDrop2'], + ['parser/parseDropDatabase'], + ['parser/parseDropSchema'], ]; } } diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 136332bc..f53b8dec 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -460,6 +460,10 @@ public static function getTablesProvider(): array 'TRUNCATE tbl', ['`tbl`'], ], + [ + 'ALTER DATABASE db CHARACTER SET utf8mb4', + [], + ], [ 'DROP VIEW v', [], diff --git a/tests/data/parser/parseAlter5.out b/tests/data/parser/parseAlter5.out index 82d98037..1db20a03 100644 --- a/tests/data/parser/parseAlter5.out +++ b/tests/data/parser/parseAlter5.out @@ -353,8 +353,8 @@ "last": 11, "table": { "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", - "database": null, - "table": "abc", + "database": "abc", + "table": null, "column": null, "expr": "`abc`", "alias": null, diff --git a/tests/data/parser/parseDropDatabase.in b/tests/data/parser/parseDropDatabase.in new file mode 100644 index 00000000..3a062404 --- /dev/null +++ b/tests/data/parser/parseDropDatabase.in @@ -0,0 +1 @@ +DROP DATABASE `db` \ No newline at end of file diff --git a/tests/data/parser/parseDropDatabase.out b/tests/data/parser/parseDropDatabase.out new file mode 100644 index 00000000..dd9d0233 --- /dev/null +++ b/tests/data/parser/parseDropDatabase.out @@ -0,0 +1,219 @@ +{ + "query": "DROP DATABASE `db`", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "DROP DATABASE `db`", + "len": 18, + "last": 18, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 6, + "idx": 6, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DROP", + "value": "DROP", + "keyword": "DROP", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 4 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DATABASE", + "value": "DATABASE", + "keyword": "DATABASE", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 35, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 13 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`db`", + "value": "db", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Symbol", + "value": 8 + }, + "flags": 2, + "position": 14 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 6, + "idx": 6, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DROP", + "value": "DROP", + "keyword": "DROP", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 4 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DATABASE", + "value": "DATABASE", + "keyword": "DATABASE", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 35, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 13 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`db`", + "value": "db", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Symbol", + "value": 8 + }, + "flags": 2, + "position": 14 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\DropStatement", + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": "DATABASE" + } + }, + "first": 0, + "last": 4, + "fields": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": "db", + "table": null, + "column": null, + "expr": "`db`", + "alias": null, + "function": null, + "subquery": null + } + ], + "table": null + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseDropSchema.in b/tests/data/parser/parseDropSchema.in new file mode 100644 index 00000000..a18ac229 --- /dev/null +++ b/tests/data/parser/parseDropSchema.in @@ -0,0 +1 @@ +DROP SCHEMA IF EXISTS db \ No newline at end of file diff --git a/tests/data/parser/parseDropSchema.out b/tests/data/parser/parseDropSchema.out new file mode 100644 index 00000000..631fd61a --- /dev/null +++ b/tests/data/parser/parseDropSchema.out @@ -0,0 +1,272 @@ +{ + "query": "DROP SCHEMA IF EXISTS db", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "DROP SCHEMA IF EXISTS db", + "len": 24, + "last": 24, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 8, + "idx": 8, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DROP", + "value": "DROP", + "keyword": "DROP", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 4 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "SCHEMA", + "value": "SCHEMA", + "keyword": "SCHEMA", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 35, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "IF EXISTS", + "value": "IF EXISTS", + "keyword": "IF EXISTS", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 7, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 21 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "db", + "value": "db", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "None", + "value": 0 + }, + "flags": 0, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 8, + "idx": 8, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "DROP", + "value": "DROP", + "keyword": "DROP", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 4 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "SCHEMA", + "value": "SCHEMA", + "keyword": "SCHEMA", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 35, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "IF EXISTS", + "value": "IF EXISTS", + "keyword": "IF EXISTS", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 7, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 21 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "db", + "value": "db", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "None", + "value": 0 + }, + "flags": 0, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\DropStatement", + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": "SCHEMA", + "3": "IF EXISTS" + } + }, + "first": 0, + "last": 6, + "fields": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": "db", + "table": null, + "column": null, + "expr": "db", + "alias": null, + "function": null, + "subquery": null + } + ], + "table": null + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file