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
85 lines (76 loc) · 2.18 KB
/
command.js
File metadata and controls
85 lines (76 loc) · 2.18 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
import { identifierToSql, hasVal, toUpper } from './util'
import { exprToSQL } from './expr'
import { tablesToSQL, tableToSQL } from './tables'
function dropToSQL(stmt) {
const clauses = ['DROP TABLE', tablesToSQL(stmt.table)]
return clauses.join(' ')
}
function truncateToSQL(stmt) {
const clauses = ['TRUNCATE', stmt.keyword, tablesToSQL(stmt.table)]
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 = type && type.toUpperCase()
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(' ')
}
export {
dropToSQL,
truncateToSQL,
renameToSQL,
useToSQL,
callToSQL,
setVarToSQL,
lockUnlockToSQL,
}