From b249a8c3c2c72299542e7dd96d1ecf01d3117f3b Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Mon, 25 Nov 2024 09:15:25 +0800 Subject: [PATCH] refactor: alter table first with optional column in mysql --- pegjs/mysql.pegjs | 10 +++++----- pegjs/sqlite.pegjs | 10 +++++----- src/alter.js | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index ac6374a5..13d4d938 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -1228,12 +1228,12 @@ alter_table_stmt }; } alter_column_suffix - = k:('after'i / 'first'i) __ i:column_ref { - return { - keyword: k, - expr: i + = k:"first"i { + return { keyword: k }; + } + / k:"after"i __ i:column_ref { + return { keyword: k, expr: i }; } - } alter_action_list = head:alter_action tail:(__ COMMA __ alter_action)* { return createList(head, tail); diff --git a/pegjs/sqlite.pegjs b/pegjs/sqlite.pegjs index 5cbdfe65..2b35e999 100644 --- a/pegjs/sqlite.pegjs +++ b/pegjs/sqlite.pegjs @@ -781,12 +781,12 @@ alter_table_stmt } alter_column_suffix - = k:('after'i / 'first'i) __ i:column_ref { - return { - keyword: k, - expr: i + = k:"first"i { + return { keyword: k }; + } + / k:"after"i __ i:column_ref { + return { keyword: k, expr: i }; } - } alter_action_list = head:alter_action tail:(__ COMMA __ alter_action)* { diff --git a/src/alter.js b/src/alter.js index 4dc0688c..530c3392 100644 --- a/src/alter.js +++ b/src/alter.js @@ -64,8 +64,10 @@ function alterExprToSQL(expr) { toUpper(prefix), name && name.trim(), dataType.filter(hasVal).join(' '), - suffix && `${toUpper(suffix.keyword)}${suffix.expr ? ` ${columnRefToSQL(suffix.expr)}` : ''}`, ] + if (suffix) { + alterArray.push(toUpper(suffix.keyword), suffix.expr && columnRefToSQL(suffix.expr)) + } return alterArray.filter(hasVal).join(' ') }