summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar thisLight2021-09-22 07:57:13 +0800
committerGravatar Vincent Rischmann2021-10-19 08:40:36 +0200
commite6bc606532dea62e487dd81c576ab4b4c41bd34d (patch)
tree61f1b2e54b7d7c3c3f1f9677fa19f55171cb5e85
parentParsedQuery: support :@$ as bind marker mark (diff)
downloadzig-sqlite-e6bc606532dea62e487dd81c576ab4b4c41bd34d.tar.gz
zig-sqlite-e6bc606532dea62e487dd81c576ab4b4c41bd34d.tar.xz
zig-sqlite-e6bc606532dea62e487dd81c576ab4b4c41bd34d.zip
BindMarker, ParsedQuery: support BindMarker.idType
Diffstat (limited to '')
-rw-r--r--query.zig50
1 files changed, 48 insertions, 2 deletions
diff --git a/query.zig b/query.zig
index 38a9a08..2213634 100644
--- a/query.zig
+++ b/query.zig
@@ -10,6 +10,12 @@ pub const Text = struct { data: []const u8 };
10const BindMarker = struct { 10const BindMarker = struct {
11 typed: ?type = null, // null == untyped 11 typed: ?type = null, // null == untyped
12 identifier: ?[]const u8 = null, 12 identifier: ?[]const u8 = null,
13 idType: IdType = .Integer,
14
15 pub const IdType = enum {
16 Integer,
17 String,
18 };
13}; 19};
14 20
15pub const ParsedQuery = struct { 21pub const ParsedQuery = struct {
@@ -42,6 +48,7 @@ pub const ParsedQuery = struct {
42 parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{}; 48 parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{};
43 current_bind_marker_type_pos = 0; 49 current_bind_marker_type_pos = 0;
44 current_bind_marker_id_pos = 0; 50 current_bind_marker_id_pos = 0;
51 parsed_query.bind_markers[parsed_query.nb_bind_markers].idType = if (c == '?') .Integer else .String;
45 state = .BindMarker; 52 state = .BindMarker;
46 buf[pos] = c; 53 buf[pos] = c;
47 pos += 1; 54 pos += 1;
@@ -52,7 +59,7 @@ pub const ParsedQuery = struct {
52 }, 59 },
53 }, 60 },
54 .BindMarker => switch (c) { 61 .BindMarker => switch (c) {
55 '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':' or '@'."), 62 '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':', '$' or '@'."),
56 '{' => { 63 '{' => {
57 state = .BindMarkerType; 64 state = .BindMarkerType;
58 }, 65 },
@@ -73,7 +80,7 @@ pub const ParsedQuery = struct {
73 }, 80 },
74 }, 81 },
75 .BindMarkerIdentifier => switch (c) { 82 .BindMarkerIdentifier => switch (c) {
76 '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':' or '@'."), 83 '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':', '$' or '@'."),
77 '{' => { 84 '{' => {
78 state = .BindMarkerType; 85 state = .BindMarkerType;
79 current_bind_marker_type_pos = 0; 86 current_bind_marker_type_pos = 0;
@@ -299,3 +306,42 @@ test "parsed query: query bind identifier" {
299 try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery()); 306 try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery());
300 } 307 }
301} 308}
309
310test "parsed query: bind markers identifier type" {
311 const testCase = struct {
312 query: []const u8,
313 expected_marker: BindMarker,
314 };
315
316 const testCases = &[_]testCase{
317 .{
318 .query = "foobar @ABC{usize}",
319 .expected_marker = .{ .idType = .String },
320 },
321 .{
322 .query = "foobar ?123{text}",
323 .expected_marker = .{ .idType = .Integer },
324 },
325 .{
326 .query = "foobar $abc{blob}",
327 .expected_marker = .{ .idType = .String },
328 },
329 .{
330 .query = "foobar ?123",
331 .expected_marker = .{ .idType = .Integer },
332 },
333 .{
334 .query = "foobar :abc",
335 .expected_marker = .{ .idType = .String },
336 }
337 };
338
339 inline for (testCases) |tc| {
340 comptime var parsed_query = ParsedQuery.from(tc.query);
341
342 try testing.expectEqual(@as(usize, 1), parsed_query.nb_bind_markers);
343
344 const bind_marker = parsed_query.bind_markers[0];
345 try testing.expectEqual(tc.expected_marker.idType, bind_marker.idType);
346 }
347}