forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow.js
More file actions
55 lines (50 loc) · 1.62 KB
/
show.js
File metadata and controls
55 lines (50 loc) · 1.62 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
import { exprToSQL } from './expr'
import { limitToSQL } from './limit'
import { tablesToSQL } from './tables'
import { commonOptionConnector, hasVal, literalToSQL, toUpper } from './util'
function showEventToSQL(showEventExpr) {
const { in: inClause, from , limit } = showEventExpr
return [
commonOptionConnector('IN', literalToSQL, inClause && inClause.right),
commonOptionConnector('FROM', tablesToSQL, from),
limitToSQL(limit),
].filter(hasVal).join(' ')
}
function showLikeAndWhereToSQL(showCharacterSetExpr) {
const { expr } = showCharacterSetExpr
if (!expr) return
const { op } = expr
if (toUpper(op) === 'LIKE') return commonOptionConnector('LIKE', literalToSQL, expr.right)
return commonOptionConnector('WHERE', exprToSQL, expr)
}
function showGrantsForUser(showGrantsForExpr) {
const { for: forExpr } = showGrantsForExpr
if (!forExpr) return
const { user, host, role_list } = forExpr
let userAndHost = `'${user}'`
if (host) userAndHost += `@'${host}'`
return ['FOR', userAndHost, role_list && 'USING', role_list && role_list.map(role => `'${role}'`).join(', ')].filter(hasVal).join(' ')
}
function showToSQL(showExpr) {
const { suffix, keyword } = showExpr
let str = ''
switch (toUpper(keyword)) {
case 'BINLOG':
str = showEventToSQL(showExpr)
break
case 'CHARACTER':
case 'COLLATION':
str = showLikeAndWhereToSQL(showExpr)
break
case 'GRANTS':
str = showGrantsForUser(showExpr)
break
default:
break
}
const result = ['SHOW', toUpper(keyword), toUpper(suffix), str]
return result.filter(hasVal).join(' ')
}
export {
showToSQL,
}