forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
235 lines (213 loc) · 5.14 KB
/
Copy pathutil.js
File metadata and controls
235 lines (213 loc) · 5.14 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import has from 'has'
const escapeMap = {
'\0' : '\\0',
'\'' : '\\\'',
'"' : '\\"',
'\b' : '\\b',
'\n' : '\\n',
'\r' : '\\r',
'\t' : '\\t',
'\x1a' : '\\Z',
// '\\' : '\\\\',
}
const DEFAULT_OPT = {
database : 'mysql',
type : 'table',
}
let parserOpt = DEFAULT_OPT
function commonOptionConnector(keyword, action, opt) {
if (!opt) return
if (!keyword) return action(opt)
return `${keyword.toUpperCase()} ${action(opt)}`
}
function connector(keyword, str) {
if (!str) return
return `${keyword.toUpperCase()} ${str}`
}
/**
* @param {(Array|boolean|string|number|null)} value
* @return {Object}
*/
function createValueExpr(value) {
const type = typeof value
if (Array.isArray(value)) return { type: 'expr_list', value: value.map(createValueExpr) }
if (value === null) return { type: 'null', value: null }
switch (type) {
case 'boolean':
return { type: 'bool', value }
case 'string':
return { type: 'string', value }
case 'number':
return { type: 'number', value }
default:
throw new Error(`Cannot convert value "${type}" to SQL`)
}
}
/**
* @param operator
* @param left
* @param right
* @return {Object}
*/
function createBinaryExpr(operator, left, right) {
const expr = { operator, type: 'binary_expr' }
expr.left = has(left, 'type') ? left : createValueExpr(left)
if (operator === 'BETWEEN' || operator === 'NOT BETWEEN') {
expr.right = {
type : 'expr_list',
value : [createValueExpr(right[0]), createValueExpr(right[1])],
}
return expr
}
expr.right = has(right, 'type') ? right : createValueExpr(right)
return expr
}
/**
* Replace param expressions
*
* @param {Object} ast - AST object
* @param {Object} keys - Keys = parameter names, values = parameter values
* @return {Object} - Newly created AST object
*/
function replaceParamsInner(ast, keys) {
Object.keys(ast)
.filter(key => {
const value = ast[key]
return Array.isArray(value) || (typeof value === 'object' && value !== null)
})
.forEach(key => {
const expr = ast[key]
if (!(typeof expr === 'object' && expr.type === 'param')) return replaceParamsInner(expr, keys)
if (typeof keys[expr.value] === 'undefined') throw new Error(`no value for parameter :${expr.value} found`)
ast[key] = createValueExpr(keys[expr.value])
return null
})
return ast
}
function escape(str) {
const res = []
for (let i = 0, len = str.length; i < len; ++i) {
let char = str[i]
const escaped = escapeMap[char]
if (escaped) char = escaped
res.push(char)
}
return res.join('')
}
function getParserOpt() {
return parserOpt
}
function setParserOpt(opt) {
parserOpt = opt
}
function identifierToSql(ident, isDual) {
const { database } = getParserOpt()
if (isDual === true) return `'${ident}'`
if (!ident) return
switch (database && database.toLowerCase()) {
case 'mysql':
case 'mariadb':
return `\`${ident}\``
case 'postgresql':
return `"${ident}"`
default:
return `\`${ident}\``
}
}
function literalToSQL(literal) {
const { type, parentheses, value } = literal
let str = value
switch (type) {
case 'string':
str = `'${escape(value)}'`
break
case 'boolean':
case 'bool':
str = value ? 'TRUE' : 'FALSE'
break
case 'null':
str = 'NULL'
break
case 'star':
str = '*'
break
case 'param':
str = `:${value}`
break
case 'origin':
str = value.toUpperCase()
break
case 'time':
case 'date':
case 'timestamp':
str = `${type.toUpperCase()} '${value}'`
break
default:
break
}
return parentheses ? `(${str})` : str
}
function replaceParams(ast, params) {
return replaceParamsInner(JSON.parse(JSON.stringify(ast)), params)
}
function columnRefToSQL(expr) {
const {
arrow,
column,
isDual,
table,
parentheses,
property,
} = expr
let str = column === '*' ? '*' : identifierToSql(column, isDual)
if (table) str = `${identifierToSql(table)}.${str}`
if (arrow) str = `${str} ${arrow} '${property}'`
return parentheses ? `(${str})` : str
}
function toUpper(val) {
if (!val) return
return val.toUpperCase()
}
function hasVal(val) {
return val
}
function commonTypeValue(opt) {
const result = []
if (!opt) return result
const { type, value } = opt
result.push(type.toUpperCase())
result.push(value.toUpperCase())
return result
}
function commentToSQL(comment) {
if (!comment) return
const result = []
const { keyword, symbol, value } = comment
result.push(keyword.toUpperCase())
if (symbol) result.push(symbol)
result.push(literalToSQL(value))
return result.join(' ')
}
function returningToSQL(returning) {
if (!returning) return ''
const { columns } = returning
return ['RETURNING', columns.map(columnRefToSQL).filter(hasVal).join(', ')].join(' ')
}
export {
commonOptionConnector,
connector,
commonTypeValue,
columnRefToSQL,
commentToSQL,
createBinaryExpr,
createValueExpr,
DEFAULT_OPT,
escape,
literalToSQL,
identifierToSql,
replaceParams,
returningToSQL,
hasVal,
setParserOpt,
toUpper,
}