Skip to content

Commit 7b09356

Browse files
Merge pull request taozhi8833998#783 from taozhi8833998/bug-func-mysql
bug: support left function name and convert function expr in mysql
2 parents 4d5d595 + 15d42b2 commit 7b09356

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

pegjs/mysql.pegjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,30 @@ count_arg
20872087
star_expr
20882088
= "*" { return { type: 'star', value: '*' }; }
20892089

2090+
convert_args
2091+
= c:column_ref __ COMMA __ d:data_type {
2092+
return {
2093+
type: 'expr_list',
2094+
value: [c, { value: d.dataType.toUpperCase() }]
2095+
}
2096+
}
2097+
/ c:column_ref __ KW_USING __ d:ident_name {
2098+
c.suffix = `USING ${d}`
2099+
return {
2100+
type: 'expr_list',
2101+
value: [c]
2102+
}
2103+
}
2104+
20902105
func_call
2091-
= name:proc_func_name __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
2106+
= 'convert'i __ LPAREN __ l:convert_args __ RPAREN {
2107+
return {
2108+
type: 'function',
2109+
name: 'CONVERT',
2110+
args: l
2111+
};
2112+
}
2113+
/ name:proc_func_name __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
20922114
return {
20932115
type: 'function',
20942116
name: name,
@@ -2637,6 +2659,7 @@ proc_func_name
26372659
}
26382660
return name;
26392661
}
2662+
/ KW_LEFT { return 'LEFT' }
26402663

26412664
proc_func_call
26422665
= name:proc_func_name __ LPAREN __ l:proc_primary_list? __ RPAREN {

src/column.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function columnOffsetToSQL(column, isDual) {
2222
function columnRefToSQL(expr) {
2323
const {
2424
arrow, as, collate, column, isDual, table, parentheses, property,
25+
suffix,
2526
} = expr
2627
let str = column === '*' ? '*' : columnOffsetToSQL(column, isDual)
2728
if (table) str = `${identifierToSql(table)}.${str}`
@@ -31,6 +32,7 @@ function columnRefToSQL(expr) {
3132
commonOptionConnector(arrow, literalToSQL, property),
3233
]
3334
if (collate) result.push(commonTypeValue(collate).join(' '))
35+
result.push(toUpper(suffix))
3436
const sql = result.filter(hasVal).join(' ')
3537
return parentheses ? `(${sql})` : sql
3638
}

src/expr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const exprToSQLConvertFn = {
1919
window_func : windowFuncToSQL,
2020
'array' : arrayStructExprToSQL,
2121
assign : assignToSQL,
22-
extract : extractFunToSQL,
2322
binary_expr : binaryToSQL,
2423
case : caseToSQL,
2524
cast : castToSQL,
2625
column_ref : columnRefToSQL,
26+
extract : extractFunToSQL,
2727
function : funcToSQL,
2828
interval : intervalToSQL,
2929
show : showToSQL,

test/select.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ describe('select', () => {
103103

104104
it('should have appropriate types', () => {
105105
const ast = parser.astify('SELECT SQL_NO_CACHE DISTINCT a FROM b WHERE c = 0 GROUP BY d ORDER BY e limit 3');
106-
107106
expect(ast.options).to.be.an('array');
108107
expect(ast.distinct).to.equal('DISTINCT');
109108
expect(ast.from).to.be.an('array');
@@ -669,6 +668,14 @@ describe('select', () => {
669668
expect(ast.orderby).to.be.null;
670669
expect(ast.limit).to.be.null;
671670
})
671+
it('should support left and covert fun', () => {
672+
expect(getParsedSql(`select * from test where LEFT(column,2)="ts";`))
673+
.to.equal("SELECT * FROM `test` WHERE LEFT(`column`, 2) = 'ts'")
674+
expect(getParsedSql(`select * from test where CONVERT(column, DATE)="test";`))
675+
.to.equal("SELECT * FROM `test` WHERE CONVERT(`column`, DATE) = 'test'")
676+
expect(getParsedSql(`select * from test where CONVERT(column using utf8)="test";`))
677+
.to.equal("SELECT * FROM `test` WHERE CONVERT(`column` USING UTF8) = 'test'")
678+
})
672679
})
673680

674681
describe('limit clause', () => {

0 commit comments

Comments
 (0)