From 96c75c89a39fb7130a293b286c8958bd63827b38 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Sat, 26 Oct 2024 18:36:10 +0800 Subject: [PATCH] refactor: change definer expr in mysql --- pegjs/mariadb.pegjs | 27 +++++++++++++++++++++------ pegjs/mysql.pegjs | 28 ++++++++++++++++++++++------ src/create.js | 4 ++-- test/create.spec.js | 6 ++++-- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index af942c4b..aeaf1c27 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -740,16 +740,31 @@ create_column_definition } trigger_definer - = 'DEFINER'i __ KW_ASSIGIN_EQUAL __ u:literal_string __ '@' __ h:literal_string { - const userNameSymbol = u.type === 'single_quote_string' ? '\'' : '"' - const hostSymbol = h.type === 'single_quote_string' ? '\'' : '"' - return `DEFINER = ${userNameSymbol}${u.value}${userNameSymbol}@${hostSymbol}${h.value}${hostSymbol}` + = 'DEFINER'i __ KW_ASSIGIN_EQUAL __ u:(backticks_quoted_ident / literal_string) __ '@' __ h:(backticks_quoted_ident / literal_string) { + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = createBinaryExpr(u, '@', h) + return createBinaryExpr(operator, left, right) } / 'DEFINER'i __ KW_ASSIGIN_EQUAL __ KW_CURRENT_USER __ LPAREN __ RPAREN { - return `DEFINER = CURRENT_USER()` + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = { + type: 'function', + name: { name: [{ type: 'default', value: 'current_user' }] }, + args:{ type: 'expr_list', value: [] }, + } + return createBinaryExpr(operator, left, right) } / 'DEFINER'i __ KW_ASSIGIN_EQUAL __ KW_CURRENT_USER { - return `DEFINER = CURRENT_USER` + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = { + type: 'function', + name: { name: [{ type: 'default', value: 'current_user' }] }, + args:{ type: 'expr_list', value: [] }, + } + return createBinaryExpr(operator, left, right) } trigger_time = 'BEFORE'i / 'AFTER'i diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index ff476de2..73d9aa8f 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -940,16 +940,31 @@ create_column_definition } trigger_definer - = 'DEFINER'i __ KW_ASSIGIN_EQUAL __ u:literal_string __ '@' __ h:literal_string { - const userNameSymbol = u.type === 'single_quote_string' ? '\'' : '"' - const hostSymbol = h.type === 'single_quote_string' ? '\'' : '"' - return `DEFINER = ${userNameSymbol}${u.value}${userNameSymbol}@${hostSymbol}${h.value}${hostSymbol}` + = 'DEFINER'i __ KW_ASSIGIN_EQUAL __ u:(backticks_quoted_ident / literal_string) __ '@' __ h:(backticks_quoted_ident / literal_string) { + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = createBinaryExpr('@', u, h) + return createBinaryExpr(operator, left, right) } / 'DEFINER'i __ KW_ASSIGIN_EQUAL __ KW_CURRENT_USER __ LPAREN __ RPAREN { - return `DEFINER = CURRENT_USER()` + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = { + type: 'function', + name: { name: [{ type: 'default', value: 'current_user' }] }, + args:{ type: 'expr_list', value: [] }, + } + return createBinaryExpr(operator, left, right) } / 'DEFINER'i __ KW_ASSIGIN_EQUAL __ KW_CURRENT_USER { - return `DEFINER = CURRENT_USER` + const left = { type: 'origin', value: 'definer' } + const operator = '=' + const right = { + type: 'function', + name: { name: [{ type: 'default', value: 'current_user' }] }, + args:{ type: 'expr_list', value: [] }, + } + return createBinaryExpr(operator, left, right) } trigger_time = 'BEFORE'i / 'AFTER'i @@ -3712,6 +3727,7 @@ literal_string value: ca[1].join('') }; } + literal_datetime = type:(KW_TIME / KW_DATE / KW_TIMESTAMP / KW_DATETIME) __ ca:("'" single_char* "'") { diff --git a/src/create.js b/src/create.js index 2e889579..c3665a31 100644 --- a/src/create.js +++ b/src/create.js @@ -109,7 +109,7 @@ function createTriggerToSQL(stmt) { order: triggerOrder, time: triggerTime, when, } = stmt const sql = [ - toUpper(type), toUpper(temporary), definer, toUpper(keyword), + toUpper(type), toUpper(temporary), exprToSQL(definer), toUpper(keyword), toUpper(ife), tableToSQL(trigger), toUpper(triggerTime), triggerEvents.map(event => { @@ -252,7 +252,7 @@ function createViewToSQL(stmt) { toUpper(temporary), toUpper(recursive), algorithm && `ALGORITHM = ${toUpper(algorithm)}`, - definer, + exprToSQL(definer), sqlSecurity && `SQL SECURITY ${toUpper(sqlSecurity)}`, toUpper(keyword), toUpper(ifNotExists), diff --git a/test/create.spec.js b/test/create.spec.js index 7e69bf88..38cd9c19 100644 --- a/test/create.spec.js +++ b/test/create.spec.js @@ -603,8 +603,10 @@ describe('create', () => { expect(getParsedSql("CREATE VIEW v (mycol) AS SELECT 'abc'")).to.equal("CREATE VIEW `v` (`mycol`) AS SELECT 'abc'") }) it('should support optional setting', () => { - expect(getParsedSql('CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = "abc"@"localhost" SQL SECURITY INVOKER VIEW test.v AS SELECT * FROM t WITH CHECK OPTION;')).to.equal('CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = "abc"@"localhost" SQL SECURITY INVOKER VIEW `test`.`v` AS SELECT * FROM `t` WITH CHECK OPTION') - expect(getParsedSql('CREATE OR REPLACE ALGORITHM = MERGE DEFINER = \'abc\'@\'localhost\' SQL SECURITY INVOKER VIEW test.v AS SELECT * FROM t WITH CASCADED CHECK OPTION;')).to.equal('CREATE OR REPLACE ALGORITHM = MERGE DEFINER = \'abc\'@\'localhost\' SQL SECURITY INVOKER VIEW `test`.`v` AS SELECT * FROM `t` WITH CASCADED CHECK OPTION') + expect(getParsedSql('CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = "abc"@"localhost" SQL SECURITY INVOKER VIEW test.v AS SELECT * FROM t WITH CHECK OPTION;')).to.equal('CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = "abc" @ "localhost" SQL SECURITY INVOKER VIEW `test`.`v` AS SELECT * FROM `t` WITH CHECK OPTION') + expect(getParsedSql('CREATE OR REPLACE ALGORITHM = MERGE DEFINER = \'abc\'@\'localhost\' SQL SECURITY INVOKER VIEW test.v AS SELECT * FROM t WITH CASCADED CHECK OPTION;')).to.equal('CREATE OR REPLACE ALGORITHM = MERGE DEFINER = \'abc\' @ \'localhost\' SQL SECURITY INVOKER VIEW `test`.`v` AS SELECT * FROM `t` WITH CASCADED CHECK OPTION') + const sql = 'create algorithm=merge definer=`root`@`localhost` sql security definer view `viewname` as select abc' + expect(getParsedSql(sql)).to.equal('CREATE ALGORITHM = MERGE DEFINER = `root` @ `localhost` SQL SECURITY DEFINER VIEW `viewname` AS SELECT `abc`') }) }) it('throw error when create type is unknown', () => {