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
154 lines (144 loc) · 4.52 KB
/
column.js
File metadata and controls
154 lines (144 loc) · 4.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
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
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 columnRefToSQL(expr) {
const {
arrow, as, collate, column, isDual, table, parentheses, property,
} = expr
let str = column === '*' ? '*' : identifierToSql(column, isDual)
if (table) str = `${identifierToSql(table)}.${str}`
const result = [
str,
commonOptionConnector('AS', exprToSQL, as),
commonOptionConnector(arrow, literalToSQL, property),
]
if (collate) result.push(commonTypeValue(collate).join(' '))
const sql = result.filter(hasVal).join(' ')
return parentheses ? `(${sql})` : sql
}
function columnDataType(definition) {
const { dataType, length, suffix, scale } = definition || {}
let result = dataType
if (length) {
result += `(${[length, scale].filter(hasVal).join(', ')})`
}
if (suffix && suffix.length) result += ` ${suffix.join(' ')}`
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(keyword.toUpperCase())
reference.push(tablesToSQL(table))
reference.push(`(${definition.map(identifierToSql).join(', ')})`)
reference.push(toUpper(match))
reference.push(...commonTypeValue(onDelete))
reference.push(...commonTypeValue(onUpdate))
return reference.filter(hasVal)
}
function columnOption(definition) {
const columnOpt = []
const {
nullable, 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(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 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)
return column.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 (column.as !== null) {
str = `${str} AS `
if (column.as.match(/^[a-z_][0-9a-z_]*$/i)) str = `${str}${identifierToSql(column.as)}`
else str = `${str}${columnIdentifierToSql(column.as)}`
}
return str
}
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,
}