forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.js
More file actions
73 lines (64 loc) · 1.96 KB
/
expr.js
File metadata and controls
73 lines (64 loc) · 1.96 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
import { literalToSQL, columnRefToSQL } from './util'
import { alterExprToSQL } from './alter'
import { aggrToSQL } from './aggregation'
import { assignToSQL } from './assign'
import { binaryToSQL } from './binary'
import { caseToSQL } from './case'
import { castToSQL, funcToSQL } from './func'
import { intervalToSQL } from './interval'
import { selectToSQL } from './select'
import { unionToSQL } from './union'
const exprToSQLConvertFn = {
alter : alterExprToSQL,
aggr_func : aggrToSQL,
assign : assignToSQL,
binary_expr : binaryToSQL,
case : caseToSQL,
cast : castToSQL,
column_ref : columnRefToSQL,
function : funcToSQL,
interval : intervalToSQL,
}
function varToSQL(expr) {
const { prefix = '@', name, members, keyword } = expr
const val = []
if (keyword) val.push(keyword)
const varName = members && members.length > 0 ? `${name}.${members.join('.')}` : name
val.push(`${prefix || ''}${varName}`)
return val.join(' ')
}
exprToSQLConvertFn.var = varToSQL
function exprToSQL(exprOrigin) {
const expr = exprOrigin
if (exprOrigin.ast) {
const { ast } = expr
Reflect.deleteProperty(expr, ast)
for (const key of Object.keys(ast)) {
expr[key] = ast[key]
}
}
return exprToSQLConvertFn[expr.type] ? exprToSQLConvertFn[expr.type](expr) : literalToSQL(expr)
}
function unaryToSQL(expr) {
const str = `${expr.operator} ${exprToSQL(expr.expr)}`
return expr.parentheses ? `(${str})` : str
}
function getExprListSQL(exprList) {
if (!exprList) return []
return exprList.map(exprToSQL)
}
exprToSQLConvertFn.expr_list = expr => {
const str = getExprListSQL(expr.value)
return expr.parentheses ? `(${str})` : str
}
exprToSQLConvertFn.select = expr => {
const str = typeof expr._next === 'object' ? unionToSQL(expr) : selectToSQL(expr)
return expr.parentheses ? `(${str})` : str
}
exprToSQLConvertFn.unary_expr = unaryToSQL
export {
exprToSQLConvertFn,
exprToSQL,
getExprListSQL,
varToSQL,
}