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
72 lines (69 loc) · 1.89 KB
/
alter.js
File metadata and controls
72 lines (69 loc) · 1.89 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
import { columnDefinitionToSQL, columnRefToSQL } 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) {
if (!expr) return ''
const {
action,
create_definitions: createDefinition,
first_after: firstAfter,
if_not_exists: ifNotExists,keyword,
old_column: oldColumn,
prefix,
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
case 'key':
name = identifierToSql(expr[resource])
break
default:
break
}
const alterArray = [
toUpper(action),
toUpper(keyword),
toUpper(ifNotExists),
oldColumn && columnRefToSQL(oldColumn),
toUpper(prefix),
name,
dataType.filter(hasVal).join(' '),
firstAfter && `${toUpper(firstAfter.keyword)} ${columnRefToSQL(firstAfter.column)}`,
]
return alterArray.filter(hasVal).join(' ')
}
export {
alterToSQL,
alterExprToSQL,
}