From 120bce04679877219a6585d5a0ffa2991b12e76f Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Mon, 27 Nov 2023 09:33:15 +0800 Subject: [PATCH] feat: support drop index and key in mysql --- pegjs/mariadb.pegjs | 33 +++++++++++++++++++++++++-------- pegjs/mysql.pegjs | 22 +++++++--------------- test/mysql-mariadb.spec.js | 7 +++++++ 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 0d4a4147..a0ce1386 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -811,6 +811,7 @@ alter_action_list alter_action = ALTER_ADD_COLUMN + / ALTER_DROP_KEY_INDEX / ALTER_DROP_COLUMN / ALTER_MODIFY_COLUMN / ALTER_ADD_INDEX_OR_KEY @@ -866,9 +867,7 @@ ALTER_MODIFY_COLUMN } ALTER_DROP_COLUMN - = KW_DROP __ - kc:KW_COLUMN __ - c:column_ref { + = KW_DROP __ kc:KW_COLUMN __ c:column_ref { return { action: 'drop', column: c, @@ -877,8 +876,7 @@ ALTER_DROP_COLUMN type: 'alter', } } - / KW_DROP __ - c:column_ref { + / KW_DROP __ c:column_ref { return { action: 'drop', column: c, @@ -887,10 +885,29 @@ ALTER_DROP_COLUMN } } +ALTER_DROP_KEY_INDEX + = KW_DROP __ 'PRIMARY'i __ KW_KEY { + return { + action: 'drop', + key: '', + keyword: 'primary key', + resource: 'key', + type: 'alter', + } + } + / KW_DROP __ k:(('FOREIGN'i? __ KW_KEY) / (KW_INDEX)) __ c:ident_name { + const resource = Array.isArray(k) ? 'key' : 'index' + return { + action: 'drop', + [resource]: c, + keyword: Array.isArray(k) ? `${[k[0], k[2]].filter(v => v).join(' ').toLowerCase()}` : k.toLowerCase(), + resource, + type: 'alter', + } + } + ALTER_ADD_INDEX_OR_KEY - = KW_ADD __ - id:create_index_definition - { + = KW_ADD __ id:create_index_definition { return { action: 'add', type: 'alter', diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 2245d765..ed43d8c7 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -1011,7 +1011,7 @@ alter_action_list alter_action = ALTER_ADD_CONSTRAINT / ALTER_DROP_CONSTRAINT - / ALTER_DROP_KEY + / ALTER_DROP_KEY_INDEX / ALTER_ENABLE_CONSTRAINT / ALTER_DISABLE_CONSTRAINT / ALTER_ADD_COLUMN @@ -1175,7 +1175,7 @@ ALTER_ADD_CONSTRAINT } } -ALTER_DROP_KEY +ALTER_DROP_KEY_INDEX = KW_DROP __ 'PRIMARY'i __ KW_KEY { return { action: 'drop', @@ -1185,21 +1185,13 @@ ALTER_DROP_KEY type: 'alter', } } - / KW_DROP __ 'FOREIGN'i __ KW_KEY __ c:ident_name { + / KW_DROP __ k:(('FOREIGN'i? __ KW_KEY) / (KW_INDEX)) __ c:ident_name { + const resource = Array.isArray(k) ? 'key' : 'index' return { action: 'drop', - key: c, - keyword: 'foreign key', - resource: 'key', - type: 'alter', - } - } - / KW_DROP __ (KW_KEY / KW_INDEX) __ c:ident_name { - return { - action: 'drop', - index: c, - keyword: 'index', - resource: 'index', + [resource]: c, + keyword: Array.isArray(k) ? `${[k[0], k[2]].filter(v => v).join(' ').toLowerCase()}` : k.toLowerCase(), + resource, type: 'alter', } } diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index 32203530..746cc87b 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -879,6 +879,13 @@ describe('mysql', () => { 'SELECT * FROM `test` WHERE `name` LIKE :pattern COLLATE UTF8MB4_GENERAL_CI' ] }, + { + title: 'alter drop index or key', + sql: [ + 'ALTER TABLE table_name DROP INDEX index_name', + 'ALTER TABLE `table_name` DROP INDEX index_name' + ] + }, ] SQL_LIST.forEach(sqlInfo => { const { title, sql } = sqlInfo