Skip to content

Commit

Permalink
feat: support like param in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Nov 12, 2023
1 parent 71319b2 commit d16d953
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
10 changes: 6 additions & 4 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2471,10 +2471,12 @@ in_op
/ KW_IN

like_op_right
= op:like_op __ right:(literal / comparison_expr) __ es:escape_op? {
if (es) right.escape = es
return { op: op, right: right };
}
= op:like_op __ right:(literal / param / comparison_expr ) __ ca:(__ collate_expr)? __ es:escape_op? {
if (es) right.escape = es
if (ca) right.suffix = { collate: ca[1] }
return { op: op, right: right };
}


regexp_op_right
= op:regexp_op __ b:'BINARY'i? __ e:(func_call / literal_string / column_ref) {
Expand Down
5 changes: 2 additions & 3 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2759,8 +2759,9 @@ regexp_op_right
}

like_op_right
= op:like_op __ right:(literal / comparison_expr) __ es:escape_op? {
= op:like_op __ right:(literal / param / comparison_expr ) __ ca:(__ collate_expr)? __ es:escape_op? {
if (es) right.escape = es
if (ca) right.suffix = { collate: ca[1] }
return { op: op, right: right };
}

Expand Down Expand Up @@ -3679,8 +3680,6 @@ KW_COMMENT = "COMMENT"i !ident_start { return 'COMMENT'; }
KW_CONSTRAINT = "CONSTRAINT"i !ident_start { return 'CONSTRAINT'; }
KW_REFERENCES = "REFERENCES"i !ident_start { return 'REFERENCES'; }



// MySQL extensions to SQL
OPT_SQL_CALC_FOUND_ROWS = "SQL_CALC_FOUND_ROWS"i
OPT_SQL_CACHE = "SQL_CACHE"i
Expand Down
12 changes: 12 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,18 @@ describe('mysql', () => {
'SELECT -`foo` > 0 ; SELECT +`foo` > 0 ; SELECT ~`foo` > 0 ; SELECT !1 > 0'
]
},
{
title: 'like pattern',
sql: [
`SELECT
*
FROM
test
WHERE
name LIKE :pattern COLLATE utf8mb4_general_ci`,
'SELECT * FROM `test` WHERE `name` LIKE :pattern COLLATE UTF8MB4_GENERAL_CI'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit d16d953

Please sign in to comment.