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
101 lines (91 loc) · 2.89 KB
/
Copy pathcolumn.js
File metadata and controls
101 lines (91 loc) · 2.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
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
import { exprToSQL } from './expr'
import { tablesToSQL } from './tables'
import { identifierToSql, commonTypeValue, columnRefToSQL, toUpper, hasVal, commentToSQL } from './util'
function columnDataType(definition) {
const dataType = definition && definition.dataType
if (definition && definition.length) return `${dataType}(${definition.length})`
return dataType
}
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, 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(toUpper(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 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(' ')
}
/**
* Stringify column expressions
*
* @param {Array} columns
* @return {string}
*/
function columnsToSQL(columns, tables) {
if (!columns) return
if (columns === '*') return columns
const baseTable = Array.isArray(tables) && tables[0]
let isDual = false
if (baseTable && baseTable.type === 'dual') isDual = true
return columns
.map(column => {
const { expr } = 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}\`${column.as}\``
}
return str
})
.join(', ')
}
export {
columnDefinitionToSQL,
columnsToSQL,
columnDataType,
columnReferenceDefinitionToSQL,
}