Skip to content

Commit

Permalink
Merge pull request #1858 from taozhi8833998/fix-collate
Browse files Browse the repository at this point in the history
fix: support collate after multiple conditions in brackets in mysql
  • Loading branch information
taozhi8833998 authored Apr 10, 2024
2 parents 3944ff4 + 7f22270 commit d4a5fd7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
11 changes: 7 additions & 4 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,10 @@ on_clause
= KW_ON __ e:or_and_expr { return e; }

where_clause
= KW_WHERE __ e:(or_and_where_expr) { return e; }
= KW_WHERE __ e:or_and_where_expr __ ca:collate_expr? {
if (ca) e.suffix = [ca]
return e;
}

group_by_clause
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
Expand Down Expand Up @@ -2742,9 +2745,9 @@ primary
/ column_ref
/ param
/ LPAREN __ list:or_and_where_expr __ RPAREN {
list.parentheses = true;
return list;
}
list.parentheses = true;
return list
}
/ var_decl
/ __ prepared_symbol:'?' {
return {
Expand Down
11 changes: 7 additions & 4 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,10 @@ on_clause
= KW_ON __ e:or_and_expr { return e; }

where_clause
= KW_WHERE __ e:or_and_where_expr { return e; }
= KW_WHERE __ e:or_and_where_expr __ ca:collate_expr? {
if (ca) e.suffix = [ca]
return e;
}

group_by_clause
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
Expand Down Expand Up @@ -3025,9 +3028,9 @@ primary
/ column_ref
/ param
/ LPAREN __ list:or_and_where_expr __ RPAREN {
list.parentheses = true;
return list;
}
list.parentheses = true
return list
}
/ var_decl
/ __ prepared_symbol:'?' {
return {
Expand Down
15 changes: 13 additions & 2 deletions src/binary.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { exprToSQL } from './expr'
import { hasVal, toUpper } from './util'
import { commonTypeValue, hasVal, toUpper } from './util'

function binaryToSQL(expr) {
let operator = expr.operator || expr.op
Expand All @@ -25,7 +25,18 @@ function binaryToSQL(expr) {
}
const escape = expr.right.escape || {}
const str = [exprToSQL(expr.left), operator, rstr, toUpper(escape.type), exprToSQL(escape.value)].filter(hasVal).join(' ')
return expr.parentheses ? `(${str})` : str
const result = [expr.parentheses ? `(${str})` : str]
const { suffix } = expr
if (!suffix) return result.join(' ')
for (const suffixItem of suffix) {
const { type } = suffixItem
switch (type) {
case 'collate':
result.push(commonTypeValue(suffixItem).join(' '))
break
}
}
return result.filter(hasVal).join(' ')
}

export {
Expand Down
9 changes: 8 additions & 1 deletion test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,14 @@ describe('mysql', () => {
'show index from user',
'SHOW INDEX FROM `user`'
]
}
},
{
title: 'collate in where clause include parentheses',
sql: [
"SELECT * FROM product WHERE (id = '1' OR id = '2') COLLATE utf8mb4_general_ci;",
"SELECT * FROM `product` WHERE (`id` = '1' OR `id` = '2') COLLATE UTF8MB4_GENERAL_CI"
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit d4a5fd7

Please sign in to comment.