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
|
const builtin = @import("builtin");
const std = @import("std");
const mem = std.mem;
const testing = std.testing;
/// Blob is used to represent a SQLite BLOB value when binding a parameter or reading a column.
pub const Blob = struct { data: []const u8 };
/// Text is used to represent a SQLite TEXT value when binding a parameter or reading a column.
pub const Text = struct { data: []const u8 };
const BindMarker = union(enum) {
Typed: type,
Untyped: void,
};
pub const ParsedQuery = struct {
const Self = @This();
bind_markers: [128]BindMarker,
nb_bind_markers: usize,
query: [1024]u8,
query_size: usize,
pub fn from(comptime query: []const u8) Self {
const State = enum {
Start,
BindMarker,
BindMarkerType,
};
comptime var buf: [query.len]u8 = undefined;
comptime var pos = 0;
comptime var state = .Start;
comptime var current_bind_marker_type: [256]u8 = undefined;
comptime var current_bind_marker_type_pos = 0;
comptime var parsed_query: ParsedQuery = undefined;
parsed_query.nb_bind_markers = 0;
inline for (query) |c, i| {
switch (state) {
.Start => switch (c) {
'?' => {
state = .BindMarker;
buf[pos] = c;
pos += 1;
},
else => {
buf[pos] = c;
pos += 1;
},
},
.BindMarker => switch (c) {
'{' => {
state = .BindMarkerType;
current_bind_marker_type_pos = 0;
},
else => {
// This is a bind marker without a type.
state = .Start;
parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{ .Untyped = {} };
parsed_query.nb_bind_markers += 1;
buf[pos] = c;
pos += 1;
},
},
.BindMarkerType => switch (c) {
'}' => {
state = .Start;
const typ = parsed_query.parseType(current_bind_marker_type[0..current_bind_marker_type_pos]);
parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{ .Typed = typ };
parsed_query.nb_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 ? so this must be an untyped bind marker.
if (state == .BindMarker) {
parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{ .Untyped = {} };
parsed_query.nb_bind_markers += 1;
}
if (state == .BindMarkerType) {
@compileError("invalid final state " ++ @tagName(state) ++ ", this means you wrote an incomplete bind marker type");
}
mem.copy(u8, &parsed_query.query, &buf);
parsed_query.query_size = pos;
return parsed_query;
}
fn parseType(comptime self: *Self, 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(builtin.TypeInfo{
.Int = builtin.TypeInfo.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);
}
pub fn getQuery(comptime self: *const Self) []const u8 {
return self.query[0..self.query_size];
}
};
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| {
comptime var parsed_query = ParsedQuery.from(tc.query);
testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery());
}
}
test "parsed query: bind markers types" {
const testCase = struct {
query: []const u8,
expected_marker: BindMarker,
};
const testCases = &[_]testCase{
.{
.query = "foobar ?{usize}",
.expected_marker = .{ .Typed = usize },
},
.{
.query = "foobar ?{text}",
.expected_marker = .{ .Typed = Text },
},
.{
.query = "foobar ?{blob}",
.expected_marker = .{ .Typed = Blob },
},
.{
.query = "foobar ?",
.expected_marker = .{ .Untyped = {} },
},
};
inline for (testCases) |tc| {
comptime var parsed_query = ParsedQuery.from(tc.query);
testing.expectEqual(1, parsed_query.nb_bind_markers);
const bind_marker = parsed_query.bind_markers[0];
switch (tc.expected_marker) {
.Typed => |typ| testing.expectEqual(typ, bind_marker.Typed),
.Untyped => |typ| testing.expectEqual(typ, bind_marker.Untyped),
}
}
}
|