-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremote_query_builder.h
More file actions
240 lines (209 loc) · 7.09 KB
/
Copy pathremote_query_builder.h
File metadata and controls
240 lines (209 loc) · 7.09 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
#ifndef SQL_ENGINE_REMOTE_QUERY_BUILDER_H
#define SQL_ENGINE_REMOTE_QUERY_BUILDER_H
#include "sql_engine/catalog.h"
#include "sql_parser/arena.h"
#include "sql_parser/string_builder.h"
#include "sql_parser/emitter.h"
#include "sql_parser/common.h"
#include "sql_parser/ast.h"
#include <cstdio>
namespace sql_engine {
template <sql_parser::Dialect D>
class RemoteQueryBuilder {
public:
explicit RemoteQueryBuilder(sql_parser::Arena& arena)
: arena_(arena) {}
// Build a SELECT statement string from plan components.
// Returns an arena-allocated StringRef.
sql_parser::StringRef build_select(
const TableInfo* table,
const sql_parser::AstNode* where_expr, // nullable
const sql_parser::AstNode** project_exprs, // nullable = SELECT *
uint16_t project_count,
const sql_parser::AstNode** group_by, // nullable
uint16_t group_count,
const sql_parser::AstNode** order_keys, // nullable
uint8_t* order_dirs,
uint16_t order_count,
int64_t limit, // -1 = no limit
bool distinct)
{
sql_parser::StringBuilder sb(arena_, 512);
sb.append("SELECT ");
if (distinct) {
sb.append("DISTINCT ");
}
// Projection
if (project_exprs && project_count > 0) {
for (uint16_t i = 0; i < project_count; ++i) {
if (i > 0) sb.append(", ");
emit_expr(project_exprs[i], sb);
}
} else {
sb.append_char('*');
}
// FROM
if (table) {
sb.append(" FROM ");
sb.append(table->table_name.ptr, table->table_name.len);
}
// WHERE
if (where_expr) {
sb.append(" WHERE ");
emit_expr(where_expr, sb);
}
// GROUP BY
if (group_by && group_count > 0) {
sb.append(" GROUP BY ");
for (uint16_t i = 0; i < group_count; ++i) {
if (i > 0) sb.append(", ");
emit_expr(group_by[i], sb);
}
}
// ORDER BY
if (order_keys && order_count > 0) {
sb.append(" ORDER BY ");
for (uint16_t i = 0; i < order_count; ++i) {
if (i > 0) sb.append(", ");
emit_expr(order_keys[i], sb);
if (order_dirs && order_dirs[i] == 1) {
sb.append(" DESC");
}
}
}
// LIMIT
if (limit >= 0) {
sb.append(" LIMIT ");
char buf[32];
int n = snprintf(buf, sizeof(buf), "%lld", (long long)limit);
sb.append(buf, n);
}
return sb.finish();
}
sql_parser::StringRef build_select_join(
const TableInfo* left,
const TableInfo* right,
const sql_parser::AstNode* on_expr,
const sql_parser::AstNode* where_expr)
{
sql_parser::StringBuilder sb(arena_, 512);
sb.append("SELECT * FROM ");
if (left) sb.append(left->table_name.ptr, left->table_name.len);
sb.append(" JOIN ");
if (right) sb.append(right->table_name.ptr, right->table_name.len);
if (on_expr) {
sb.append(" ON ");
emit_expr(on_expr, sb);
}
if (where_expr) {
sb.append(" WHERE ");
emit_expr(where_expr, sb);
}
return sb.finish();
}
// Build an INSERT statement string.
sql_parser::StringRef build_insert(
const TableInfo* table,
const sql_parser::AstNode** columns,
uint16_t col_count,
const sql_parser::AstNode** value_rows,
uint16_t row_count)
{
sql_parser::StringBuilder sb(arena_, 512);
sb.append("INSERT INTO ");
if (table) {
sb.append(table->table_name.ptr, table->table_name.len);
}
// Column list
if (columns && col_count > 0) {
sb.append(" (");
for (uint16_t i = 0; i < col_count; ++i) {
if (i > 0) sb.append(", ");
emit_expr(columns[i], sb);
}
sb.append_char(')');
}
// VALUES
if (value_rows && row_count > 0) {
sb.append(" VALUES ");
for (uint16_t r = 0; r < row_count; ++r) {
if (r > 0) sb.append(", ");
sb.append_char('(');
if (value_rows[r]) {
uint16_t vi = 0;
for (const sql_parser::AstNode* val = value_rows[r]->first_child;
val; val = val->next_sibling, ++vi) {
if (vi > 0) sb.append(", ");
emit_expr(val, sb);
}
}
sb.append_char(')');
}
}
return sb.finish();
}
// Build an UPDATE statement string.
sql_parser::StringRef build_update(
const TableInfo* table,
const sql_parser::AstNode** set_cols,
const sql_parser::AstNode** set_exprs,
uint16_t set_count,
const sql_parser::AstNode* where_expr)
{
sql_parser::StringBuilder sb(arena_, 512);
sb.append("UPDATE ");
if (table) {
sb.append(table->table_name.ptr, table->table_name.len);
}
sb.append(" SET ");
for (uint16_t i = 0; i < set_count; ++i) {
if (i > 0) sb.append(", ");
emit_expr(set_cols[i], sb);
sb.append(" = ");
emit_expr(set_exprs[i], sb);
}
if (where_expr) {
sb.append(" WHERE ");
emit_expr(where_expr, sb);
}
return sb.finish();
}
// Build a DELETE statement string.
sql_parser::StringRef build_delete(
const TableInfo* table,
const sql_parser::AstNode* where_expr)
{
sql_parser::StringBuilder sb(arena_, 512);
sb.append("DELETE FROM ");
if (table) {
sb.append(table->table_name.ptr, table->table_name.len);
}
if (where_expr) {
sb.append(" WHERE ");
emit_expr(where_expr, sb);
}
return sb.finish();
}
sql_parser::StringRef build_update_from_ast(const sql_parser::AstNode* ast) {
sql_parser::Emitter<D> emitter(arena_);
emitter.emit(ast);
return emitter.result();
}
sql_parser::StringRef build_delete_from_ast(const sql_parser::AstNode* ast) {
sql_parser::Emitter<D> emitter(arena_);
emitter.emit(ast);
return emitter.result();
}
private:
sql_parser::Arena& arena_;
void emit_expr(const sql_parser::AstNode* expr, sql_parser::StringBuilder& sb) {
if (!expr) return;
// Use the parser's emitter to emit the expression
sql_parser::Emitter<D> emitter(arena_);
emitter.emit(expr);
sql_parser::StringRef result = emitter.result();
sb.append(result.ptr, result.len);
}
};
} // namespace sql_engine
#endif // SQL_ENGINE_REMOTE_QUERY_BUILDER_H