-
Notifications
You must be signed in to change notification settings - Fork 54
/
query.zig
443 lines (393 loc) · 16 KB
/
query.zig
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
const std = @import("std");
const mem = std.mem;
const testing = std.testing;
const Blob = @import("sqlite.zig").Blob;
const Text = @import("sqlite.zig").Text;
const BindMarker = struct {
/// Name of the bind parameter in case it's named.
name: ?[]const u8 = null,
/// Contains the expected type for a bind parameter which will be checked
/// at comptime when calling bind on a statement.
///
/// A null means the bind parameter is untyped so there won't be comptime checking.
typed: ?type = null,
};
fn isNamedIdentifierChar(c: u8) bool {
return std.ascii.isAlphabetic(c) or std.ascii.isDigit(c) or c == '_';
}
fn bindMarkerForName(comptime markers: []const BindMarker, comptime name: []const u8) ?BindMarker {
for (markers) |marker| {
if (marker.name != null and std.mem.eql(u8, marker.name.?, name))
return marker;
}
return null;
}
pub fn ParsedQuery(comptime tmp_query: []const u8) type {
return struct {
const Self = @This();
const result = parse();
pub const bind_markers = result.bind_markers[0..result.bind_markers_len];
pub fn getQuery() []const u8 {
return Self.result.query[0..Self.result.query_len];
}
const ParsedQueryResult = struct {
bind_markers: [128]BindMarker,
bind_markers_len: usize,
query: [tmp_query.len]u8,
query_len: usize,
};
fn parse() ParsedQueryResult {
// This contains the final SQL query after parsing with our
// own typed bind markers removed.
var buf: [tmp_query.len]u8 = undefined;
var pos = 0;
var state = .start;
var current_bind_marker_type: [256]u8 = undefined;
var current_bind_marker_type_pos = 0;
// becomes part of our result
var tmp_bind_markers: [128]BindMarker = undefined;
var nb_tmp_bind_markers: usize = 0;
// used for capturing slices, such as bind parameter name
var hold_pos = 0;
for (tmp_query) |c| {
switch (state) {
.start => switch (c) {
'?', ':', '@', '$' => {
tmp_bind_markers[nb_tmp_bind_markers] = BindMarker{};
current_bind_marker_type_pos = 0;
state = .bind_marker;
buf[pos] = c;
pos += 1;
},
'\'', '"', '[', '`' => {
state = .inside_string;
buf[pos] = c;
pos += 1;
},
else => {
buf[pos] = c;
pos += 1;
},
},
.inside_string => switch (c) {
'\'', '"', ']', '`' => {
state = .start;
buf[pos] = c;
pos += 1;
},
else => {
buf[pos] = c;
pos += 1;
},
},
.bind_marker => switch (c) {
'?', ':', '@', '$' => @compileError("invalid multiple '?', ':', '$' or '@'."),
'{' => {
state = .bind_marker_type;
},
else => {
if (isNamedIdentifierChar(c)) {
// This is the start of a named bind marker.
state = .bind_marker_identifier;
hold_pos = pos + 1;
} else {
// This is a unnamed, untyped bind marker.
state = .start;
tmp_bind_markers[nb_tmp_bind_markers].typed = null;
nb_tmp_bind_markers += 1;
}
buf[pos] = c;
pos += 1;
},
},
.bind_marker_identifier => switch (c) {
'?', ':', '@', '$' => @compileError("unregconised multiple '?', ':', '$' or '@'."),
'{' => {
state = .bind_marker_type;
current_bind_marker_type_pos = 0;
},
else => {
if (!isNamedIdentifierChar(c)) {
// This marks the end of the named bind marker.
state = .start;
const name = buf[hold_pos - 1 .. pos];
// TODO(vincent): name retains a pointer to a comptime var, FIX !
if (bindMarkerForName(tmp_bind_markers[0..nb_tmp_bind_markers], name) == null) {
const new_buf = buf;
tmp_bind_markers[nb_tmp_bind_markers].name = new_buf[hold_pos - 1 .. pos];
nb_tmp_bind_markers += 1;
}
}
buf[pos] = c;
pos += 1;
},
},
.bind_marker_type => switch (c) {
'}' => {
state = .start;
const type_info_string = current_bind_marker_type[0..current_bind_marker_type_pos];
// Handles optional types
const typ = if (type_info_string[0] == '?') blk: {
const child_type = ParseType(type_info_string[1..]);
break :blk @Type(std.builtin.Type{
.optional = .{
.child = child_type,
},
});
} else blk: {
break :blk ParseType(type_info_string);
};
tmp_bind_markers[nb_tmp_bind_markers].typed = typ;
nb_tmp_bind_markers += 1;
},
else => {
current_bind_marker_type[current_bind_marker_type_pos] = c;
current_bind_marker_type_pos += 1;
},
},
else => {
@compileError("invalid state " ++ @tagName(state));
},
}
}
// The last character was a bind marker prefix so this must be an untyped bind marker.
switch (state) {
.bind_marker => {
tmp_bind_markers[nb_tmp_bind_markers].typed = null;
nb_tmp_bind_markers += 1;
},
.bind_marker_identifier => {
const new_buf = buf;
tmp_bind_markers[nb_tmp_bind_markers].name = @as([]const u8, new_buf[hold_pos - 1 .. pos]);
nb_tmp_bind_markers += 1;
},
.start => {},
else => @compileError("invalid final state " ++ @tagName(state) ++ ", this means you wrote an incomplete bind marker type"),
}
const final_bind_markers = tmp_bind_markers;
const final_bind_markers_len = nb_tmp_bind_markers;
const final_buf = buf;
const final_query_len = pos;
return .{
.bind_markers = final_bind_markers,
.bind_markers_len = final_bind_markers_len,
.query = final_buf,
.query_len = final_query_len,
};
}
};
}
fn ParseType(comptime type_info: []const u8) type {
if (type_info.len <= 0) @compileError("invalid type info " ++ type_info);
// Integer
if (mem.eql(u8, "usize", type_info)) return usize;
if (mem.eql(u8, "isize", type_info)) return isize;
if (type_info[0] == 'u' or type_info[0] == 'i') {
return @Type(std.builtin.Type{
.int = std.builtin.Type.Int{
.signedness = if (type_info[0] == 'i') .signed else .unsigned,
.bits = std.fmt.parseInt(usize, type_info[1..type_info.len], 10) catch {
@compileError("invalid type info " ++ type_info);
},
},
});
}
// Float
if (mem.eql(u8, "f16", type_info)) return f16;
if (mem.eql(u8, "f32", type_info)) return f32;
if (mem.eql(u8, "f64", type_info)) return f64;
if (mem.eql(u8, "f128", type_info)) return f128;
// Bool
if (mem.eql(u8, "bool", type_info)) return bool;
// Strings
if (mem.eql(u8, "[]const u8", type_info) or mem.eql(u8, "[]u8", type_info)) {
return []const u8;
}
if (mem.eql(u8, "text", type_info)) return Text;
if (mem.eql(u8, "blob", type_info)) return Blob;
@compileError("invalid type info " ++ type_info);
}
test "parsed query: query" {
const testCase = struct {
query: []const u8,
expected_query: []const u8,
};
const testCases = &[_]testCase{
.{
.query = "INSERT INTO user(id, name, age) VALUES(?{usize}, ?{[]const u8}, ?{u32})",
.expected_query = "INSERT INTO user(id, name, age) VALUES(?, ?, ?)",
},
.{
.query = "SELECT id, name, age FROM user WHER age > ?{u32} AND age < ?{u32}",
.expected_query = "SELECT id, name, age FROM user WHER age > ? AND age < ?",
},
.{
.query = "SELECT id, name, age FROM user WHER age > ? AND age < ?",
.expected_query = "SELECT id, name, age FROM user WHER age > ? AND age < ?",
},
};
inline for (testCases) |tc| {
@setEvalBranchQuota(100000);
const parsed_query = ParsedQuery(tc.query);
try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery());
}
}
test "parsed query: bind markers types" {
const testCase = struct {
query: []const u8,
expected_marker: BindMarker,
};
const prefixes = &[_][]const u8{
"?",
"?123",
":",
":hello",
"$",
"$foobar",
"@",
"@name",
};
inline for (prefixes) |prefix| {
const testCases = &[_]testCase{
.{
.query = "foobar " ++ prefix ++ "{usize}",
.expected_marker = .{ .typed = usize },
},
.{
.query = "foobar " ++ prefix ++ "{text}",
.expected_marker = .{ .typed = Text },
},
.{
.query = "foobar " ++ prefix ++ "{blob}",
.expected_marker = .{ .typed = Blob },
},
.{
.query = "foobar " ++ prefix,
.expected_marker = .{ .typed = null },
},
.{
.query = "foobar " ++ prefix ++ "{?[]const u8}",
.expected_marker = .{ .typed = ?[]const u8 },
},
};
inline for (testCases) |tc| {
@setEvalBranchQuota(100000);
const parsed_query = comptime ParsedQuery(tc.query);
try testing.expectEqual(1, parsed_query.bind_markers.len);
const bind_marker = parsed_query.bind_markers[0];
try testing.expectEqual(tc.expected_marker.typed, bind_marker.typed);
}
}
}
test "parsed query: bind markers identifier" {
const testCase = struct {
query: []const u8,
expected_marker: BindMarker,
};
const testCases = &[_]testCase{
.{
.query = "foobar @ABC{usize}",
.expected_marker = .{ .typed = usize },
},
.{
.query = "foobar ?123{text}",
.expected_marker = .{ .typed = Text },
},
.{
.query = "foobar $abc{blob}",
.expected_marker = .{ .typed = Blob },
},
.{
.query = "foobar :430{u32}",
.expected_marker = .{ .typed = u32 },
},
.{
.query = "foobar ?123",
.expected_marker = .{ .typed = null, .name = "123" },
},
.{
.query = "foobar :hola",
.expected_marker = .{ .typed = null, .name = "hola" },
},
.{
.query = "foobar @foo",
.expected_marker = .{ .typed = null, .name = "foo" },
},
};
inline for (testCases) |tc| {
const parsed_query = comptime ParsedQuery(tc.query);
try testing.expectEqual(@as(usize, 1), parsed_query.bind_markers.len);
const bind_marker = parsed_query.bind_markers[0];
if (bind_marker.name) |name| {
try testing.expectEqualStrings(tc.expected_marker.name.?, name);
}
try testing.expectEqual(tc.expected_marker.typed, bind_marker.typed);
}
}
test "parsed query: query bind identifier" {
const testCase = struct {
query: []const u8,
expected_query: []const u8,
expected_nb_bind_markers: usize,
};
const testCases = &[_]testCase{
.{
.query = "INSERT INTO user(id, name, age) VALUES(@id{usize}, :name{[]const u8}, $age{u32})",
.expected_query = "INSERT INTO user(id, name, age) VALUES(@id, :name, $age)",
.expected_nb_bind_markers = 3,
},
.{
.query = "INSERT INTO user(id, name, age) VALUES($id, $name, $age)",
.expected_query = "INSERT INTO user(id, name, age) VALUES($id, $name, $age)",
.expected_nb_bind_markers = 3,
},
.{
.query = "SELECT id, name, age FROM user WHER age > :ageGT{u32} AND age < @ageLT{u32}",
.expected_query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < @ageLT",
.expected_nb_bind_markers = 2,
},
.{
.query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < $ageLT",
.expected_query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < $ageLT",
.expected_nb_bind_markers = 2,
},
.{
.query = "SELECT id, name, age FROM user WHER age > $my_age{i32} AND age < :your_age{i32}",
.expected_query = "SELECT id, name, age FROM user WHER age > $my_age AND age < :your_age",
.expected_nb_bind_markers = 2,
},
};
inline for (testCases) |tc| {
@setEvalBranchQuota(100000);
comptime {
const parsed_query = ParsedQuery(tc.query);
try testing.expectEqual(tc.expected_nb_bind_markers, parsed_query.bind_markers.len);
try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery());
}
}
}
test "parsed query: bind marker character inside string" {
const testCase = struct {
query: []const u8,
exp_bind_markers: comptime_int,
exp: []const u8,
};
const testCases = &[_]testCase{
.{
.query = "SELECT json_extract(metadata, '$.name') AS name FROM foobar",
.exp_bind_markers = 0,
.exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar",
},
.{
.query = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name{text}",
.exp_bind_markers = 1,
.exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name",
},
};
inline for (testCases) |tc| {
@setEvalBranchQuota(100000);
const parsed_query = ParsedQuery(tc.query);
try testing.expectEqual(@as(usize, tc.exp_bind_markers), parsed_query.bind_markers.len);
try testing.expectEqualStrings(tc.exp, parsed_query.getQuery());
}
}