forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-definition.js
More file actions
92 lines (86 loc) · 2.46 KB
/
Copy pathindex-definition.js
File metadata and controls
92 lines (86 loc) · 2.46 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
import {
literalToSQL,
toUpper,
hasVal,
commentToSQL,
onPartitionsToSQL,
} from './util'
import { exprToSQL } from './expr'
function indexTypeToSQL(indexType) {
if (!indexType) return []
const { keyword, type } = indexType
return [keyword.toUpperCase(), toUpper(type)]
}
function indexOptionToSQL(indexOpt) {
if (!indexOpt) return
const { type, expr, symbol } = indexOpt
const upperType = type.toUpperCase()
const indexOptArray = []
indexOptArray.push(upperType)
switch (upperType) {
case 'KEY_BLOCK_SIZE':
if (symbol) indexOptArray.push(symbol)
indexOptArray.push(literalToSQL(expr))
break
case 'BTREE':
case 'HASH':
indexOptArray.length = 0
indexOptArray.push(...indexTypeToSQL(indexOpt))
break
case 'WITH PARSER':
indexOptArray.push(expr)
break
case 'VISIBLE':
case 'INVISIBLE':
break
case 'COMMENT':
indexOptArray.shift()
indexOptArray.push(commentToSQL(indexOpt))
break
case 'DATA_COMPRESSION':
indexOptArray.push(symbol, toUpper(expr.value), onPartitionsToSQL(expr.on))
break
default:
indexOptArray.push(symbol, literalToSQL(expr))
break
}
return indexOptArray.filter(hasVal).join(' ')
}
function indexOptionListToSQL(indexOptList) {
if (!indexOptList) return []
return indexOptList.map(indexOptionToSQL)
}
function indexTypeAndOptionToSQL(indexDefinition) {
const {
constraint_type: constraintType,
index_type: indexType,
index_options: indexOptions = [],
definition,
on, with: withExpr,
} = indexDefinition
const dataType = []
dataType.push(...indexTypeToSQL(indexType))
if (definition && definition.length) {
const definitionSQL = toUpper(constraintType) === 'CHECK' ? `(${exprToSQL(definition[0])})` : `(${definition.map(col => exprToSQL(col)).join(', ')})`
dataType.push(definitionSQL)
}
dataType.push(indexOptionListToSQL(indexOptions).join(' '))
if (withExpr) dataType.push(`WITH (${indexOptionListToSQL(withExpr).join(', ')})`)
if (on) dataType.push(`ON [${on}]`)
return dataType
}
function indexDefinitionToSQL(indexDefinition) {
const indexSQL = []
const { keyword, index } = indexDefinition
indexSQL.push(toUpper(keyword))
indexSQL.push(index)
indexSQL.push(...indexTypeAndOptionToSQL(indexDefinition))
return indexSQL.filter(hasVal).join(' ')
}
export {
indexDefinitionToSQL,
indexTypeToSQL,
indexOptionToSQL,
indexOptionListToSQL,
indexTypeAndOptionToSQL,
}