forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
128 lines (119 loc) · 3.39 KB
/
command.js
File metadata and controls
128 lines (119 loc) · 3.39 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
import { alterExprToSQL } from './alter'
import { columnDataType, columnRefToSQL } from './column'
import { createDefinitionToSQL } from './create'
import { identifierToSql, hasVal, toUpper } from './util'
import { exprToSQL } from './expr'
import { tablesToSQL, tableToSQL } from './tables'
function commonCmdToSQL(stmt) {
const { type, keyword, name } = stmt
const clauses = [toUpper(type), toUpper(keyword)]
switch (keyword) {
case 'table':
clauses.push(tablesToSQL(name))
break
case 'procedure':
clauses.push(identifierToSql(name))
break
case 'index':
clauses.push(
columnRefToSQL(name),
'ON',
tableToSQL(stmt.table),
stmt.options && stmt.options.map(alterExprToSQL).filter(hasVal).join(' ')
)
break
default:
break
}
return clauses.filter(hasVal).join(' ')
}
function renameToSQL(stmt) {
const { type, table } = stmt
const clauses = []
const prefix = `${type && type.toUpperCase()} TABLE`
if (table) {
for (const tables of table) {
const renameInfo = tables.map(tableToSQL)
clauses.push(renameInfo.join(' TO '))
}
}
return `${prefix} ${clauses.join(', ')}`
}
function useToSQL(stmt) {
const { type, db } = stmt
const action = toUpper(type)
const database = identifierToSql(db)
return `${action} ${database}`
}
function callToSQL(stmt) {
const type = 'CALL'
const storeProcessCall = exprToSQL(stmt.expr)
return `${type} ${storeProcessCall}`
}
function setVarToSQL(stmt) {
const { expr } = stmt
const action = 'SET'
const val = exprToSQL(expr)
return `${action} ${val}`
}
function pgLock(stmt) {
const { lock_mode: lockMode, nowait } = stmt
const lockInfo = []
if (lockMode) {
const { mode } = lockMode
lockInfo.push(mode.toUpperCase())
}
if (nowait) lockInfo.push(nowait.toUpperCase())
return lockInfo
}
function lockUnlockToSQL(stmt) {
const { type, keyword, tables } = stmt
const result = [type.toUpperCase(), toUpper(keyword)]
if (type.toUpperCase() === 'UNLOCK') return result.join(' ')
const tableStmt = []
for (const tableInfo of tables) {
const { table, lock_type: lockType } = tableInfo
const tableInfoTemp = [tableToSQL(table)]
if (lockType) {
const lockKeyList = ['prefix', 'type', 'suffix']
tableInfoTemp.push(lockKeyList.map(key => toUpper(lockType[key])).filter(hasVal).join(' '))
}
tableStmt.push(tableInfoTemp.join(' '))
}
result.push(tableStmt.join(', '), ...pgLock(stmt))
return result.filter(hasVal).join(' ')
}
function declareToSQL(stmt) {
const { type, declare } = stmt
const result = [toUpper(type)]
const info = declare.map(dec => {
const { at, name, as, prefix, definition, keyword } = dec
const declareInfo = [`${at}${name}`, toUpper(as)]
switch (keyword) {
case 'variable':
declareInfo.push(columnDataType(prefix))
if (definition) declareInfo.push('=', exprToSQL(definition))
break
case 'cursor':
declareInfo.push(toUpper(prefix))
break
case 'table':
declareInfo.push(toUpper(prefix), `(${definition.map(createDefinitionToSQL).join(', ')})`)
break
default:
break
}
return declareInfo.filter(hasVal).join(' ')
}).join(', ')
result.push(info)
return result.join(' ')
}
export {
commonCmdToSQL,
declareToSQL,
renameToSQL,
useToSQL,
callToSQL,
setVarToSQL,
lockUnlockToSQL,
}