forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalter.js
More file actions
191 lines (181 loc) · 5.82 KB
/
alter.js
File metadata and controls
191 lines (181 loc) · 5.82 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
import { columnDefinitionToSQL, columnRefToSQL, columnsToSQL } from './column'
import { createDefinitionToSQL } from './create'
import { indexTypeAndOptionToSQL } from './index-definition'
import { tablesToSQL, tableToSQL } from './tables'
import { exprToSQL } from './expr'
import { selectToSQL } from './select'
import { dataTypeToSQL, hasVal, toUpper, identifierToSql, literalToSQL } from './util'
function alterExprPartition(action, expr) {
switch (action) {
case 'add':
const sql = expr.map(({ name, value }) => ['PARTITION', literalToSQL(name), 'VALUES', toUpper(value.type), `(${literalToSQL(value.expr)})`].join(' ')).join(', ')
return `(${sql})`
default:
return columnsToSQL(expr)
}
}
function alterExprToSQL(expr) {
if (!expr) return ''
const {
action,
create_definitions: createDefinition,
if_not_exists: ifNotExists, keyword,
if_exists: ifExists,
old_column: oldColumn,
prefix,
resource,
symbol,
suffix,
} = expr
let name = ''
let dataType = []
switch (resource) {
case 'column':
dataType = [columnDefinitionToSQL(expr)]
break
case 'index':
dataType = indexTypeAndOptionToSQL(expr)
name = expr[resource]
break
case 'table':
case 'schema':
name = identifierToSql(expr[resource])
break
case 'aggregate':
case 'function':
case 'domain':
case 'type':
name = identifierToSql(expr[resource])
break
case 'algorithm':
case 'lock':
case 'table-option':
name = [symbol, toUpper(expr[resource])].filter(hasVal).join(' ')
break
case 'constraint':
name = identifierToSql(expr[resource])
dataType = [createDefinitionToSQL(createDefinition)]
break
case 'partition':
dataType = [alterExprPartition(action, expr.partitions)]
break
case 'key':
name = identifierToSql(expr[resource])
break
default:
name = [symbol, expr[resource]].filter(val => val !== null).join(' ')
break
}
const alterArray = [
toUpper(action),
toUpper(keyword),
toUpper(ifNotExists),
toUpper(ifExists),
oldColumn && columnRefToSQL(oldColumn),
toUpper(prefix),
name && name.trim(),
dataType.filter(hasVal).join(' '),
]
if (suffix) {
alterArray.push(toUpper(suffix.keyword), suffix.expr && columnRefToSQL(suffix.expr))
}
return alterArray.filter(hasVal).join(' ')
}
function alterTableToSQL(stmt) {
const { type, table, if_exists, prefix, expr = [] } = stmt
const action = toUpper(type)
const tableName = tablesToSQL(table)
const exprList = expr.map(exprToSQL)
const result = [action, 'TABLE', toUpper(if_exists), literalToSQL(prefix), tableName, exprList.join(', ')]
return result.filter(hasVal).join(' ')
}
function alterViewToSQL(stmt) {
const { type, columns, attributes, select, view, with: withExpr } = stmt
const action = toUpper(type)
const viewName = tableToSQL(view)
const result = [action, 'VIEW', viewName]
if (columns) result.push(`(${columns.map(columnRefToSQL).join(', ')})`)
if (attributes) result.push(`WITH ${attributes.map(toUpper).join(', ')}`)
result.push('AS', selectToSQL(select))
if (withExpr) result.push(toUpper(withExpr))
return result.filter(hasVal).join(' ')
}
function alterArgsToSQL(arg) {
const defaultSQL = arg.default && [toUpper(arg.default.keyword), exprToSQL(arg.default.value)].join(' ')
return [toUpper(arg.mode), arg.name, dataTypeToSQL(arg.type), defaultSQL].filter(hasVal).join(' ')
}
function alterSchemaToSQL(stmt) {
const { expr, keyword, schema, type } = stmt
const result = [toUpper(type), toUpper(keyword), identifierToSql(schema), alterExprToSQL(expr)]
return result.filter(hasVal).join(' ')
}
function alterDomainTypeToSQL(stmt) {
const { expr, keyword, name, type } = stmt
const result = [
toUpper(type),
toUpper(keyword),
[identifierToSql(name.schema), identifierToSql(name.name)].filter(hasVal).join('.'),
alterExprToSQL(expr),
]
return result.filter(hasVal).join(' ')
}
function alterFunctionToSQL(stmt) {
const { args, expr, keyword, name, type } = stmt
const result = [
toUpper(type),
toUpper(keyword),
[
[identifierToSql(name.schema), identifierToSql(name.name)].filter(hasVal).join('.'),
args && `(${args.expr ? args.expr.map(alterArgsToSQL).join(', ') : ''})`,
].filter(hasVal).join(''),
alterExprToSQL(expr),
]
return result.filter(hasVal).join(' ')
}
function alterAggregateToSQL(stmt) {
const { args, expr, keyword, name, type } = stmt
const { expr: argsExpr, orderby } = args
const result = [
toUpper(type),
toUpper(keyword),
[
[identifierToSql(name.schema), identifierToSql(name.name)].filter(hasVal).join('.'),
`(${argsExpr.map(alterArgsToSQL).join(', ')}${orderby ? [' ORDER', 'BY', orderby.map(alterArgsToSQL).join(', ')].join(' ') : ''})`,
].filter(hasVal).join(''),
alterExprToSQL(expr),
]
return result.filter(hasVal).join(' ')
}
function alterSequenceToSQL(stmt) {
const { type, keyword, sequence, if_exists, expr = [] } = stmt
const action = toUpper(type)
const sequenceName = tablesToSQL(sequence)
const exprList = expr.map(createDefinitionToSQL)
const result = [action, toUpper(keyword), toUpper(if_exists), sequenceName, exprList.join(', ')]
return result.filter(hasVal).join(' ')
}
function alterToSQL(stmt) {
const { keyword = 'table' } = stmt
switch (keyword) {
case 'aggregate':
return alterAggregateToSQL(stmt)
case 'table':
return alterTableToSQL(stmt)
case 'schema':
return alterSchemaToSQL(stmt)
case 'sequence':
return alterSequenceToSQL(stmt)
case 'domain':
case 'type':
return alterDomainTypeToSQL(stmt)
case 'function':
return alterFunctionToSQL(stmt)
case 'view':
return alterViewToSQL(stmt)
}
}
export {
alterArgsToSQL,
alterToSQL,
alterExprToSQL,
}