From 0774027e8dc6ef01506e8f58dc17c572dfffba7a Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Wed, 4 Sep 2024 14:30:38 +0200 Subject: [PATCH 1/2] Proposal for a fix of #580. --- src/Components/AlterOperation.php | 5 ++++- tests/data/bugs/gh317.out | 18 +++++++----------- tests/data/parser/parseAlter.out | 18 +++++++----------- tests/data/parser/parsephpMyAdminExport1.out | 18 +++++++----------- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index 3e09c1cf..fcd40c32 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -131,7 +131,10 @@ class AlterOperation extends Component 'BY' => 2, 'FOREIGN' => 2, 'FULLTEXT' => 2, - 'KEY' => 2, + 'KEY' => [ + 2, + 'var', + ], 'KEYS' => 2, 'PARTITION' => 2, 'PARTITION BY' => 2, diff --git a/tests/data/bugs/gh317.out b/tests/data/bugs/gh317.out index 92e3d169..2e68ce1d 100644 --- a/tests/data/bugs/gh317.out +++ b/tests/data/bugs/gh317.out @@ -209,19 +209,15 @@ "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", "options": { "1": "ADD", - "2": "KEY" + "2": { + "name": "KEY", + "equals": false, + "expr": "`IDX_REPAIR`", + "value": "IDX_REPAIR" + } } }, - "field": { - "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", - "database": null, - "table": null, - "column": "IDX_REPAIR", - "expr": "`IDX_REPAIR`", - "alias": null, - "function": null, - "subquery": null - }, + "field": null, "partitions": null, "unknown": [ { diff --git a/tests/data/parser/parseAlter.out b/tests/data/parser/parseAlter.out index 6dcffee9..eb62eb84 100644 --- a/tests/data/parser/parseAlter.out +++ b/tests/data/parser/parseAlter.out @@ -327,19 +327,15 @@ "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", "options": { "1": "ADD", - "2": "KEY" + "2": { + "name": "KEY", + "equals": false, + "expr": "`idx_actor_last_name`", + "value": "idx_actor_last_name" + } } }, - "field": { - "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", - "database": null, - "table": null, - "column": "idx_actor_last_name", - "expr": "`idx_actor_last_name`", - "alias": null, - "function": null, - "subquery": null - }, + "field": null, "partitions": null, "unknown": [ { diff --git a/tests/data/parser/parsephpMyAdminExport1.out b/tests/data/parser/parsephpMyAdminExport1.out index 4ee4bdb4..b78796fd 100644 --- a/tests/data/parser/parsephpMyAdminExport1.out +++ b/tests/data/parser/parsephpMyAdminExport1.out @@ -5480,19 +5480,15 @@ "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", "options": { "1": "ADD", - "2": "KEY" + "2": { + "name": "KEY", + "equals": false, + "expr": "`INDEX_totalTime`", + "value": "INDEX_totalTime" + } } }, - "field": { - "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", - "database": null, - "table": null, - "column": "INDEX_totalTime", - "expr": "`INDEX_totalTime`", - "alias": null, - "function": null, - "subquery": null - }, + "field": null, "partitions": null, "unknown": [ { From 34c75394ebd46175d9639072d9c00e0c9c1512dc Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Wed, 4 Sep 2024 14:41:30 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Add=20test=20cases=20of=20the=20parser=20wi?= =?UTF-8?q?th=20ALTER=20TABLE=20=E2=80=A6=20RENAME=20KEY=20=E2=80=A6=20TO?= =?UTF-8?q?=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Parser/AlterStatementTest.php | 2 + .../data/parser/parseAlterTableRenameKey1.in | 1 + .../data/parser/parseAlterTableRenameKey1.out | 249 +++++++++++ .../data/parser/parseAlterTableRenameKey2.in | 1 + .../data/parser/parseAlterTableRenameKey2.out | 410 ++++++++++++++++++ 5 files changed, 663 insertions(+) create mode 100644 tests/data/parser/parseAlterTableRenameKey1.in create mode 100644 tests/data/parser/parseAlterTableRenameKey1.out create mode 100644 tests/data/parser/parseAlterTableRenameKey2.in create mode 100644 tests/data/parser/parseAlterTableRenameKey2.out diff --git a/tests/Parser/AlterStatementTest.php b/tests/Parser/AlterStatementTest.php index f4f739ca..19071502 100644 --- a/tests/Parser/AlterStatementTest.php +++ b/tests/Parser/AlterStatementTest.php @@ -42,6 +42,8 @@ public static function alterProvider(): array ['parser/parseAlterErr4'], ['parser/parseAlterTableRenameIndex1'], ['parser/parseAlterTableRenameIndex2'], + ['parser/parseAlterTableRenameKey1'], + ['parser/parseAlterTableRenameKey2'], ['parser/parseAlterTablePartitionByRange1'], ['parser/parseAlterTablePartitionByRange2'], ['parser/parseAlterTableCoalescePartition'], diff --git a/tests/data/parser/parseAlterTableRenameKey1.in b/tests/data/parser/parseAlterTableRenameKey1.in new file mode 100644 index 00000000..90e599a5 --- /dev/null +++ b/tests/data/parser/parseAlterTableRenameKey1.in @@ -0,0 +1 @@ +ALTER TABLE `transactions` RENAME KEY `fk_transactions_catalog_entries1_idx` TO `fk_transactions_catalog_entries2_idx` diff --git a/tests/data/parser/parseAlterTableRenameKey1.out b/tests/data/parser/parseAlterTableRenameKey1.out new file mode 100644 index 00000000..c178bfcf --- /dev/null +++ b/tests/data/parser/parseAlterTableRenameKey1.out @@ -0,0 +1,249 @@ +{ + "query": "ALTER TABLE `transactions` RENAME KEY `fk_transactions_catalog_entries1_idx` TO `fk_transactions_catalog_entries2_idx`\n", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "ALTER TABLE `transactions` RENAME KEY `fk_transactions_catalog_entries1_idx` TO `fk_transactions_catalog_entries2_idx`\n", + "len": 119, + "last": 119, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "ALTER", + "value": "ALTER", + "keyword": "ALTER", + "type": 1, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "TABLE", + "value": "TABLE", + "keyword": "TABLE", + "type": 1, + "flags": 3, + "position": 6 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`transactions`", + "value": "transactions", + "keyword": null, + "type": 8, + "flags": 2, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 26 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "RENAME", + "value": "RENAME", + "keyword": "RENAME", + "type": 1, + "flags": 3, + "position": 27 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 33 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "KEY", + "value": "KEY", + "keyword": "KEY", + "type": 1, + "flags": 19, + "position": 34 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 37 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`fk_transactions_catalog_entries1_idx`", + "value": "fk_transactions_catalog_entries1_idx", + "keyword": null, + "type": 8, + "flags": 2, + "position": 38 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 76 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "TO", + "value": "TO", + "keyword": "TO", + "type": 1, + "flags": 3, + "position": 77 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 79 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`fk_transactions_catalog_entries2_idx`", + "value": "fk_transactions_catalog_entries2_idx", + "keyword": null, + "type": 8, + "flags": 2, + "position": 80 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 118 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": null, + "value": null, + "keyword": null, + "type": 9, + "flags": 0, + "position": null + } + ], + "count": 17, + "idx": 17 + }, + "delimiter": ";", + "delimiterLen": 1, + "strict": false, + "errors": [] + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\AlterStatement", + "table": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": "transactions", + "column": null, + "expr": "`transactions`", + "alias": null, + "function": null, + "subquery": null + }, + "altered": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation", + "ROUTINE_OPTIONS": { + "COMMENT": [ + 1, + "var" + ], + "LANGUAGE SQL": 2, + "CONTAINS SQL": 3, + "NO SQL": 3, + "READS SQL DATA": 3, + "MODIFIES SQL DATA": 3, + "SQL SECURITY": 4, + "DEFINER": 5, + "INVOKER": 5 + }, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": "RENAME", + "2": { + "name": "KEY", + "equals": false, + "expr": "`fk_transactions_catalog_entries1_idx`", + "value": "fk_transactions_catalog_entries1_idx" + }, + "3": { + "name": "TO", + "equals": false, + "expr": "`fk_transactions_catalog_entries2_idx`", + "value": "fk_transactions_catalog_entries2_idx" + } + } + }, + "field": null, + "partitions": null, + "unknown": [] + } + ], + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "3": "TABLE" + } + }, + "first": 0, + "last": 16 + } + ], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseAlterTableRenameKey2.in b/tests/data/parser/parseAlterTableRenameKey2.in new file mode 100644 index 00000000..b59fc642 --- /dev/null +++ b/tests/data/parser/parseAlterTableRenameKey2.in @@ -0,0 +1 @@ +ALTER TABLE testtable RENAME KEY my_index TO my_index2, ALGORITHM=INPLACE, LOCK=NONE; diff --git a/tests/data/parser/parseAlterTableRenameKey2.out b/tests/data/parser/parseAlterTableRenameKey2.out new file mode 100644 index 00000000..934d3913 --- /dev/null +++ b/tests/data/parser/parseAlterTableRenameKey2.out @@ -0,0 +1,410 @@ +{ + "query": "ALTER TABLE testtable RENAME KEY my_index TO my_index2, ALGORITHM=INPLACE, LOCK=NONE;\n", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "ALTER TABLE testtable RENAME KEY my_index TO my_index2, ALGORITHM=INPLACE, LOCK=NONE;\n", + "len": 86, + "last": 86, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "ALTER", + "value": "ALTER", + "keyword": "ALTER", + "type": 1, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 5 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "TABLE", + "value": "TABLE", + "keyword": "TABLE", + "type": 1, + "flags": 3, + "position": 6 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "testtable", + "value": "testtable", + "keyword": null, + "type": 0, + "flags": 0, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 21 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "RENAME", + "value": "RENAME", + "keyword": "RENAME", + "type": 1, + "flags": 3, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 28 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "KEY", + "value": "KEY", + "keyword": "KEY", + "type": 1, + "flags": 19, + "position": 29 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 32 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "my_index", + "value": "my_index", + "keyword": null, + "type": 0, + "flags": 0, + "position": 33 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 41 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "TO", + "value": "TO", + "keyword": "TO", + "type": 1, + "flags": 3, + "position": 42 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 44 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "my_index2", + "value": "my_index2", + "keyword": null, + "type": 0, + "flags": 0, + "position": 45 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 54 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 55 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "ALGORITHM", + "value": "ALGORITHM", + "keyword": "ALGORITHM", + "type": 1, + "flags": 1, + "position": 56 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 65 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "INPLACE", + "value": "INPLACE", + "keyword": null, + "type": 0, + "flags": 0, + "position": 66 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 73 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 74 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "LOCK", + "value": "LOCK", + "keyword": "LOCK", + "type": 1, + "flags": 3, + "position": 75 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "=", + "value": "=", + "keyword": null, + "type": 2, + "flags": 2, + "position": 79 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "NONE", + "value": "NONE", + "keyword": "NONE", + "type": 1, + "flags": 1, + "position": 80 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ";", + "value": ";", + "keyword": null, + "type": 9, + "flags": 0, + "position": 84 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 85 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": null, + "value": null, + "keyword": null, + "type": 9, + "flags": 0, + "position": null + } + ], + "count": 28, + "idx": 28 + }, + "delimiter": ";", + "delimiterLen": 1, + "strict": false, + "errors": [] + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\AlterStatement", + "table": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": "testtable", + "column": null, + "expr": "testtable", + "alias": null, + "function": null, + "subquery": null + }, + "altered": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation", + "ROUTINE_OPTIONS": { + "COMMENT": [ + 1, + "var" + ], + "LANGUAGE SQL": 2, + "CONTAINS SQL": 3, + "NO SQL": 3, + "READS SQL DATA": 3, + "MODIFIES SQL DATA": 3, + "SQL SECURITY": 4, + "DEFINER": 5, + "INVOKER": 5 + }, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": "RENAME", + "2": { + "name": "KEY", + "equals": false, + "expr": "my_index", + "value": "my_index" + }, + "3": { + "name": "TO", + "equals": false, + "expr": "my_index2", + "value": "my_index2" + } + } + }, + "field": null, + "partitions": null, + "unknown": [] + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation", + "ROUTINE_OPTIONS": { + "COMMENT": [ + 1, + "var" + ], + "LANGUAGE SQL": 2, + "CONTAINS SQL": 3, + "NO SQL": 3, + "READS SQL DATA": 3, + "MODIFIES SQL DATA": 3, + "SQL SECURITY": 4, + "DEFINER": 5, + "INVOKER": 5 + }, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": { + "name": "ALGORITHM", + "equals": true, + "expr": "INPLACE", + "value": "INPLACE" + } + } + }, + "field": null, + "partitions": null, + "unknown": [] + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation", + "ROUTINE_OPTIONS": { + "COMMENT": [ + 1, + "var" + ], + "LANGUAGE SQL": 2, + "CONTAINS SQL": 3, + "NO SQL": 3, + "READS SQL DATA": 3, + "MODIFIES SQL DATA": 3, + "SQL SECURITY": 4, + "DEFINER": 5, + "INVOKER": 5 + }, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "1": { + "name": "LOCK", + "equals": true, + "expr": "NONE", + "value": "NONE" + } + } + }, + "field": null, + "partitions": null, + "unknown": [] + } + ], + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "3": "TABLE" + } + }, + "first": 0, + "last": 25 + } + ], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file