forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalter.js
More file actions
52 lines (49 loc) · 1.52 KB
/
alter.js
File metadata and controls
52 lines (49 loc) · 1.52 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
import { columnDefinitionToSQL } from './column'
import { createDefinitionToSQL } from './create'
import { indexTypeAndOptionToSQL } from './index-definition'
import { tablesToSQL } from './tables'
import { exprToSQL } from './expr'
import { hasVal, toUpper, identifierToSql } from './util'
function alterToSQL(stmt) {
const { type, table, expr = [] } = stmt
const action = toUpper(type)
const tableName = tablesToSQL(table)
const exprList = expr.map(exprToSQL)
const result = [action, 'TABLE', tableName, exprList.join(', ')]
return result.filter(hasVal).join(' ')
}
function alterExprToSQL(expr) {
const {
action, create_definitions: createDefinition, if_not_exists: ifNotExists,keyword, resource, symbol,
} = expr
let name = ''
let dataType = []
switch (resource) {
case 'column':
dataType = [columnDefinitionToSQL(expr)]
break
case 'index':
dataType = indexTypeAndOptionToSQL(expr)
name = expr[resource]
break
case 'table':
name = identifierToSql(expr[resource])
break
case 'algorithm':
case 'lock':
name = [symbol, toUpper(expr[resource])].filter(hasVal).join(' ')
break
case 'constraint':
name = identifierToSql(expr[resource])
dataType = [createDefinitionToSQL(createDefinition)]
break
default:
break
}
const alterArray = [toUpper(action), toUpper(keyword), toUpper(ifNotExists), name, dataType.filter(hasVal).join(' ')]
return alterArray.filter(hasVal).join(' ')
}
export {
alterToSQL,
alterExprToSQL,
}