forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
40 lines (37 loc) · 1.07 KB
/
update.js
File metadata and controls
40 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { tablesToSQL } from './tables'
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
import { limitToSQL } from './limit'
import { hasVal, identifierToSql, commonOptionConnector, returningToSQL } from './util'
/**
* @param {Array} sets
* @return {string}
*/
function setToSQL(sets) {
if (!sets || sets.length === 0) return ''
const clauses = []
for (const set of sets) {
let str = ''
const { table, column, value } = set
str = [table, column].filter(hasVal).map(info => identifierToSql(info)).join('.')
if (value) str = `${str} = ${exprToSQL(value)}`
clauses.push(str)
}
return clauses.join(', ')
}
function updateToSQL(stmt) {
const { table, set, where, orderby, limit, returning } = stmt
const clauses = [
'UPDATE',
tablesToSQL(table),
commonOptionConnector('SET', setToSQL, set),
commonOptionConnector('WHERE', exprToSQL, where),
orderOrPartitionByToSQL(orderby, 'order by'),
limitToSQL(limit),
returningToSQL(returning),
]
return clauses.filter(hasVal).join(' ')
}
export {
updateToSQL,
setToSQL,
}