forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.js
More file actions
131 lines (123 loc) · 4.31 KB
/
Copy pathtables.js
File metadata and controls
131 lines (123 loc) · 4.31 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
import { binaryToSQL } from './binary'
import { columnRefToSQL } from './column'
import { exprToSQL } from './expr'
import { valuesToSQL } from './insert'
import { commonOptionConnector, hasVal, identifierToSql, literalToSQL, toUpper } from './util'
function unnestToSQL(unnestExpr) {
const { type, as, expr, with_offset: withOffset } = unnestExpr
const result = [
`${toUpper(type)}(${expr && exprToSQL(expr) || ''})`,
commonOptionConnector('AS', identifierToSql, as),
commonOptionConnector(
toUpper(withOffset && withOffset.keyword),
identifierToSql,
withOffset && withOffset.as
),
]
return result.filter(hasVal).join(' ')
}
function pivotOperatorToSQL(operator) {
const { as, column, expr, in_expr, type } = operator
const result = [
exprToSQL(expr),
'FOR',
columnRefToSQL(column),
binaryToSQL(in_expr),
]
const sql = [`${toUpper(type)}(${result.join(' ')})`]
if (as) sql.push('AS', identifierToSql(as))
return sql.join(' ')
}
function operatorToSQL(operator) {
if (!operator) return
const { type } = operator
switch (type) {
case 'pivot':
return pivotOperatorToSQL(operator)
default:
return ''
}
}
function tableHintToSQL(tableHintExpr) {
if (!tableHintExpr) return
const { keyword, expr, index, index_columns, parentheses, prefix } = tableHintExpr
const result = []
switch (keyword.toLowerCase()) {
case 'forceseek':
result.push(toUpper(keyword), `(${identifierToSql(index)}`, `(${index_columns.map(exprToSQL).filter(hasVal).join(', ')}))`)
break
case 'spatial_window_max_cells':
result.push(toUpper(keyword), '=', exprToSQL(expr))
break
case 'index':
result.push(toUpper(prefix), toUpper(keyword), parentheses ? `(${expr.map(identifierToSql).join(', ')})` : `= ${identifierToSql(expr)}`)
break
default:
result.push(exprToSQL(expr))
}
return result.filter(hasVal).join(' ')
}
function tableToSQL(tableInfo) {
if (toUpper(tableInfo.type) === 'UNNEST') return unnestToSQL(tableInfo)
const { table, db, as, expr, operator, prefix: prefixStr, schema, tablesample, table_hint } = tableInfo
const database = identifierToSql(db)
const schemaStr = identifierToSql(schema)
let tableName = table && identifierToSql(table)
if (expr && expr.type === 'values') {
const { parentheses, values, prefix } = expr
const valueSQL = [parentheses && '(', '', parentheses && ')']
let valuesExpr = valuesToSQL(values)
if (prefix) valuesExpr = valuesExpr.split('(').slice(1).map(val => `${toUpper(prefix)}(${val}`).join('')
valueSQL[1] = `VALUES ${valuesExpr}`
tableName = valueSQL.filter(hasVal).join('')
}
if (expr && expr.type !== 'values') tableName = exprToSQL(expr)
tableName = [toUpper(prefixStr), tableName].filter(hasVal).join(' ')
let str = [database, schemaStr, tableName].filter(hasVal).join('.')
if (tableInfo.parentheses) str = `(${str})`
const result = [str, operatorToSQL(operator)]
if (tablesample) {
const tableSampleSQL = ['TABLESAMPLE', exprToSQL(tablesample.expr), literalToSQL(tablesample.repeatable)].filter(hasVal).join(' ')
result.push(tableSampleSQL)
}
if (as) result.push('AS', identifierToSql(as))
if (table_hint) result.push(`${toUpper(table_hint.keyword)}`, `(${table_hint.expr.map(tableHintToSQL).filter(hasVal).join(', ')})`)
return result.filter(hasVal).join(' ')
}
/**
* @param {Array} tables
* @return {string}
*/
function tablesToSQL(tables) {
if (!tables) return ''
const baseTable = tables[0]
const clauses = []
if (baseTable.type === 'dual') return 'DUAL'
clauses.push(tableToSQL(baseTable))
for (let i = 1; i < tables.length; ++i) {
const joinExpr = tables[i]
const { on, using, join } = joinExpr
const str = []
str.push(join ? ` ${join}` : ',')
str.push(tableToSQL(joinExpr))
str.push(commonOptionConnector('ON', exprToSQL, on))
if (using) str.push(`USING (${using.map(identifierToSql).join(', ')})`)
clauses.push(str.filter(hasVal).join(' '))
}
return clauses.filter(hasVal).join('')
}
function tableOptionToSQL(tableOption) {
const { keyword, symbol, value } = tableOption
const sql = [keyword.toUpperCase()]
if (symbol) sql.push(symbol)
sql.push(value)
return sql.join(' ')
}
export {
operatorToSQL,
tableHintToSQL,
tablesToSQL,
tableOptionToSQL,
tableToSQL,
unnestToSQL,
}