forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion.js
More file actions
100 lines (96 loc) · 2.77 KB
/
union.js
File metadata and controls
100 lines (96 loc) · 2.77 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
import { alterToSQL } from './alter'
import { analyzeToSQL, attachToSQL } from './analyze'
import { createToSQL } from './create'
import { commentOnToSQL } from './comment'
import { explainToSQL } from './explain'
import { selectToSQL } from './select'
import { deleteToSQL } from './delete'
import { updateToSQL } from './update'
import { insertToSQL } from './insert'
import {
callToSQL,
commonCmdToSQL,
deallocateToSQL,
declareToSQL,
descToSQL,
executeToSQL,
forLoopToSQL,
grantAndRevokeToSQL,
ifToSQL,
useToSQL,
raiseToSQL,
renameToSQL,
setVarToSQL,
lockUnlockToSQL,
} from './command'
import { execToSQL } from './exec'
import { orderOrPartitionByToSQL } from './expr'
import { limitToSQL } from './limit'
import { loadDataToSQL } from './load'
import { procToSQL } from './proc'
import { transactionToSQL } from './transaction'
import { showToSQL } from './show'
import { hasVal, toUpper } from './util'
const typeToSQLFn = {
alter : alterToSQL,
analyze : analyzeToSQL,
attach : attachToSQL,
create : createToSQL,
comment : commentOnToSQL,
select : selectToSQL,
deallocate : deallocateToSQL,
delete : deleteToSQL,
exec : execToSQL,
execute : executeToSQL,
explain : explainToSQL,
for : forLoopToSQL,
update : updateToSQL,
if : ifToSQL,
insert : insertToSQL,
load_data : loadDataToSQL,
drop : commonCmdToSQL,
truncate : commonCmdToSQL,
replace : insertToSQL,
declare : declareToSQL,
use : useToSQL,
rename : renameToSQL,
call : callToSQL,
desc : descToSQL,
set : setVarToSQL,
lock : lockUnlockToSQL,
unlock : lockUnlockToSQL,
show : showToSQL,
grant : grantAndRevokeToSQL,
revoke : grantAndRevokeToSQL,
proc : procToSQL,
raise : raiseToSQL,
transaction : transactionToSQL,
}
function unionToSQL(stmt) {
if (!stmt) return ''
const fun = typeToSQLFn[stmt.type]
const { _parentheses, _orderby, _limit } = stmt
const res = [_parentheses && '(', fun(stmt)]
while (stmt._next) {
const nextFun = typeToSQLFn[stmt._next.type]
const unionKeyword = toUpper(stmt.set_op)
res.push(unionKeyword, nextFun(stmt._next))
stmt = stmt._next
}
res.push(_parentheses && ')', orderOrPartitionByToSQL(_orderby, 'order by'), limitToSQL(_limit))
return res.filter(hasVal).join(' ')
}
function multipleToSQL(stmt) {
const res = []
for (let i = 0, len = stmt.length; i < len; ++i) {
const astInfo = stmt[i] && stmt[i].ast ? stmt[i].ast : stmt[i]
let sql = unionToSQL(astInfo)
if (i === len - 1 && astInfo.type === 'transaction') sql = `${sql} ;`
res.push(sql)
}
return res.join(' ; ')
}
export {
unionToSQL,
multipleToSQL,
}