diff options
| author | 2021-12-18 22:27:32 +0100 | |
|---|---|---|
| committer | 2021-12-18 22:46:40 +0100 | |
| commit | 18951b9d8e1903d97a6b97a094cc03b0b87651bc (patch) | |
| tree | 9c8513a001e0bf784482072abd5ddb9a9d60b42d | |
| parent | readme: add a note about sqlite compile-time options (diff) | |
| download | zig-sqlite-18951b9d8e1903d97a6b97a094cc03b0b87651bc.tar.gz zig-sqlite-18951b9d8e1903d97a6b97a094cc03b0b87651bc.tar.xz zig-sqlite-18951b9d8e1903d97a6b97a094cc03b0b87651bc.zip | |
add a test for bind markers inside strings
| -rw-r--r-- | query.zig | 32 |
1 files changed, 31 insertions, 1 deletions
| @@ -95,7 +95,7 @@ pub const ParsedQuery = struct { | |||
| 95 | state = .BindMarkerType; | 95 | state = .BindMarkerType; |
| 96 | current_bind_marker_type_pos = 0; | 96 | current_bind_marker_type_pos = 0; |
| 97 | 97 | ||
| 98 | // A bind marker with id and type: ?AAA{[]const u8}, we don't need move the pointer. | 98 | // A bind marker with id and type: ?AAA{[]const u8}, we don't need to move the pointer. |
| 99 | if (current_bind_marker_id_pos > 0) { | 99 | if (current_bind_marker_id_pos > 0) { |
| 100 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = current_bind_marker_id[0..current_bind_marker_id_pos]; | 100 | parsed_query.bind_markers[parsed_query.nb_bind_markers].identifier = current_bind_marker_id[0..current_bind_marker_id_pos]; |
| 101 | } | 101 | } |
| @@ -218,6 +218,7 @@ test "parsed query: query" { | |||
| 218 | }; | 218 | }; |
| 219 | 219 | ||
| 220 | inline for (testCases) |tc| { | 220 | inline for (testCases) |tc| { |
| 221 | @setEvalBranchQuota(100000); | ||
| 221 | comptime var parsed_query = ParsedQuery.from(tc.query); | 222 | comptime var parsed_query = ParsedQuery.from(tc.query); |
| 222 | try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery()); | 223 | try testing.expectEqualStrings(tc.expected_query, parsed_query.getQuery()); |
| 223 | } | 224 | } |
| @@ -353,3 +354,32 @@ test "parsed query: bind markers identifier type" { | |||
| 353 | try testing.expectEqual(tc.expected_marker.identifier_type, bind_marker.identifier_type); | 354 | try testing.expectEqual(tc.expected_marker.identifier_type, bind_marker.identifier_type); |
| 354 | } | 355 | } |
| 355 | } | 356 | } |
| 357 | |||
| 358 | test "parsed query: bind marker character inside string" { | ||
| 359 | const testCase = struct { | ||
| 360 | query: []const u8, | ||
| 361 | exp_bind_markers: comptime_int, | ||
| 362 | exp: []const u8, | ||
| 363 | }; | ||
| 364 | |||
| 365 | const testCases = &[_]testCase{ | ||
| 366 | .{ | ||
| 367 | .query = "SELECT json_extract(metadata, '$.name') AS name FROM foobar", | ||
| 368 | .exp_bind_markers = 0, | ||
| 369 | .exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar", | ||
| 370 | }, | ||
| 371 | .{ | ||
| 372 | .query = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name{text}", | ||
| 373 | .exp_bind_markers = 1, | ||
| 374 | .exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name", | ||
| 375 | }, | ||
| 376 | }; | ||
| 377 | |||
| 378 | inline for (testCases) |tc| { | ||
| 379 | @setEvalBranchQuota(100000); | ||
| 380 | comptime var parsed_query = ParsedQuery.from(tc.query); | ||
| 381 | |||
| 382 | try testing.expectEqual(@as(usize, tc.exp_bind_markers), parsed_query.nb_bind_markers); | ||
| 383 | try testing.expectEqualStrings(tc.exp, parsed_query.getQuery()); | ||
| 384 | } | ||
| 385 | } | ||