diff options
| author | 2021-09-21 15:19:32 +0800 | |
|---|---|---|
| committer | 2021-10-19 08:40:36 +0200 | |
| commit | e23c079848bce2a756e54f890b40e88a05326c86 (patch) | |
| tree | 77411d0806c4117ad5649de96e5e887ed8cb65d4 /query.zig | |
| parent | sqlite: move to new ParsedQuery and BindMarker (diff) | |
| download | zig-sqlite-e23c079848bce2a756e54f890b40e88a05326c86.tar.gz zig-sqlite-e23c079848bce2a756e54f890b40e88a05326c86.tar.xz zig-sqlite-e23c079848bce2a756e54f890b40e88a05326c86.zip | |
ParsedQuery: support :@$ as bind marker mark
Diffstat (limited to '')
| -rw-r--r-- | query.zig | 35 |
1 files changed, 17 insertions, 18 deletions
| @@ -38,7 +38,10 @@ pub const ParsedQuery = struct { | |||
| 38 | inline for (query) |c| { | 38 | inline for (query) |c| { |
| 39 | switch (state) { | 39 | switch (state) { |
| 40 | .Start => switch (c) { | 40 | .Start => switch (c) { |
| 41 | '?' => { | 41 | '?', ':', '@', '$' => { |
| 42 | parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{}; | ||
| 43 | current_bind_marker_type_pos = 0; | ||
| 44 | current_bind_marker_id_pos = 0; | ||
| 42 | state = .BindMarker; | 45 | state = .BindMarker; |
| 43 | buf[pos] = c; | 46 | buf[pos] = c; |
| 44 | pos += 1; | 47 | pos += 1; |
| @@ -49,23 +52,20 @@ pub const ParsedQuery = struct { | |||
| 49 | }, | 52 | }, |
| 50 | }, | 53 | }, |
| 51 | .BindMarker => switch (c) { | 54 | .BindMarker => switch (c) { |
| 55 | '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':' or '@'."), | ||
| 52 | '{' => { | 56 | '{' => { |
| 53 | parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{}; | ||
| 54 | state = .BindMarkerType; | 57 | state = .BindMarkerType; |
| 55 | current_bind_marker_type_pos = 0; | ||
| 56 | }, | 58 | }, |
| 57 | else => { | 59 | else => { |
| 58 | if (std.ascii.isAlpha(c) or std.ascii.isDigit(c)){ | 60 | if (std.ascii.isAlpha(c) or std.ascii.isDigit(c)){ |
| 59 | parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{}; | ||
| 60 | state = .BindMarkerIdentifier; | 61 | state = .BindMarkerIdentifier; |
| 61 | current_bind_marker_id_pos = 0; | ||
| 62 | current_bind_marker_id[current_bind_marker_id_pos] = c; | 62 | current_bind_marker_id[current_bind_marker_id_pos] = c; |
| 63 | current_bind_marker_id_pos += 1; | 63 | current_bind_marker_id_pos += 1; |
| 64 | } else { | 64 | } else { |
| 65 | // This is a bind marker without a type. | 65 | // This is a bind marker without a type. |
| 66 | state = .Start; | 66 | state = .Start; |
| 67 | 67 | ||
| 68 | parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{}; | 68 | parsed_query.bind_markers[parsed_query.nb_bind_markers].typed = null; |
| 69 | parsed_query.nb_bind_markers += 1; | 69 | parsed_query.nb_bind_markers += 1; |
| 70 | } | 70 | } |
| 71 | buf[pos] = c; | 71 | buf[pos] = c; |
| @@ -73,6 +73,7 @@ pub const ParsedQuery = struct { | |||
| 73 | }, | 73 | }, |
| 74 | }, | 74 | }, |
| 75 | .BindMarkerIdentifier => switch (c) { | 75 | .BindMarkerIdentifier => switch (c) { |
| 76 | '?', ':', '@', '$' => @compileError("unregconised multiple '?', ':' or '@'."), | ||
| 76 | '{' => { | 77 | '{' => { |
| 77 | state = .BindMarkerType; | 78 | state = .BindMarkerType; |
| 78 | current_bind_marker_type_pos = 0; | 79 | current_bind_marker_type_pos = 0; |
| @@ -123,9 +124,7 @@ pub const ParsedQuery = struct { | |||
| 123 | } else if (state == .BindMarkerIdentifier) { | 124 | } else if (state == .BindMarkerIdentifier) { |
| 124 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = std.fmt.comptimePrint("{s}", .{current_bind_marker_id[0..current_bind_marker_id_pos]}); | 125 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = std.fmt.comptimePrint("{s}", .{current_bind_marker_id[0..current_bind_marker_id_pos]}); |
| 125 | parsed_query.nb_bind_markers += 1; | 126 | parsed_query.nb_bind_markers += 1; |
| 126 | } | 127 | } else if (state == .BindMarkerType) { |
| 127 | |||
| 128 | if (state == .BindMarkerType) { | ||
| 129 | @compileError("invalid final state " ++ @tagName(state) ++ ", this means you wrote an incomplete bind marker type"); | 128 | @compileError("invalid final state " ++ @tagName(state) ++ ", this means you wrote an incomplete bind marker type"); |
| 130 | } | 129 | } |
| 131 | 130 | ||
| @@ -247,7 +246,7 @@ test "parsed query: bind markers identifier" { | |||
| 247 | 246 | ||
| 248 | const testCases = &[_]testCase{ | 247 | const testCases = &[_]testCase{ |
| 249 | .{ | 248 | .{ |
| 250 | .query = "foobar ?ABC{usize}", | 249 | .query = "foobar @ABC{usize}", |
| 251 | .expected_marker = .{ .identifier = "ABC" }, | 250 | .expected_marker = .{ .identifier = "ABC" }, |
| 252 | }, | 251 | }, |
| 253 | .{ | 252 | .{ |
| @@ -255,7 +254,7 @@ test "parsed query: bind markers identifier" { | |||
| 255 | .expected_marker = .{ .identifier = "123" }, | 254 | .expected_marker = .{ .identifier = "123" }, |
| 256 | }, | 255 | }, |
| 257 | .{ | 256 | .{ |
| 258 | .query = "foobar ?abc{blob}", | 257 | .query = "foobar $abc{blob}", |
| 259 | .expected_marker = .{ .identifier = "abc" }, | 258 | .expected_marker = .{ .identifier = "abc" }, |
| 260 | }, | 259 | }, |
| 261 | .{ | 260 | .{ |
| @@ -267,7 +266,7 @@ test "parsed query: bind markers identifier" { | |||
| 267 | inline for (testCases) |tc| { | 266 | inline for (testCases) |tc| { |
| 268 | comptime var parsed_query = ParsedQuery.from(tc.query); | 267 | comptime var parsed_query = ParsedQuery.from(tc.query); |
| 269 | 268 | ||
| 270 | try testing.expectEqual(1, parsed_query.nb_bind_markers); | 269 | try testing.expectEqual(@as(usize, 1), parsed_query.nb_bind_markers); |
| 271 | 270 | ||
| 272 | const bind_marker = parsed_query.bind_markers[0]; | 271 | const bind_marker = parsed_query.bind_markers[0]; |
| 273 | try testing.expectEqualStrings(tc.expected_marker.identifier.?, bind_marker.identifier.?); | 272 | try testing.expectEqualStrings(tc.expected_marker.identifier.?, bind_marker.identifier.?); |
| @@ -282,16 +281,16 @@ test "parsed query: query bind identifier" { | |||
| 282 | 281 | ||
| 283 | const testCases = &[_]testCase{ | 282 | const testCases = &[_]testCase{ |
| 284 | .{ | 283 | .{ |
| 285 | .query = "INSERT INTO user(id, name, age) VALUES(?id{usize}, ?name{[]const u8}, ?age{u32})", | 284 | .query = "INSERT INTO user(id, name, age) VALUES(@id{usize}, :name{[]const u8}, $age{u32})", |
| 286 | .expected_query = "INSERT INTO user(id, name, age) VALUES(?id, ?name, ?age)", | 285 | .expected_query = "INSERT INTO user(id, name, age) VALUES(@id, :name, $age)", |
| 287 | }, | 286 | }, |
| 288 | .{ | 287 | .{ |
| 289 | .query = "SELECT id, name, age FROM user WHER age > ?ageGT{u32} AND age < ?ageLT{u32}", | 288 | .query = "SELECT id, name, age FROM user WHER age > :ageGT{u32} AND age < @ageLT{u32}", |
| 290 | .expected_query = "SELECT id, name, age FROM user WHER age > ?ageGT AND age < ?ageLT", | 289 | .expected_query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < @ageLT", |
| 291 | }, | 290 | }, |
| 292 | .{ | 291 | .{ |
| 293 | .query = "SELECT id, name, age FROM user WHER age > ?ageGT AND age < ?ageLT", | 292 | .query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < $ageLT", |
| 294 | .expected_query = "SELECT id, name, age FROM user WHER age > ?ageGT AND age < ?ageLT", | 293 | .expected_query = "SELECT id, name, age FROM user WHER age > :ageGT AND age < $ageLT", |
| 295 | }, | 294 | }, |
| 296 | }; | 295 | }; |
| 297 | 296 | ||