Skip to content

Commit

Permalink
feat: support drop index and key in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Nov 27, 2023
1 parent c5d9d8b commit 120bce0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
33 changes: 25 additions & 8 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -877,8 +876,7 @@ ALTER_DROP_COLUMN
type: 'alter',
}
}
/ KW_DROP __
c:column_ref {
/ KW_DROP __ c:column_ref {
return {
action: 'drop',
column: c,
Expand All @@ -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',
Expand Down
22 changes: 7 additions & 15 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1175,7 +1175,7 @@ ALTER_ADD_CONSTRAINT
}
}

ALTER_DROP_KEY
ALTER_DROP_KEY_INDEX
= KW_DROP __ 'PRIMARY'i __ KW_KEY {
return {
action: 'drop',
Expand All @@ -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',
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 120bce0

Please sign in to comment.