-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlFormat.js
282 lines (267 loc) · 9.76 KB
/
sqlFormat.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict'
const util = require('./util')
const KEY_WORDS = `
ACTION,ALGORITHM,ALWAYS,AS,ASC,AUTO_INCREMENT,AVG_ROW_LENGTH,BTREE,BY,CASCADE,CHARACTER,CHECK,CHECKSUM,
COLLATE,COLUMNS,COLUMN_FORMAT,COMMENT,COMPACT,COMPRESSED,COMPRESSION,CONNECTION,CONSTRAINT,CREATE,DATA,
DEFAULT,DELAY_KEY_WRITE,DELETE,DESC,DIRECTORY,DISK,DYNAMIC,ENCRYPTION,ENFORCED,ENGINE,EXISTS,FIRST,FIXED,
FOREIGN,FULL,FULLTEXT,GENERATED,HASH,IF,IGNORE,IN,INDEX,INSERT_METHOD,INVISIBLE,KEY,KEY_BLOCK_SIZE,LAST,
LESS,LIKE,LINEAR,LIST,LZ,MATCH,MAXVALUE,MAX_ROWS,MEMORY,MIN_ROWS,NO,NONE,NOT,NULL,ON,PACK_KEYS,PARSER,
PARTIAL,PARTITION,PARTITIONS,PASSWORD,PRIMARY,RANGE,REDUNDANT,REFERENCES,REPLACE,RESTRICT,ROW_FORMAT,SELECT,
SET,SIMPLE,SPATIAL,STATS_AUTO_RECALC,STATS_PERSISTENT,STATS_SAMPLE_PAGES,STORAGE,STORED,SUBPARTITION,
SUBPARTITIONS,TABLE,TABLESPACE,TEMPORARY,THAN,UNION,UNIQUE,UPDATE,USING,VALUES,VIRTUAL,VISIBLE,WITH,ZLIB
`.split(',').map(w => w.trim())
const KEY_WORDS_SET = new Set(KEY_WORDS)
const KEY_WORDS_MAP = {
'INNODB': 'InnoDB'
}
//目前不支持保留外部 注释, 请使用 mysql 的 COMMENT 语法
module.exports = {
rules: {
//必追加的规则, 最先执行
'_base': [
[/\n+/g, '\n', 'out'],
[/[\f\r\t\v]/g, ' ', 'out'],
[/[ ]+/g, ' ', 'out'],
[/^\s+/, '', 'raw'],
[/\s+$/, '', 'raw'],
],
'simple': [
'zeroDate',
'noSpaceInBrackets',
'noSpaceWrapEqual',
'spaceWrapBrackets',
'spaceAfterComma',
'noSpaceBeforeFuncBrackets',
'noSpaceBeforeTypeBrackets',
'referencesOrder',
'useNow',
'useKey',
'noKeyName',
'noIntLen',
'stringMultiLine',
'noAutoIncrement',
'noForeignAutoKey',
'intValueNoQuote',
'uppercase',
'indent2',
'spaceLine2',
'partLine',
'noSpaceEndLine',
],
'normal': [
'uppercase',
'useKey',
'useCurrent',
'noSpaceInBrackets',
'spaceBeforeBrackets',
'spaceAfterComma',
'noSpaceBeforeFuncBrackets',
'noSpaceBeforeTypeBrackets',
'noSpaceWrapEqual',
'indent2',
'spaceLine2',
'noSpaceEndLine',
],
'noSpaceEndLine': [[/[^\S\r\n]+\n/g, '\n', 'out']],
//缩进
'indent2': [[/\n +/g, '\n ', 'out']],
'indent4': [[/\n +/g, '\n ', 'out']],
//; 中间的空行数
'noSpaceLine': [[/;\s*/, ';', 'out']],
'spaceLine0': [[/;\s*/, ';\n', 'out']],
'spaceLine1': [[/;\s*/, ';\n\n', 'out']],
'spaceLine2': [[/;\s*/, ';\n\n\n', 'out']],
'spaceLine3': [[/;\s*/, ';\n\n\n\n', 'out']],
'partLine': [[/,\n+(\s*)(PRIMARY|KEY|UNIQUE|FOREIGN|CONSTRAINT)/i, ',\n\n$1$2', 'out']],
'zeroDate': [
[/date NOT NULL DEFAULT '0000-00-00'/gi, 'date NOT NULL DEFAULT 0', 'raw'],
[/datetime NOT NULL DEFAULT '0000-00-00 00:00:00'/gi, 'datetime NOT NULL DEFAULT 0', 'raw'],
],
'referencesOrder': [
[/ON UPDATE(( \w+){1,2}) ON DELETE(( \w+){1,2})/gi, 'ON DELETE$3 ON UPDATE$1', 'out'],
],
'useNow': [
[/\bCURRENT_TIMESTAMP\s*\(([0-9]+)\)/gi, 'NOW($1)', 'out'],
[/\bCURRENT_TIMESTAMP\b/gi, 'NOW()', 'out'],
],
'useCurrent': [
[/\bNOW\s*\(\s*([0-9]+)\s*\)/gi, 'CURRENT_TIMESTAMP($1)', 'out'],
[/\bNOW\s*\(\s*\)/gi, 'CURRENT_TIMESTAMP', 'out']
],
'useKey': [[/\bINDEX\b/gi, 'KEY', 'out']],
'useIndex': [[/\bKEY\b/gi, 'INDEX', 'out']],
'noKeyName': [
[/\bCONSTRAINT\s*`?\w+`?\s*/gi, '', 'out', '\''],
[/\bKEY(\s+|\s*`)\w+`?(\s*)\(/gi, 'KEY$2(', 'out', '\''],
],
'noIndexName': ['noKeyName'],
'noIntLen': [[/int\s*\([0-9]+\)/gi, 'int', 'out']],
'stringMultiLine': [[/\\n/g, '\n', 'in', '\'']],
'stringInLine': [[/\n/g, '\\n', 'in', '\'']],
'noBackQuote': [[/^`(.+)`$/gi, '$1', 'in']],
'noSpaceWrapBackQuote': [[/ *(`\w+`) */gi, '$1', 'out', '\'']],
'noSpaceInBrackets': [
[/\( +(\S)/g, '($1', 'out'],
[/(\S) +\)/g, '$1)', 'out']
],
'noSpaceBeforeBrackets': [[/ *\(/g, '(', 'out']],
'noSpaceAfterBrackets': [[/\) */g, ')', 'out']],
'spaceBeforeBrackets': [[/ *\(/g, ' (', 'out']],
'spaceAfterBrackets': [[/\) */g, ') ', 'out'], [/\) ,/g, '),', 'out']],
'spaceBeforeComma': [[/ *,/g, ' ,', 'out']],
'spaceAfterComma': [[/, *([^\n]|$)/g, ', $1', 'out']],
'noSpaceWrapBrackets': ['noSpaceBeforeBrackets', 'noSpaceAfterBrackets'],
'spaceWrapBrackets': ['spaceBeforeBrackets', 'spaceAfterBrackets'],
'noSpaceBeforeTypeBrackets': [
[/int(\s*)\(/gi, 'int(', 'out'],
[/char(\s*)\(/gi, 'char(', 'out'],
[/\benum(\s*)\(/gi, 'enum(', 'out'],
[/\bdatetime(\s*)\(/gi, 'datetime(', 'out'],
],
'noSpaceBeforeFuncBrackets': [
[/\bcurrent_timestamp(\s*)\(/gi, 'current_timestamp(', 'out'],
[/\bnow(\s*)\(/gi, 'now(', 'out'],
],
'noSpaceWrapEqual': [[/\s*=\s*/gi, '=', 'out']],
'noTableCharset_utf8': [[/\s*DEFAULT CHARSET\s*=\s*utf8\b/i, '', 'out']],
'noTableCharset_utf8mb4': [
[/\s*DEFAULT\s+CHARSET\s*=\s*utf8mb4\s+COLLATE\s*=\s*utf8mb4_0900_ai_ci/i, '', 'out'],
[/\s*DEFAULT\s+CHARSET\s*=\s*utf8mb4\s+COLLATE\s*=\s*utf8mb4_general_ci/i, '', 'out']
],
'noTableCharset': ['noTableCharset_utf8', 'noTableCharset_utf8mb4'],
'noAutoIncrement': [[/\s*AUTO_INCREMENT\s*=\s*[0-9]+/i, '', 'out']],
'intValueNoQuote': [[/\b(tinyint|smallint|mediumint|int|bigint|float|double)( |\()(.*?) DEFAULT '(-?[0-9.]+)'/gi, '$1$2$3 DEFAULT $4', 'raw']],
'uppercase': [(sql, items) => {
util.forEachQuoteItems(items, 'out', item => {
let words = item.str.match(/\b[a-z_]+\b/gi)
words = [...new Set(words)]
for (const word of words) {
let uppercase = word.toUpperCase()
if (uppercase != word && KEY_WORDS_SET.has(uppercase)) {
item.str = item.str.replace(new RegExp(`\\b${word}\\b`, 'g'), uppercase)
} else if (KEY_WORDS_MAP[uppercase]) {
item.str = item.str.replace(new RegExp(`\\b${word}\\b`, 'g'), KEY_WORDS_MAP[uppercase])
}
}
})
}],
'noForeignAutoKey': [(sql, items) => {
let rows = util.splitOutQuote(items, '\n')
let foreignKey = []
for (const row of rows) {
let rt = row.match(/\bFOREIGN\s+KEY\s*\((.*?)\)/i)
if (rt) {
let keys = rt[1].split(',').map(col => {
col = col.trim()
return col[0] == '`' ? col.slice(1, -1) : col
})
foreignKey.push(keys)
}
}
let oldLen = rows.length
for (const keys of foreignKey) {
let strKeys = keys.map(key => `\`?${key}\`?`).join('\\s*,\\s*')
rows = rows.filter(row => !row.match(new RegExp(`^\\s*KEY\\s*\\(\\s*${strKeys}\\s*\\)`)))
}
if (rows.length != oldLen) return rows.join('\n') //修改了才返回
}]
},
createRules(rules = [], addBase = false) {
let _rules = rules
if (addBase) {
let _rules = [...this.rules._base]
if (typeof rules == 'string') {
_rules.push(rules)
} else {
_rules.push(...rules)
}
}
let newRules = []
if (typeof _rules == 'string') {
if (!this.rules[_rules]) throw new Error(`formatRules没有 '${_rules}'`)
newRules.push(...this.createRules(this.rules[_rules]))
} else if (Array.isArray(_rules)) {
for (const rule of _rules) {
if (Array.isArray(rule) || typeof rule == 'function') {
newRules.push(rule)
} else if (typeof rule == 'string') {
if (!this.rules[rule]) throw new Error(`formatRules没有 '${rule}'`)
newRules.push(...this.createRules(this.rules[rule]))
} else {
throw new Error('formatRules 错误')
}
}
} else {
throw new Error('formatRules 错误')
}
return newRules
},
rulesToString(rules) {
if (typeof rules == 'string') return rules
let strings = rules.map(rule => {
if (Array.isArray(rule)) {
return rule.map(v => v.toString()).join('|')
} else if (typeof rule == 'function') {
return rule.toString()
} else if (typeof rule == 'string') {
return rule
} else {
throw new Error('formatRules 错误')
}
})
return strings.join(',')
},
format(sql, rules, outComment) {
let _rules = this.createRules(rules, true)
sql = util.removeComment(sql, undefined, outComment)
let items = util.splitOutQuote(sql, ';')
let content = ''
items.forEach((str, i) => {
str = str.trim()
if (!str) return
if (i != items.length - 1) str += ';'
content += this.formatOne(str, _rules)
})
return content.trim()
},
formatOne(sql, rules) {
let items = null
let itemsChar
let needUpdateItems = true
let updateString = (newSql) => {
if (newSql == undefined) return
if (newSql != sql) {
sql = newSql
needUpdateItems = true
}
}
let updateItems = (char) => {
items = util.splitByQuote(newStr(), { type: 'all', char })
itemsChar = char
needUpdateItems = false
}
//needUpdateItems 为 true 的时候,说明 sql 是最新的, 否则以 items 内的为准
let newStr = () => needUpdateItems ? sql : items.map(item => item.str).join('')
for (const rule of rules) {
if (typeof rule == 'function') {
if (needUpdateItems) updateItems()
updateString(rule(newStr(), items))
continue
}
let [reg, sub, where, char] = rule
if (where == 'raw') {
updateString(newStr().replace(reg, sub))
} else {
if (needUpdateItems || char !== itemsChar) updateItems(char) //char 发生变化就需要更新 items
let isBreak = false
util.forEachQuoteItems(items, where, item => {
if (isBreak) return
let temp = item.str.replace(reg, sub)
if (!reg.global) isBreak = temp != item.str
item.str = temp
})
}
}
return newStr()
}
}