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
364 lines (331 loc) · 8.27 KB
/
core.pegjs
File metadata and controls
364 lines (331 loc) · 8.27 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Core SQL statement patterns shared across SQL dialects
// Note: Some dialects may override specific rules for custom behavior
// Multiple statements separated by semicolons
multiple_stmt
= head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* {
const headAst = head && head.ast || head
const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst;
for (let i = 0; i < tail.length; i++) {
if(!tail[i][3] || tail[i][3].length === 0) continue;
cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]);
}
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: cur
}
}
// Command statements
cmd_stmt
= drop_stmt
/ create_stmt
/ truncate_stmt
/ rename_stmt
/ call_stmt
/ use_stmt
/ alter_stmt
/ set_stmt
/ lock_stmt
/ unlock_stmt
// CRUD statements
crud_stmt
= union_stmt
/ update_stmt
/ replace_insert_stmt
/ insert_no_columns_stmt
/ insert_into_set
/ delete_stmt
/ cmd_stmt
/ proc_stmts
// Set operations (UNION, INTERSECT, EXCEPT)
set_op
= u:(KW_UNION / KW_INTERSECT / KW_EXCEPT) __ s:(KW_ALL / KW_DISTINCT)? {
return s ? `${u.toLowerCase()} ${s.toLowerCase()}` : `${u.toLowerCase()}`
}
/ KW_MINUS { return 'minus' }
// Union statement
union_stmt
= head:select_stmt tail:(__ set_op __ select_stmt)* __ ob:order_by_clause? __ l:limit_clause? {
let cur = head
for (let i = 0; i < tail.length; i++) {
cur._next = tail[i][3]
cur.set_op = tail[i][1]
cur = cur._next
}
if(ob) head._orderby = ob
if(l) head._limit = l
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: head
}
}
// SELECT statement wrapper
select_stmt
= select_stmt_nake
/ s:('(' __ select_stmt __ ')') {
return {
...s[2],
parentheses_symbol: true,
}
}
// WITH clause (Common Table Expression)
with_clause
= KW_WITH __ head:cte_definition tail:(__ COMMA __ cte_definition)* {
return createList(head, tail);
}
/ __ KW_WITH __ KW_RECURSIVE __ cte:cte_definition tail:(__ COMMA __ cte_definition)* {
cte.recursive = true;
return createList(cte, tail);
}
// CTE definition
cte_definition
= name:(literal_string / ident_name) __ columns:cte_column_definition? __ KW_AS __ LPAREN __ stmt:union_stmt __ RPAREN {
if (typeof name === 'string') name = { type: 'default', value: name }
return { name, stmt, columns };
}
// CTE column definition
cte_column_definition
= LPAREN __ l:column_ref_list __ RPAREN {
return l
}
// Core SELECT statement structure
select_stmt_nake
= __ cte:with_clause? __ KW_SELECT ___
opts:option_clause? __
d:KW_DISTINCT? __
c:column_clause __
f:from_clause? __
w:where_clause? __
g:group_by_clause? __
h:having_clause? __
o:order_by_clause? __
l:limit_clause? __ {
if(f) f.forEach(info => info.table && tableList.add(`select::${info.db}::${info.table}`));
return {
with: cte,
type: 'select',
options: opts,
distinct: d,
columns: c,
from: f,
where: w,
groupby: g,
having: h,
orderby: o,
limit: l
};
}
// MySQL query options
option_clause
= head:query_option tail:(__ query_option)* {
const opts = [head];
for (let i = 0, l = tail.length; i < l; ++i) {
opts.push(tail[i][1]);
}
return opts;
}
query_option
= option:(
OPT_SQL_CALC_FOUND_ROWS
/ (OPT_SQL_CACHE / OPT_SQL_NO_CACHE)
/ OPT_SQL_BIG_RESULT
/ OPT_SQL_SMALL_RESULT
/ OPT_SQL_BUFFER_RESULT
) { return option; }
// Column clause for SELECT
column_clause
= head:(KW_ALL / (STAR !ident_start) / STAR) tail:(__ COMMA __ column_list_item)* {
columnList.add('select::null::(.*)')
const item = {
expr: {
type: 'column_ref',
table: null,
column: '*'
},
as: null
}
if (tail && tail.length > 0) return createList(item, tail)
return [item]
}
/ head:column_list_item tail:(__ COMMA __ column_list_item)* {
return createList(head, tail);
}
// Column list item (with optional alias)
column_list_item
= tbl:(ident __ DOT)? __ STAR {
const table = tbl && tbl[0] || null
columnList.add(`select::${table}::(.*)`);
return {
expr: {
type: 'column_ref',
table: table,
column: '*'
},
as: null
};
}
/ e:binary_column_expr __ alias:alias_clause? {
if (e.type === 'double_quote_string' || e.type === 'single_quote_string') {
columnList.add(`select::null::${e.value}`)
}
return { expr: e, as: alias };
}
// FROM clause
from_clause
= KW_FROM __ l:table_ref_list { return l; }
// WHERE clause
where_clause
= KW_WHERE __ e:or_and_where_expr { return e; }
// GROUP BY clause
group_by_clause
= KW_GROUP __ KW_BY __ e:expr_list {
return {
columns: e.value
}
}
// HAVING clause
having_clause
= KW_HAVING __ e:or_and_where_expr { return e; }
// ORDER BY clause
order_by_clause
= KW_ORDER __ KW_BY __ l:order_by_list { return l; }
order_by_list
= head:order_by_element tail:(__ COMMA __ order_by_element)* {
return createList(head, tail);
}
order_by_element
= e:expr __ d:(KW_DESC / KW_ASC)? {
const obj = { expr: e, type: d };
return obj;
}
// LIMIT clause (basic version - dialects may override)
limit_clause
= KW_LIMIT __ i1:number_or_param __ tail:(COMMA __ number_or_param)? {
const res = { value: [i1] }
if (tail) res.value.push(tail[2])
return res
}
/ KW_LIMIT __ i1:number_or_param __ KW_OFFSET __ i2:number_or_param {
return {
value: [i1],
offset: i2
}
}
number_or_param
= literal_numeric
/ param
// USE statement
use_stmt
= KW_USE __
d:ident {
tableList.add(`use::${d}::null`);
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'use',
db: d
}
};
}
// CALL statement
call_stmt
= KW_CALL __
e:proc_func_call {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'call',
expr: e
}
}
}
// RENAME statement
rename_stmt
= KW_RENAME __
KW_TABLE __
t:table_to_list {
t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${dt.db}::${dt.table}`)))
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'rename',
table: t
}
};
}
// SET statement
set_stmt
= KW_SET __
kw:(KW_GLOBAL / KW_SESSION / KW_LOCAL / KW_PERSIST / KW_PERSIST_ONLY)? __
a:assign_stmt_list {
a.keyword = kw
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'set',
keyword: kw,
expr: a
}
}
}
// LOCK/UNLOCK statements
unlock_stmt
= KW_UNLOCK __ KW_TABLES {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'unlock',
keyword: 'tables'
}
}
}
lock_type
= "READ"i __ s:("LOCAL"i)? {
return {
type: 'read',
suffix: s && 'local'
}
}
/ p:("LOW_PRIORITY"i)? __ "WRITE"i {
return {
type: 'write',
prefix: p && 'low_priority'
}
}
lock_table
= t:table_base __ lt:lock_type {
tableList.add(`lock::${t.db}::${t.table}`)
return {
table: t,
lock_type: lt
}
}
lock_table_list
= head:lock_table tail:(__ COMMA __ lock_table)* {
return createList(head, tail);
}
lock_stmt
= KW_LOCK __ KW_TABLES __ ltl:lock_table_list {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'lock',
keyword: 'tables',
tables: ltl
}
}
}
// Expression list
expr_list
= head:expr tail:(__ COMMA __ expr)* {
const el = { type: 'expr_list' };
el.value = createList(head, tail);
return el;
}