forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn.js
More file actions
185 lines (172 loc) · 6.18 KB
/
Copy pathcolumn.js
File metadata and controls
185 lines (172 loc) · 6.18 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { constraintDefinitionToSQL } from './constrain'
import { exprToSQL } from './expr'
import { castToSQL } from './func'
import { tablesToSQL } from './tables'
import {
autoIncreatementToSQL,
columnIdentifierToSql,
commonOptionConnector,
commonTypeValue,
commentToSQL,
hasVal,
identifierToSql,
literalToSQL,
toUpper,
} from './util'
function columnOffsetToSQL(column, isDual) {
if (typeof column === 'string') return identifierToSql(column, isDual)
const { expr, offset, suffix } = column
return [exprToSQL(expr), offset, suffix].filter(hasVal).join('')
}
function columnRefToSQL(expr) {
const {
array_index, arrows = [], as, collate, column, isDual, schema, table, parentheses, properties,
suffix, order_by,
} = expr
let str = column === '*' ? '*' : columnOffsetToSQL(column, isDual)
if (table) str = `${identifierToSql(table)}.${str}`
if (schema) str = `${identifierToSql(schema)}.${str}`
if (array_index) {
str = `${str}[${array_index.number}]`
if (array_index.property) str = `${str}.${array_index.property.value}`
}
const result = [
str,
commonOptionConnector('AS', exprToSQL, as),
arrows.map((arrow, index) => commonOptionConnector(arrow, literalToSQL, properties[index])).join(' '),
]
if (collate) result.push(commonTypeValue(collate).join(' '))
result.push(toUpper(suffix))
result.push(toUpper(order_by))
const sql = result.filter(hasVal).join(' ')
return parentheses ? `(${sql})` : sql
}
function columnDataType(definition) {
const { dataType, length, suffix, scale, expr } = definition || {}
let result = dataType
if (length != null) result += `(${[length, scale].filter(val => val != null).join(', ')})`
if (suffix && suffix.length) result += ` ${suffix.join(' ')}`
if (expr) result += exprToSQL(expr)
return result
}
function columnReferenceDefinitionToSQL(referenceDefinition) {
const reference = []
if (!referenceDefinition) return reference
const {
definition,
keyword,
match,
table,
on_delete: onDelete,
on_update: onUpdate,
} = referenceDefinition
reference.push(toUpper(keyword))
reference.push(tablesToSQL(table))
reference.push(definition && `(${definition.map(col => exprToSQL(col)).join(', ')})`)
reference.push(toUpper(match))
reference.push(...commonTypeValue(onDelete))
reference.push(...commonTypeValue(onUpdate))
return reference.filter(hasVal)
}
function columnOption(definition) {
const columnOpt = []
const {
nullable, character_set: characterSet, check, comment, collate, storage,
default_val: defaultOpt,
auto_increment: autoIncrement,
unique_or_primary: uniquePrimary,
column_format: columnFormat,
reference_definition: referenceDefinition,
} = definition
columnOpt.push(toUpper(nullable && nullable.value))
if (defaultOpt) {
const { type, value } = defaultOpt
columnOpt.push(type.toUpperCase(), exprToSQL(value))
}
columnOpt.push(constraintDefinitionToSQL(check))
columnOpt.push(autoIncreatementToSQL(autoIncrement), toUpper(uniquePrimary), commentToSQL(comment))
columnOpt.push(...commonTypeValue(characterSet))
columnOpt.push(...commonTypeValue(collate))
columnOpt.push(...commonTypeValue(columnFormat))
columnOpt.push(...commonTypeValue(storage))
columnOpt.push(...columnReferenceDefinitionToSQL(referenceDefinition))
return columnOpt.filter(hasVal).join(' ')
}
function columnOrderToSQL(columnOrder) {
const { column, collate, nulls, opclass, order } = columnOrder
const result = [
exprToSQL(column),
commonOptionConnector(collate && collate.type, identifierToSql, collate && collate.value),
opclass,
toUpper(order),
toUpper(nulls),
]
return result.filter(hasVal).join(' ')
}
function generatedExpressionToSQL(generated) {
if (!generated) return
const result = [toUpper(generated.value), `(${exprToSQL(generated.expr)})`, toUpper(generated.storage_type)]
return result.filter(hasVal).join(' ')
}
function columnDefinitionToSQL(columnDefinition) {
const column = []
const name = columnRefToSQL(columnDefinition.column)
const dataType = columnDataType(columnDefinition.definition)
column.push(name)
column.push(dataType)
const columnOpt = columnOption(columnDefinition)
column.push(columnOpt)
const generated = generatedExpressionToSQL(columnDefinition.generated)
column.push(generated)
return column.filter(hasVal).join(' ')
}
function asToSQL(asStr) {
if (!asStr) return ''
return ['AS', /^(`?)[a-z_][0-9a-z_]*(`?)$/i.test(asStr) ? identifierToSql(asStr) : columnIdentifierToSql(asStr)].join(' ')
}
function fulltextSearchToSQL(expr) {
const { against, as, columns, match, mode } = expr
const matchExpr = [toUpper(match), `(${columns.map(col => columnRefToSQL(col)).join(', ')})`].join(' ')
const againstExpr = [toUpper(against), ['(', exprToSQL(expr.expr), mode && ` ${literalToSQL(mode)}`, ')'].filter(hasVal).join('')].join(' ')
return [matchExpr, againstExpr, asToSQL(as)].filter(hasVal).join(' ')
}
function columnToSQL(column, isDual) {
const { expr, type } = column
if (type === 'cast') return castToSQL(column)
if (isDual) expr.isDual = isDual
let str = exprToSQL(expr)
if (expr.parentheses && Reflect.has(expr, 'array_index')) str = `(${str})`
if (expr.array_index && expr.type !== 'column_ref') str = `${str}[${expr.array_index.number}]`
return [str, asToSQL(column.as)].filter(hasVal).join(' ')
}
function getDual(tables) {
const baseTable = Array.isArray(tables) && tables[0]
if (baseTable && baseTable.type === 'dual') return true
return false
}
/**
* Stringify column expressions
*
* @param {Array} columns
* @return {string}
*/
function columnsToSQL(columns, tables) {
if (!columns || columns === '*') return columns
const isDual = getDual(tables)
const result = []
const { expr_list: exprList, star, type } = columns
result.push(star, toUpper(type))
const exprListArr = exprList || columns
const columnsStr = exprListArr.map(col => columnToSQL(col, isDual)).join(', ')
result.push([type && '(', columnsStr, type && ')'].filter(hasVal).join(''))
return result.filter(hasVal).join(' ')
}
export {
columnDefinitionToSQL,
columnRefToSQL,
columnsToSQL,
columnDataType,
columnOrderToSQL,
columnReferenceDefinitionToSQL,
fulltextSearchToSQL,
}