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
108 lines (98 loc) · 3.96 KB
/
func.js
File metadata and controls
108 lines (98 loc) · 3.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
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
import { exprToSQL } from './expr'
import { commonOptionConnector, commonTypeValue, 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 ''
switch (target.array) {
case 'one':
return '[]'
case 'two':
return '[][]'
}
}
function castToSQL(expr) {
const { arrows = [], collate, target, expr: expression, keyword, symbol, as: alias, properties = [] } = expr
const { 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 (dataTypeSuffix && dataTypeSuffix.length) str += ` ${dataTypeSuffix.join(' ')}`
let prefix = exprToSQL(expression)
let symbolChar = '::'
let suffix = ''
if (symbol === 'as') {
prefix = `${toUpper(keyword)}(${prefix}`
suffix = ')'
symbolChar = ` ${symbol.toUpperCase()} `
}
suffix += arrows.map((arrow, index) => commonOptionConnector(arrow, literalToSQL, properties[index])).join(' ')
if (alias) suffix += ` AS ${identifierToSql(alias)}`
if (collate) suffix += ` ${commonTypeValue(collate).join(' ')}`
const arrayDimension = arrayDimensionToSymbol(target)
const result = [prefix, symbolChar, quoted, dataType, quoted, arrayDimension, str, suffix]
return result.filter(hasVal).join('')
}
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 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 funcToSQL(expr) {
const { args, name, args_parentheses, parentheses, over, collate, suffix } = expr
const collateStr = commonTypeValue(collate).join(' ')
const overStr = overToSQL(over)
const suffixStr = exprToSQL(suffix)
const funcName = [literalToSQL(name.schema), name.name.map(literalToSQL).join('.')].filter(hasVal).join('.')
if (!args) return [funcName, overStr].filter(hasVal).join(' ')
let separator = expr.separator || ', '
if (toUpper(funcName) === 'TRIM') separator = ' '
let str = [funcName]
str.push(args_parentheses === false ? ' ' : '(')
str.push(exprToSQL(args).join(separator))
if (args_parentheses !== false) str.push(')')
str = [str.join(''), suffixStr].filter(hasVal).join(' ')
return [parentheses ? `(${str})` : str, collateStr, 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,
castToSQL,
extractFunToSQL,
flattenFunToSQL,
funcToSQL,
lambdaToSQL,
tablefuncFunToSQL,
}