-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete_parser.h
More file actions
358 lines (303 loc) · 12.8 KB
/
Copy pathdelete_parser.h
File metadata and controls
358 lines (303 loc) · 12.8 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
#ifndef SQL_PARSER_DELETE_PARSER_H
#define SQL_PARSER_DELETE_PARSER_H
#include "sql_parser/common.h"
#include "sql_parser/token.h"
#include "sql_parser/tokenizer.h"
#include "sql_parser/ast.h"
#include "sql_parser/arena.h"
#include "sql_parser/expression_parser.h"
#include "sql_parser/table_ref_parser.h"
namespace sql_parser {
// Flags on NODE_DELETE_STMT
static constexpr uint16_t FLAG_DELETE_MULTI_TABLE = 0x01; // multi-table form
static constexpr uint16_t FLAG_DELETE_FORM2 = 0x02; // MySQL form 2 (DELETE FROM ... USING)
template <Dialect D>
class DeleteParser {
public:
DeleteParser(Tokenizer<D>& tokenizer, Arena& arena)
: tok_(tokenizer), arena_(arena), expr_parser_(tokenizer, arena),
table_ref_parser_(tokenizer, arena, expr_parser_) {}
void set_subquery_callback(SubqueryParseCallback<D> cb) {
expr_parser_.set_subquery_callback(cb);
}
// Parse DELETE statement (DELETE keyword already consumed).
AstNode* parse() {
AstNode* root = make_node(arena_, NodeType::NODE_DELETE_STMT);
if (!root) return nullptr;
if constexpr (D == Dialect::MySQL) {
return parse_mysql(root);
} else {
return parse_pgsql(root);
}
}
private:
Tokenizer<D>& tok_;
Arena& arena_;
ExpressionParser<D> expr_parser_;
TableRefParser<D> table_ref_parser_;
// ---- MySQL DELETE ----
// Single-table: DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM table [WHERE] [ORDER BY] [LIMIT]
// Multi-table form 1: DELETE [opts] t1, t2 FROM table_refs [WHERE]
// Multi-table form 2: DELETE [opts] FROM t1, t2 USING table_refs [WHERE]
AstNode* parse_mysql(AstNode* root) {
// Options: LOW_PRIORITY, QUICK, IGNORE
AstNode* opts = parse_stmt_options();
if (opts) root->add_child(opts);
if (tok_.peek().type == TokenType::TK_FROM) {
// Could be single-table or multi-table form 2
tok_.skip(); // consume FROM
// Parse the first table reference
AstNode* first_table = parse_simple_table_ref();
if (!first_table) return root;
// Check if comma follows (target list) or if USING follows
if (tok_.peek().type == TokenType::TK_COMMA || tok_.peek().type == TokenType::TK_USING) {
// Could be multi-table form 2: DELETE FROM t1[, t2] USING ...
// Or single-table with comma would be unusual. Check for USING after table list.
// Collect all target tables
root->add_child(first_table);
while (tok_.peek().type == TokenType::TK_COMMA) {
tok_.skip();
AstNode* next_table = parse_simple_table_ref();
if (next_table) root->add_child(next_table);
}
if (tok_.peek().type == TokenType::TK_USING) {
// Multi-table form 2
tok_.skip(); // consume USING
root->flags = FLAG_DELETE_MULTI_TABLE | FLAG_DELETE_FORM2;
// Parse source table references (with JOINs)
AstNode* using_clause = make_node(arena_, NodeType::NODE_DELETE_USING_CLAUSE);
AstNode* from = table_ref_parser_.parse_from_clause();
if (from) {
// Move children of FROM_CLAUSE into USING_CLAUSE
for (AstNode* c = from->first_child; c; ) {
AstNode* next = c->next_sibling;
c->next_sibling = nullptr;
using_clause->add_child(c);
c = next;
}
}
root->add_child(using_clause);
// WHERE
if (tok_.peek().type == TokenType::TK_WHERE) {
tok_.skip();
AstNode* where = parse_where_clause();
if (where) root->add_child(where);
}
} else {
// Single-table (just one target table, no USING)
// Parse optional WHERE, ORDER BY, LIMIT
if (tok_.peek().type == TokenType::TK_WHERE) {
tok_.skip();
AstNode* where = parse_where_clause();
if (where) root->add_child(where);
}
if (tok_.peek().type == TokenType::TK_ORDER) {
tok_.skip();
if (tok_.peek().type == TokenType::TK_BY) tok_.skip();
AstNode* order_by = parse_order_by();
if (order_by) root->add_child(order_by);
}
if (tok_.peek().type == TokenType::TK_LIMIT) {
tok_.skip();
AstNode* limit = parse_limit();
if (limit) root->add_child(limit);
}
}
} else {
// Single-table DELETE: DELETE FROM table [WHERE] [ORDER BY] [LIMIT]
root->add_child(first_table);
if (tok_.peek().type == TokenType::TK_WHERE) {
tok_.skip();
AstNode* where = parse_where_clause();
if (where) root->add_child(where);
}
if (tok_.peek().type == TokenType::TK_ORDER) {
tok_.skip();
if (tok_.peek().type == TokenType::TK_BY) tok_.skip();
AstNode* order_by = parse_order_by();
if (order_by) root->add_child(order_by);
}
if (tok_.peek().type == TokenType::TK_LIMIT) {
tok_.skip();
AstNode* limit = parse_limit();
if (limit) root->add_child(limit);
}
}
} else {
// Multi-table form 1: DELETE t1[, t2] FROM table_refs [WHERE]
root->flags = FLAG_DELETE_MULTI_TABLE;
// Parse target table list
AstNode* first_target = parse_simple_table_ref();
if (first_target) root->add_child(first_target);
while (tok_.peek().type == TokenType::TK_COMMA) {
tok_.skip();
AstNode* next_target = parse_simple_table_ref();
if (next_target) root->add_child(next_target);
}
// Expect FROM
if (tok_.peek().type == TokenType::TK_FROM) {
tok_.skip();
// Parse source table references (with JOINs)
AstNode* from = table_ref_parser_.parse_from_clause();
if (from) root->add_child(from);
}
// WHERE
if (tok_.peek().type == TokenType::TK_WHERE) {
tok_.skip();
AstNode* where = parse_where_clause();
if (where) root->add_child(where);
}
}
return root;
}
// ---- PostgreSQL DELETE ----
// DELETE FROM [ONLY] table [[AS] alias] [USING using_list] [WHERE] [RETURNING]
AstNode* parse_pgsql(AstNode* root) {
// Consume FROM
if (tok_.peek().type == TokenType::TK_FROM) {
tok_.skip();
}
// Optional ONLY keyword
if (tok_.peek().type == TokenType::TK_ONLY) {
AstNode* opts = make_node(arena_, NodeType::NODE_STMT_OPTIONS);
Token only_tok = tok_.next_token();
opts->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER, only_tok.text));
root->add_child(opts);
}
// Single table reference with optional alias
AstNode* table_ref = table_ref_parser_.parse_table_reference();
if (table_ref) root->add_child(table_ref);
// USING clause
if (tok_.peek().type == TokenType::TK_USING) {
tok_.skip();
AstNode* using_clause = make_node(arena_, NodeType::NODE_DELETE_USING_CLAUSE);
// Parse table list (comma-separated, potentially with JOINs)
while (true) {
AstNode* tref = table_ref_parser_.parse_table_reference();
if (tref) using_clause->add_child(tref);
if (tok_.peek().type == TokenType::TK_COMMA) {
tok_.skip();
} else {
break;
}
}
root->add_child(using_clause);
}
// WHERE
if (tok_.peek().type == TokenType::TK_WHERE) {
tok_.skip();
AstNode* where = parse_where_clause();
if (where) root->add_child(where);
}
// RETURNING
if (tok_.peek().type == TokenType::TK_RETURNING) {
AstNode* ret = parse_returning();
if (ret) root->add_child(ret);
}
return root;
}
// ---- Shared helpers ----
// Parse a simple table reference (name or schema.name, no alias parsing for target tables)
AstNode* parse_simple_table_ref() {
AstNode* ref = make_node(arena_, NodeType::NODE_TABLE_REF);
if (!ref) return nullptr;
Token name = tok_.next_token();
if (tok_.peek().type == TokenType::TK_DOT) {
tok_.skip();
Token table_name = tok_.next_token();
AstNode* qname = make_node(arena_, NodeType::NODE_QUALIFIED_NAME);
qname->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER, name.text));
qname->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER, table_name.text));
ref->add_child(qname);
} else {
ref->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER, name.text));
}
return ref;
}
// Parse MySQL options: LOW_PRIORITY, QUICK, IGNORE
AstNode* parse_stmt_options() {
AstNode* opts = nullptr;
while (true) {
Token t = tok_.peek();
if (t.type == TokenType::TK_LOW_PRIORITY ||
t.type == TokenType::TK_QUICK ||
t.type == TokenType::TK_IGNORE) {
if (!opts) opts = make_node(arena_, NodeType::NODE_STMT_OPTIONS);
tok_.skip();
opts->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER, t.text));
} else {
break;
}
}
return opts;
}
// Parse WHERE clause
AstNode* parse_where_clause() {
AstNode* where = make_node(arena_, NodeType::NODE_WHERE_CLAUSE);
if (!where) return nullptr;
AstNode* expr = expr_parser_.parse();
if (expr) where->add_child(expr);
return where;
}
// Parse ORDER BY clause
AstNode* parse_order_by() {
AstNode* order_by = make_node(arena_, NodeType::NODE_ORDER_BY_CLAUSE);
if (!order_by) return nullptr;
while (true) {
AstNode* expr = expr_parser_.parse();
if (!expr) break;
AstNode* item = make_node(arena_, NodeType::NODE_ORDER_BY_ITEM);
item->add_child(expr);
// Optional ASC/DESC
Token dir = tok_.peek();
if (dir.type == TokenType::TK_ASC || dir.type == TokenType::TK_DESC) {
tok_.skip();
item->add_child(make_node(arena_, NodeType::NODE_IDENTIFIER,
dir.type == TokenType::TK_ASC ? StringRef{"ASC", 3}
: StringRef{"DESC", 4}));
}
order_by->add_child(item);
if (tok_.peek().type == TokenType::TK_COMMA) {
tok_.skip();
} else {
break;
}
}
return order_by;
}
// Parse LIMIT clause
AstNode* parse_limit() {
AstNode* limit = make_node(arena_, NodeType::NODE_LIMIT_CLAUSE);
if (!limit) return nullptr;
AstNode* count = expr_parser_.parse();
if (count) limit->add_child(count);
return limit;
}
// Parse PostgreSQL RETURNING clause
AstNode* parse_returning() {
if (tok_.peek().type != TokenType::TK_RETURNING) return nullptr;
tok_.skip(); // RETURNING
AstNode* ret = make_node(arena_, NodeType::NODE_RETURNING_CLAUSE);
if (!ret) return nullptr;
while (true) {
AstNode* expr = expr_parser_.parse();
if (!expr) break;
ret->add_child(expr);
// Check for optional alias
Token next = tok_.peek();
if (next.type == TokenType::TK_AS) {
tok_.skip();
Token alias_name = tok_.next_token();
ret->add_child(make_node(arena_, NodeType::NODE_ALIAS, alias_name.text));
}
if (tok_.peek().type == TokenType::TK_COMMA) {
tok_.skip();
} else {
break;
}
}
return ret;
}
};
} // namespace sql_parser
#endif // SQL_PARSER_DELETE_PARSER_H