diff options
Diffstat (limited to 'query.zig')
| -rw-r--r-- | query.zig | 35 |
1 files changed, 17 insertions, 18 deletions
| @@ -13,12 +13,6 @@ const BindMarker = struct { | |||
| 13 | /// | 13 | /// |
| 14 | /// A null means the bind parameter is untyped so there won't be comptime checking. | 14 | /// A null means the bind parameter is untyped so there won't be comptime checking. |
| 15 | typed: ?type = null, | 15 | typed: ?type = null, |
| 16 | |||
| 17 | // TODO(vincent): both identifier and identifier_type are unused outside of parsing them. | ||
| 18 | // Should we remove them ? | ||
| 19 | |||
| 20 | /// Contains the bind parameter identifier string. | ||
| 21 | identifier: ?[]const u8 = null, | ||
| 22 | }; | 16 | }; |
| 23 | 17 | ||
| 24 | pub const ParsedQuery = struct { | 18 | pub const ParsedQuery = struct { |
| @@ -102,11 +96,6 @@ pub const ParsedQuery = struct { | |||
| 102 | '{' => { | 96 | '{' => { |
| 103 | state = .BindMarkerType; | 97 | state = .BindMarkerType; |
| 104 | current_bind_marker_type_pos = 0; | 98 | current_bind_marker_type_pos = 0; |
| 105 | |||
| 106 | // A bind marker with id and type: ?AAA{[]const u8}, we don't need to move the pointer. | ||
| 107 | if (current_bind_marker_id_pos > 0) { | ||
| 108 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = current_bind_marker_id[0..current_bind_marker_id_pos]; | ||
| 109 | } | ||
| 110 | }, | 99 | }, |
| 111 | else => { | 100 | else => { |
| 112 | if (std.ascii.isAlpha(c) or std.ascii.isDigit(c)) { | 101 | if (std.ascii.isAlpha(c) or std.ascii.isDigit(c)) { |
| @@ -115,7 +104,6 @@ pub const ParsedQuery = struct { | |||
| 115 | } else { | 104 | } else { |
| 116 | state = .Start; | 105 | state = .Start; |
| 117 | if (current_bind_marker_id_pos > 0) { | 106 | if (current_bind_marker_id_pos > 0) { |
| 118 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = current_bind_marker_id[0..current_bind_marker_id_pos]; | ||
| 119 | parsed_query.nb_bind_markers += 1; | 107 | parsed_query.nb_bind_markers += 1; |
| 120 | } | 108 | } |
| 121 | } | 109 | } |
| @@ -150,7 +138,6 @@ pub const ParsedQuery = struct { | |||
| 150 | parsed_query.nb_bind_markers += 1; | 138 | parsed_query.nb_bind_markers += 1; |
| 151 | }, | 139 | }, |
| 152 | .BindMarkerIdentifier => { | 140 | .BindMarkerIdentifier => { |
| 153 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = current_bind_marker_id[0..current_bind_marker_id_pos]; | ||
| 154 | parsed_query.nb_bind_markers += 1; | 141 | parsed_query.nb_bind_markers += 1; |
| 155 | }, | 142 | }, |
| 156 | .Start => {}, | 143 | .Start => {}, |
| @@ -277,19 +264,31 @@ test "parsed query: bind markers identifier" { | |||
| 277 | const testCases = &[_]testCase{ | 264 | const testCases = &[_]testCase{ |
| 278 | .{ | 265 | .{ |
| 279 | .query = "foobar @ABC{usize}", | 266 | .query = "foobar @ABC{usize}", |
| 280 | .expected_marker = .{ .identifier = "ABC" }, | 267 | .expected_marker = .{ .typed = usize }, |
| 281 | }, | 268 | }, |
| 282 | .{ | 269 | .{ |
| 283 | .query = "foobar ?123{text}", | 270 | .query = "foobar ?123{text}", |
| 284 | .expected_marker = .{ .identifier = "123" }, | 271 | .expected_marker = .{ .typed = Text }, |
| 285 | }, | 272 | }, |
| 286 | .{ | 273 | .{ |
| 287 | .query = "foobar $abc{blob}", | 274 | .query = "foobar $abc{blob}", |
| 288 | .expected_marker = .{ .identifier = "abc" }, | 275 | .expected_marker = .{ .typed = Blob }, |
| 276 | }, | ||
| 277 | .{ | ||
| 278 | .query = "foobar :430{u32}", | ||
| 279 | .expected_marker = .{ .typed = u32 }, | ||
| 289 | }, | 280 | }, |
| 290 | .{ | 281 | .{ |
| 291 | .query = "foobar ?123", | 282 | .query = "foobar ?123", |
| 292 | .expected_marker = .{ .identifier = "123" }, | 283 | .expected_marker = .{}, |
| 284 | }, | ||
| 285 | .{ | ||
| 286 | .query = "foobar :hola", | ||
| 287 | .expected_marker = .{}, | ||
| 288 | }, | ||
| 289 | .{ | ||
| 290 | .query = "foobar @foo", | ||
| 291 | .expected_marker = .{}, | ||
| 293 | }, | 292 | }, |
| 294 | }; | 293 | }; |
| 295 | 294 | ||
| @@ -299,7 +298,7 @@ test "parsed query: bind markers identifier" { | |||
| 299 | try testing.expectEqual(@as(usize, 1), parsed_query.nb_bind_markers); | 298 | try testing.expectEqual(@as(usize, 1), parsed_query.nb_bind_markers); |
| 300 | 299 | ||
| 301 | const bind_marker = parsed_query.bind_markers[0]; | 300 | const bind_marker = parsed_query.bind_markers[0]; |
| 302 | try testing.expectEqualStrings(tc.expected_marker.identifier.?, bind_marker.identifier.?); | 301 | try testing.expectEqual(tc.expected_marker, bind_marker); |
| 303 | } | 302 | } |
| 304 | } | 303 | } |
| 305 | 304 | ||