diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index fcd40c32..4264f03f 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -146,6 +146,8 @@ class AlterOperation extends Component 2, 'var', ], + 'UNIQUE' => 2, + 'UNIQUE KEY' => 2, 'CHARACTER SET' => 3, 'TO' => [ diff --git a/tests/Parser/AlterStatementTest.php b/tests/Parser/AlterStatementTest.php index 19071502..7207c87a 100644 --- a/tests/Parser/AlterStatementTest.php +++ b/tests/Parser/AlterStatementTest.php @@ -49,6 +49,8 @@ public static function alterProvider(): array ['parser/parseAlterTableCoalescePartition'], ['parser/parseAlterTableAddColumnWithCheck'], ['parser/parseAlterTableAddSpatialIndex1'], + ['parser/parseAlterTableAddUniqueKey1'], + ['parser/parseAlterTableAddUniqueKey2'], ['parser/parseAlterTableDropAddIndex1'], ['parser/parseAlterTableDropColumn1'], ['parser/parseAlterTableModifyColumn'], diff --git a/tests/data/parser/parseAlter14.out b/tests/data/parser/parseAlter14.out index a8eb1149..51c9699c 100644 --- a/tests/data/parser/parseAlter14.out +++ b/tests/data/parser/parseAlter14.out @@ -325,24 +325,22 @@ "options": { "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", "options": { - "1": "ADD" + "1": "ADD", + "2": "UNIQUE KEY" } }, - "field": null, + "field": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": null, + "column": "functional_index", + "expr": "`functional_index`", + "alias": null, + "function": null, + "subquery": null + }, "partitions": null, "unknown": [ - { - "@type": "@10" - }, - { - "@type": "@11" - }, - { - "@type": "@12" - }, - { - "@type": "@13" - }, { "@type": "@14" }, diff --git a/tests/data/parser/parseAlterTableAddUniqueKey1.in b/tests/data/parser/parseAlterTableAddUniqueKey1.in new file mode 100644 index 00000000..15baa47a --- /dev/null +++ b/tests/data/parser/parseAlterTableAddUniqueKey1.in @@ -0,0 +1 @@ +ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`); diff --git a/tests/data/parser/parseAlterTableAddUniqueKey1.out b/tests/data/parser/parseAlterTableAddUniqueKey1.out new file mode 100644 index 00000000..074879e9 --- /dev/null +++ b/tests/data/parser/parseAlterTableAddUniqueKey1.out @@ -0,0 +1,266 @@ +{ + "query": "ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`);\n", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "ALTER TABLE testtable ADD UNIQUE KEY name_of_the_unique_constraint (`UNIQUE_COLUMN`);\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": "ADD", + "value": "ADD", + "keyword": "ADD", + "type": 1, + "flags": 3, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 25 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "UNIQUE KEY", + "value": "UNIQUE KEY", + "keyword": "UNIQUE KEY", + "type": 1, + "flags": 23, + "position": 26 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 36 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "name_of_the_unique_constraint", + "value": "name_of_the_unique_constraint", + "keyword": null, + "type": 0, + "flags": 0, + "position": 37 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 66 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "(", + "value": "(", + "keyword": null, + "type": 2, + "flags": 16, + "position": 67 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`UNIQUE_COLUMN`", + "value": "UNIQUE_COLUMN", + "keyword": null, + "type": 8, + "flags": 2, + "position": 68 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ")", + "value": ")", + "keyword": null, + "type": 2, + "flags": 16, + "position": 83 + }, + { + "@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": 18, + "idx": 18 + }, + "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": "ADD", + "2": "UNIQUE KEY" + } + }, + "field": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": null, + "column": "name_of_the_unique_constraint", + "expr": "name_of_the_unique_constraint", + "alias": null, + "function": null, + "subquery": null + }, + "partitions": null, + "unknown": [ + { + "@type": "@14" + }, + { + "@type": "@15" + }, + { + "@type": "@16" + } + ] + } + ], + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "3": "TABLE" + } + }, + "first": 0, + "last": 15 + } + ], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseAlterTableAddUniqueKey2.in b/tests/data/parser/parseAlterTableAddUniqueKey2.in new file mode 100644 index 00000000..aedc2012 --- /dev/null +++ b/tests/data/parser/parseAlterTableAddUniqueKey2.in @@ -0,0 +1 @@ +ALTER TABLE testtable ADD UNIQUE name_of_the_unique_constraint (`UNIQUE_COLUMN`); diff --git a/tests/data/parser/parseAlterTableAddUniqueKey2.out b/tests/data/parser/parseAlterTableAddUniqueKey2.out new file mode 100644 index 00000000..bcd766b6 --- /dev/null +++ b/tests/data/parser/parseAlterTableAddUniqueKey2.out @@ -0,0 +1,266 @@ +{ + "query": "ALTER TABLE testtable ADD UNIQUE name_of_the_unique_constraint (`UNIQUE_COLUMN`);\n", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "ALTER TABLE testtable ADD UNIQUE name_of_the_unique_constraint (`UNIQUE_COLUMN`);\n", + "len": 82, + "last": 82, + "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": "ADD", + "value": "ADD", + "keyword": "ADD", + "type": 1, + "flags": 3, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 25 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "UNIQUE", + "value": "UNIQUE", + "keyword": "UNIQUE", + "type": 1, + "flags": 19, + "position": 26 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 32 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "name_of_the_unique_constraint", + "value": "name_of_the_unique_constraint", + "keyword": null, + "type": 0, + "flags": 0, + "position": 33 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 62 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "(", + "value": "(", + "keyword": null, + "type": 2, + "flags": 16, + "position": 63 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "`UNIQUE_COLUMN`", + "value": "UNIQUE_COLUMN", + "keyword": null, + "type": 8, + "flags": 2, + "position": 64 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ")", + "value": ")", + "keyword": null, + "type": 2, + "flags": 16, + "position": 79 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ";", + "value": ";", + "keyword": null, + "type": 9, + "flags": 0, + "position": 80 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "\n", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 81 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": null, + "value": null, + "keyword": null, + "type": 9, + "flags": 0, + "position": null + } + ], + "count": 18, + "idx": 18 + }, + "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": "ADD", + "2": "UNIQUE" + } + }, + "field": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": null, + "column": "name_of_the_unique_constraint", + "expr": "name_of_the_unique_constraint", + "alias": null, + "function": null, + "subquery": null + }, + "partitions": null, + "unknown": [ + { + "@type": "@14" + }, + { + "@type": "@15" + }, + { + "@type": "@16" + } + ] + } + ], + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": { + "3": "TABLE" + } + }, + "first": 0, + "last": 15 + } + ], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file diff --git a/tests/data/parser/parsephpMyAdminExport1.out b/tests/data/parser/parsephpMyAdminExport1.out index b78796fd..786d792b 100644 --- a/tests/data/parser/parsephpMyAdminExport1.out +++ b/tests/data/parser/parsephpMyAdminExport1.out @@ -5401,24 +5401,22 @@ "options": { "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", "options": { - "1": "ADD" + "1": "ADD", + "2": "UNIQUE KEY" } }, - "field": null, + "field": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression", + "database": null, + "table": null, + "column": "idServer", + "expr": "`idServer`", + "alias": null, + "function": null, + "subquery": null + }, "partitions": null, "unknown": [ - { - "@type": "@394" - }, - { - "@type": "@395" - }, - { - "@type": "@396" - }, - { - "@type": "@397" - }, { "@type": "@398" },