Skip to content

Commit

Permalink
Merge pull request #1612 from taozhi8833998/fix-convert-mysql
Browse files Browse the repository at this point in the history
fix: convert fun in mysql
  • Loading branch information
taozhi8833998 authored Oct 5, 2023
2 parents 2068f92 + 5e8dbde commit 0e17e61
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2783,8 +2783,8 @@ convert_args
value: [c, { type: 'datatype', ...d, }]
}
}
/ c:(column_ref / literal_string / literal_numeric) __ KW_USING __ d:ident_name {
c.suffix = `USING ${d}`
/ c:or_and_where_expr __ KW_USING __ d:ident_name {
c.suffix = `USING ${d.toUpperCase()}`
return {
type: 'expr_list',
value: [c]
Expand Down Expand Up @@ -2874,7 +2874,7 @@ func_call
over: up
}
}
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
return {
Expand Down
6 changes: 3 additions & 3 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3075,8 +3075,8 @@ convert_args
value: [c, { type: 'datatype', ...d, }]
}
}
/ c:(column_ref / literal_string / literal_numeric) __ KW_USING __ d:ident_name {
c.suffix = `USING ${d}`
/ c:or_and_where_expr __ KW_USING __ d:ident_name {
c.suffix = `USING ${d.toUpperCase()}`
return {
type: 'expr_list',
value: [c]
Expand Down Expand Up @@ -3165,7 +3165,7 @@ func_call
over: up
}
}
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
return {
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function literalToSQL(literal) {
if (!literal) return
let { prefix } = literal
const { type, parentheses, suffix, value } = literal
let str = value
let str = typeof literal === 'string' ? literal : value
switch (type) {
case 'backticks_quote_string':
str = `\`${escape(value)}\``
Expand Down
13 changes: 13 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,13 @@ describe('mysql', () => {
"GRANT ALL ON *.* TO 'someuser'@'somehost' WITH GRANT OPTION"
]
},
{
title: 'convert using',
sql: [
`select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")) using utf8);`,
`SELECT CONVERT(json_unquote(json_extract('{"thing": "252"}', "$.thing")) USING UTF8)`
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand All @@ -801,6 +808,12 @@ describe('mysql', () => {
})
})

it('should throw error when covert args is not right', () => {
const sql = `select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")));`
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", "~", or [ \\t\\n\\r] but ")" found.')
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", "~", or [ \\t\\n\\r] but ")" found.')
})

it('should join multiple table with comma', () => {
const sql = 'SELECT * FROM t1 INNER JOIN t2 ON t1.id = t2.id, t3 AS tbl'
let ast = parser.astify(sql)
Expand Down

0 comments on commit 0e17e61

Please sign in to comment.