forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.pegjs
More file actions
258 lines (235 loc) · 5.55 KB
/
core.pegjs
File metadata and controls
258 lines (235 loc) · 5.55 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
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
// Common CREATE statement rules shared across SQL dialects
// These rules are identical across all dialects
// CREATE TABLE ... LIKE
create_like_table_simple
= KW_LIKE __ t:table_ref_list {
return {
type: 'like',
table: t
}
}
create_like_table
= create_like_table_simple
/ LPAREN __ e:create_like_table __ RPAREN {
e.parentheses = true;
return e;
}
// Table definition (columns, constraints, indexes)
create_table_definition
= LPAREN __ head:create_definition tail:(__ COMMA __ create_definition)* __ RPAREN {
return createList(head, tail);
}
// Constraint name
constraint_name
= kc:KW_CONSTRAINT __ c:ident? {
return {
keyword: kc.toLowerCase(),
constraint: c
}
}
// COLLATE expression
collate_expr
= KW_COLLATE __ s:KW_ASSIGIN_EQUAL? __ ca:ident {
return {
type: 'collate',
keyword: 'collate',
collate: {
name: ca,
symbol: s,
}
}
}
// Column format
column_format
= k:'COLUMN_FORMAT'i __ f:('FIXED'i / 'DYNAMIC'i / 'DEFAULT'i) {
return {
type: 'column_format',
value: f.toLowerCase()
}
}
// Storage type
storage
= k:'STORAGE'i __ s:('DISK'i / 'MEMORY'i) {
return {
type: 'storage',
value: s.toLowerCase()
}
}
// DEFAULT expression
default_expr
= KW_DEFAULT __ ce:expr {
return {
type: 'default',
value: ce
}
}
// Index type (BTREE, HASH)
index_type
= KW_USING __
t:("BTREE"i / "HASH"i) {
return {
keyword: 'using',
type: t.toLowerCase(),
}
}
// Index options
index_options
= head:index_option tail:(__ index_option)* {
const result = [head];
for (let i = 0; i < tail.length; i++) {
result.push(tail[i][1]);
}
return result;
}
index_option
= k:KW_KEY_BLOCK_SIZE __ e:(KW_ASSIGIN_EQUAL)? __ kbs:literal_numeric {
return {
type: k.toLowerCase(),
symbol: e,
expr: kbs
};
}
/ index_type
/ "WITH"i __ "PARSER"i __ pn:ident_name {
return {
type: 'with parser',
expr: pn
}
}
/ k:("VISIBLE"i / "INVISIBLE"i) {
return {
type: k.toLowerCase(),
expr: k.toLowerCase()
}
}
/ keyword_comment
// Index definition
create_index_definition
= kc:(KW_INDEX / KW_KEY) __
c:column? __
t:index_type? __
de:cte_column_definition __
id:index_options? {
return {
index: c,
definition: de,
keyword: kc.toLowerCase(),
index_type: t,
resource: 'index',
index_options: id,
}
}
// Fulltext/Spatial index
create_fulltext_spatial_index_definition
= p:(KW_FULLTEXT / KW_SPATIAL) __
kc:(KW_INDEX / KW_KEY)? __
c:column? __
de:cte_column_definition __
id:index_options? {
return {
index: c,
definition: de,
keyword: kc && `${p.toLowerCase()} ${kc.toLowerCase()}` || p.toLowerCase(),
index_options: id,
resource: 'index',
}
}
// Reference definition for foreign keys
reference_definition
= kc:KW_REFERENCES __
t:table_ref_list __
de:cte_column_definition __
m:('MATCH FULL'i / 'MATCH PARTIAL'i / 'MATCH SIMPLE'i)? __
od:on_reference? __
ou:on_reference? {
return {
definition: de,
table: t,
keyword: kc.toLowerCase(),
match: m && m.toLowerCase(),
on_action: [od, ou].filter(v => v)
}
}
/ oa:on_reference {
return {
on_action: [oa]
}
}
on_reference
= KW_ON __ kw:(KW_DELETE / KW_UPDATE) __ ro:reference_option {
return {
type: `on ${kw[0].toLowerCase()}`,
value: ro
}
}
reference_option
= kw:KW_CURRENT_TIMESTAMP __ LPAREN __ l:expr_list? __ RPAREN {
return {
type: 'function',
name: { name: [{ type: 'origin', value: kw }]},
args: l
}
}
/ kc:('RESTRICT'i / 'CASCADE'i / 'SET NULL'i / 'NO ACTION'i / 'SET DEFAULT'i / KW_CURRENT_TIMESTAMP) {
return {
type: 'origin',
value: kc.toLowerCase()
}
}
// Table options
table_options
= head:table_option tail:(__ COMMA? __ table_option)* {
return createList(head, tail)
}
create_option_character_set_kw
= 'CHARACTER'i __ 'SET'i {
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
value: v
}
}
table_option
= kw:('AUTO_INCREMENT'i / 'AVG_ROW_LENGTH'i / 'KEY_BLOCK_SIZE'i / 'MAX_ROWS'i / 'MIN_ROWS'i / 'STATS_SAMPLE_PAGES'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:literal_numeric {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: v.value
}
}
/ create_option_character_set
/ kw:(KW_COMMENT / 'CONNECTION'i) __ s:(KW_ASSIGIN_EQUAL)? __ c:literal_string {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: `'${c.value}'`
}
}
/ kw:'COMPRESSION'i __ s:(KW_ASSIGIN_EQUAL)? __ v:("'"('ZLIB'i / 'LZ4'i / 'NONE'i)"'") {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: v.join('').toUpperCase()
}
}
/ kw:'ENGINE'i __ s:(KW_ASSIGIN_EQUAL)? __ c:ident_name {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: c.toUpperCase()
}
}
// CREATE DATABASE definition
create_db_definition
= head:create_option_character_set tail:(__ create_option_character_set)* {
return createList(head, tail, 1)
}
// IF NOT EXISTS clause
if_not_exists_stmt
= 'IF'i __ KW_NOT __ KW_EXISTS {
return 'IF NOT EXISTS'
}