forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
139 lines (132 loc) · 4.73 KB
/
create.js
File metadata and controls
139 lines (132 loc) · 4.73 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
import { exprToSQL } from './expr'
import { indexDefinitionToSQL, indexOptionListToSQL, indexTypeToSQL } from './index-definition'
import { columnDefinitionToSQL } from './column'
import { constraintDefinitionToSQL } from './constrain'
import { funcToSQL } from './func'
import { tablesToSQL, tableOptionToSQL, tableToSQL } from './tables'
import { unionToSQL } from './union'
import { columnOrderListToSQL, commonOptionConnector, commonKeywordArgsToSQL, toUpper, hasVal, identifierToSql, triggerEventToSQL, literalToSQL } from './util'
function createDefinitionToSQL(definition) {
if (!definition) return []
const { resource } = definition
switch (resource) {
case 'column':
return columnDefinitionToSQL(definition)
case 'index':
return indexDefinitionToSQL(definition)
case 'constraint':
return constraintDefinitionToSQL(definition)
default:
throw new Error(`unknow resource = ${resource} type`)
}
}
function createTableToSQL(stmt) {
const {
type, keyword, table, like, as, temporary,
if_not_exists: ifNotExists,
create_definitions: createDefinition,
table_options: tableOptions,
ignore_replace: ignoreReplace,
query_expr: queryExpr,
} = stmt
const sql = [toUpper(type), toUpper(temporary), toUpper(keyword), toUpper(ifNotExists), tablesToSQL(table)]
if (like) {
const { type: likeType, table: likeTable } = like
const likeTableName = tablesToSQL(likeTable)
sql.push(toUpper(likeType), likeTableName)
return sql.filter(hasVal).join(' ')
}
if (createDefinition) {
sql.push(`(${createDefinition.map(createDefinitionToSQL).join(', ')})`)
}
if (tableOptions) {
sql.push(tableOptions.map(tableOptionToSQL).join(' '))
}
sql.push(toUpper(ignoreReplace), toUpper(as))
if (queryExpr) sql.push(unionToSQL(queryExpr))
return sql.filter(hasVal).join(' ')
}
function createTriggerToSQL(stmt) {
const {
constraint, constraint_kw: constraintKw,
deferrable,
events, execute,
for_each: forEach, from,
location,
keyword,
type, table,
when,
} = stmt
const sql = [toUpper(type), toUpper(constraintKw), toUpper(keyword), identifierToSql(constraint), toUpper(location)]
const event = triggerEventToSQL(events)
sql.push(event, 'ON', tableToSQL(table))
if (from) sql.push('FROM', tableToSQL(from))
sql.push(...commonKeywordArgsToSQL(deferrable), ...commonKeywordArgsToSQL(forEach))
if (when) sql.push(toUpper(when.type), exprToSQL(when.cond))
sql.push(toUpper(execute.keyword), funcToSQL(execute.expr))
return sql.filter(hasVal).join(' ')
}
function createExtensionToSQL(stmt) {
const {
extension, from, if_not_exists: ifNotExists,
keyword, schema, type, with: withName, version,
} = stmt
const sql = [
toUpper(type),
toUpper(keyword),
toUpper(ifNotExists),
literalToSQL(extension),
toUpper(withName),
commonOptionConnector('SCHEMA', literalToSQL, schema),
commonOptionConnector('VERSION', literalToSQL, version),
commonOptionConnector('FROM', literalToSQL, from),
]
return sql.filter(hasVal).join(' ')
}
function createIndexToSQL(stmt) {
const {
concurrently, filestream_on: fileStream, keyword, include, index_columns: indexColumns,
index_type: indexType, index_using: indexUsing, index, on, on_kw: onKw, table, tablespace, type, where,
with: withExpr, with_before_where: withBeforeWhere,
} = stmt
const withIndexOpt = withExpr && `WITH (${indexOptionListToSQL(withExpr).join(', ')})`
const includeColumns = include && `${toUpper(include.keyword)} (${include.columns.map(col => identifierToSql(col)).join(', ')})`
const sql = [
toUpper(type), toUpper(indexType), toUpper(keyword), toUpper(concurrently),
identifierToSql(index), toUpper(onKw), tableToSQL(table), ...indexTypeToSQL(indexUsing),
`(${columnOrderListToSQL(indexColumns)})`, includeColumns,
commonOptionConnector('TABLESPACE', literalToSQL, tablespace),
]
if (withBeforeWhere) {
sql.push(withIndexOpt, commonOptionConnector('WHERE', exprToSQL, where))
} else {
sql.push(commonOptionConnector('WHERE', exprToSQL, where), withIndexOpt)
}
sql.push(commonOptionConnector('ON', exprToSQL, on), commonOptionConnector('FILESTREAM_ON', literalToSQL, fileStream))
return sql.filter(hasVal).join(' ')
}
function createToSQL(stmt) {
const { keyword } = stmt
let sql = ''
switch (keyword.toLowerCase()) {
case 'table':
sql = createTableToSQL(stmt)
break
case 'trigger':
sql = createTriggerToSQL(stmt)
break
case 'extension':
sql = createExtensionToSQL(stmt)
break
case 'index':
sql = createIndexToSQL(stmt)
break
default:
throw new Error(`unknow create resource ${keyword}`)
}
return sql
}
export {
createToSQL,
createDefinitionToSQL,
}