forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.js
More file actions
152 lines (139 loc) · 5.5 KB
/
func.js
File metadata and controls
152 lines (139 loc) · 5.5 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
import { arrayIndexToSQL, columnOffsetToSQL } from './column'
import { exprToSQL, orderOrPartitionByToSQL } from './expr'
import { hasVal, identifierToSql, literalToSQL, toUpper } from './util'
import { overToSQL } from './over'
function anyValueFuncToSQL(stmt) {
const { args, type, over } = stmt
const { expr, having } = args
let sql = `${toUpper(type)}(${exprToSQL(expr)}`
if (having) sql = `${sql} HAVING ${toUpper(having.prefix)} ${exprToSQL(having.expr)}`
sql = `${sql})`
const overStr = overToSQL(over)
return [sql, overStr].filter(hasVal).join(' ')
}
function arrayDimensionToSymbol(target) {
if (!target || !target.array) return ''
const { keyword } = target.array
if (keyword) return toUpper(keyword)
const { dimension, length } = target.array
const result = []
for (let i = 0; i < dimension; i++) {
result.push('[')
if (length && length[i]) result.push(literalToSQL(length[i]))
result.push(']')
}
return result.join('')
}
function castToSQL(expr) {
const { target: targets, expr: expression, keyword, symbol, as: alias, offset, parentheses: outParentheses } = expr
let prefix = columnOffsetToSQL({ expr: expression, offset })
const result = []
for (let i = 0, len = targets.length; i < len; ++i) {
const target = targets[i]
const { angle_brackets: angleBrackets, length, dataType, parentheses, quoted, scale, suffix: dataTypeSuffix, expr: targetExpr } = target
let str = targetExpr ? exprToSQL(targetExpr) : ''
if (length != null) str = scale ? `${length}, ${scale}` : length
if (parentheses) str = `(${str})`
if (angleBrackets) str = `<${str}>`
if (dataTypeSuffix && dataTypeSuffix.length) str += ` ${dataTypeSuffix.map(literalToSQL).join(' ')}`
let symbolChar = '::'
let suffix = ''
const targetResult = []
if (symbol === 'as') {
if (i === 0) prefix = `${toUpper(keyword)}(${prefix}`
suffix = ')'
symbolChar = ` ${symbol.toUpperCase()} `
}
if (i === 0) targetResult.push(prefix)
const arrayDimension = arrayDimensionToSymbol(target)
targetResult.push(symbolChar, quoted, dataType, quoted, arrayDimension, str, suffix)
result.push(targetResult.filter(hasVal).join(''))
}
if (alias) result.push(` AS ${identifierToSql(alias)}`)
const sql = result.filter(hasVal).join('')
return outParentheses ? `(${sql})` : sql
}
function extractFunToSQL(stmt) {
const { args, type } = stmt
const { field, cast_type: castType, source } = args
const result = [`${toUpper(type)}(${toUpper(field)}`, 'FROM', toUpper(castType), exprToSQL(source)]
return `${result.filter(hasVal).join(' ')})`
}
function flattenArgToSQL(arg) {
if (!arg) return ''
const { type, symbol, value } = arg
const result = [toUpper(type), symbol, exprToSQL(value)]
return result.filter(hasVal).join(' ')
}
function jsonObjectArgToSQL(argExpr) {
const { expr } = argExpr
const { key, value, on } = expr
const result = [exprToSQL(key), 'VALUE', exprToSQL(value)]
if (on) result.push('ON', 'NULL', exprToSQL(on))
return result.filter(hasVal).join(' ')
}
function flattenFunToSQL(stmt) {
const { args, type } = stmt
const keys = ['input', 'path', 'outer', 'recursive', 'mode']
const argsStr = keys.map(key => flattenArgToSQL(args[key])).filter(hasVal).join(', ')
return `${toUpper(type)}(${argsStr})`
}
function funcArgToSQL(argExpr) {
const { name, symbol, expr } = argExpr.value
return [name, symbol, exprToSQL(expr)].filter(hasVal).join(' ')
}
function withinGroupToSQL(stmt) {
if (!stmt) return ''
const { type, keyword, orderby } = stmt
return [toUpper(type), toUpper(keyword), `(${orderOrPartitionByToSQL(orderby, 'order by')})`].filter(hasVal).join(' ')
}
function funcToSQL(expr) {
const { args, array_index, name, args_parentheses, parentheses, within_group: withinGroup, over, suffix } = expr
const overStr = overToSQL(over)
const withinGroupStr = withinGroupToSQL(withinGroup)
const suffixStr = exprToSQL(suffix)
const funcName = [literalToSQL(name.schema), name.name.map(literalToSQL).join('.')].filter(hasVal).join('.')
if (!args) return [funcName, withinGroupStr, overStr].filter(hasVal).join(' ')
let separator = expr.separator || ', '
if (toUpper(funcName) === 'TRIM') separator = ' '
let str = [funcName]
str.push(args_parentheses === false ? ' ' : '(')
const argsList = exprToSQL(args)
if (Array.isArray(separator)) {
let argsSQL = argsList[0]
for (let i = 1, len = argsList.length; i < len; ++i) {
argsSQL = [argsSQL, argsList[i]].join(` ${exprToSQL(separator[i - 1])} `)
}
str.push(argsSQL)
} else {
str.push(argsList.join(separator))
}
if (args_parentheses !== false) str.push(')')
str.push(arrayIndexToSQL(array_index))
str = [str.join(''), suffixStr].filter(hasVal).join(' ')
return [parentheses ? `(${str})` : str, withinGroupStr, overStr].filter(hasVal).join(' ')
}
function tablefuncFunToSQL(expr) {
const { as, name, args } = expr
const funcName = [literalToSQL(name.schema), name.name.map(literalToSQL).join('.')].filter(hasVal).join('.')
const result = [`${funcName}(${exprToSQL(args).join(', ')})`, 'AS', funcToSQL(as)]
return result.join(' ')
}
function lambdaToSQL(stmt) {
const { args, expr } = stmt
const { value, parentheses } = args
const argsList = value.map(exprToSQL).join(', ')
return [parentheses ? `(${argsList})` : argsList, '->', exprToSQL(expr)].join(' ')
}
export {
anyValueFuncToSQL,
arrayDimensionToSymbol,
castToSQL,
extractFunToSQL,
flattenFunToSQL,
funcArgToSQL,
funcToSQL,
jsonObjectArgToSQL,
lambdaToSQL,
tablefuncFunToSQL,
}