Skip to content

Commit

Permalink
Merge pull request #2195 from taozhi8833998/refactor-definer-mysql
Browse files Browse the repository at this point in the history
refactor: change definer expr in mysql
  • Loading branch information
taozhi8833998 authored Oct 27, 2024
2 parents a823aec + 96c75c8 commit 227b9fb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
27 changes: 21 additions & 6 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3712,6 +3727,7 @@ literal_string
value: ca[1].join('')
};
}


literal_datetime
= type:(KW_TIME / KW_DATE / KW_TIMESTAMP / KW_DATETIME) __ ca:("'" single_char* "'") {
Expand Down
4 changes: 2 additions & 2 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 227b9fb

Please sign in to comment.