Skip to content

Commit feb692a

Browse files
committed
refactor: update mysql reserved word
1 parent 3a1b40e commit feb692a

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

pegjs/mysql.pegjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,9 +1746,6 @@ set_item
17461746
= tbl:(ident __ DOT)? __ c:column __ '=' __ v:additive_expr {
17471747
return { column: c, value: v, table: tbl && tbl[0] };
17481748
}
1749-
/ tbl:(ident __ DOT)? __ c:column __ '=' __ KW_VALUES __ LPAREN __ v:column_ref __ RPAREN {
1750-
return { column: c, value: v, table: tbl && tbl[0], keyword: 'values' };
1751-
}
17521749

17531750
insert_value_clause
17541751
= value_clause
@@ -2840,7 +2837,11 @@ proc_func_name
28402837
}
28412838
return name;
28422839
}
2843-
/ ident_name
2840+
/ n:ident_name {
2841+
const upperName = n.toUpperCase()
2842+
if (reservedMap[upperName] === true) return upperName
2843+
return n
2844+
}
28442845
/ quoted_ident
28452846

28462847
proc_func_call

src/update.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tablesToSQL } from './tables'
22
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
33
import { limitToSQL } from './limit'
4-
import { hasVal, identifierToSql, commonOptionConnector, returningToSQL, toUpper } from './util'
4+
import { hasVal, identifierToSql, commonOptionConnector, returningToSQL } from './util'
55

66
/**
77
* @param {Array} sets
@@ -11,15 +11,14 @@ function setToSQL(sets) {
1111
if (!sets || sets.length === 0) return ''
1212
const clauses = []
1313
for (const set of sets) {
14-
const { table, column, value, keyword } = set
14+
const { table, column, value } = set
1515
const str = [table, column].filter(hasVal).map(info => identifierToSql(info)).join('.')
1616
const setItem = [str]
1717
let val = ''
1818
if (value) {
1919
val = exprToSQL(value)
2020
setItem.push('=', val)
2121
}
22-
if (keyword) setItem[2] = `${toUpper(keyword)}(${val})`
2322
clauses.push(setItem.filter(hasVal).join(' '))
2423
}
2524
return clauses.join(', ')

test/update.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ describe('update', () => {
200200
});
201201
})
202202

203+
it('should support set value', () => {
204+
expect(getParsedSql('update a set id = 123, name = values(abc) where age > 15 order by name'))
205+
.to.be.equal('UPDATE `a` SET `id` = 123, `name` = VALUES(`abc`) WHERE `age` > 15 ORDER BY `name` ASC')
206+
})
207+
203208
it('should support order by and limit in update sql', () => {
204209
expect(getParsedSql('update a set id = 123 where age > 15 order by name')).to.be.equal('UPDATE `a` SET `id` = 123 WHERE `age` > 15 ORDER BY `name` ASC')
205210
expect(getParsedSql('update a set id = 123 order by name')).to.be.equal('UPDATE `a` SET `id` = 123 ORDER BY `name` ASC')

0 commit comments

Comments
 (0)