diff options
| author | 2025-06-10 00:01:24 +0200 | |
|---|---|---|
| committer | 2025-06-10 00:01:24 +0200 | |
| commit | be8b4965b46fc1a7a819bf3cba09f370c0e9c64c (patch) | |
| tree | 11a1f297b139c485baf17d09de68d69259e3b5ae /query.zig | |
| parent | Merge pull request #185 from vrischmann/update-sqlite (diff) | |
| parent | add test case, switch from using a nullable to undefined (diff) | |
| download | zig-sqlite-be8b4965b46fc1a7a819bf3cba09f370c0e9c64c.tar.gz zig-sqlite-be8b4965b46fc1a7a819bf3cba09f370c0e9c64c.tar.xz zig-sqlite-be8b4965b46fc1a7a819bf3cba09f370c0e9c64c.zip | |
Merge pull request #188 from gracen-writes-code/master
Allow for strings within strings in SQL queries
Diffstat (limited to 'query.zig')
| -rw-r--r-- | query.zig | 30 |
1 files changed, 28 insertions, 2 deletions
| @@ -53,6 +53,11 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type { | |||
| 53 | var pos = 0; | 53 | var pos = 0; |
| 54 | var state = .start; | 54 | var state = .start; |
| 55 | 55 | ||
| 56 | // This holds the starting character of the string while | ||
| 57 | // state is .inside_string so that we know which type of | ||
| 58 | // string we're exiting from | ||
| 59 | var string_starting_character: u8 = undefined; | ||
| 60 | |||
| 56 | var current_bind_marker_type: [256]u8 = undefined; | 61 | var current_bind_marker_type: [256]u8 = undefined; |
| 57 | var current_bind_marker_type_pos = 0; | 62 | var current_bind_marker_type_pos = 0; |
| 58 | 63 | ||
| @@ -75,6 +80,7 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type { | |||
| 75 | }, | 80 | }, |
| 76 | '\'', '"', '[', '`' => { | 81 | '\'', '"', '[', '`' => { |
| 77 | state = .inside_string; | 82 | state = .inside_string; |
| 83 | string_starting_character = c; | ||
| 78 | buf[pos] = c; | 84 | buf[pos] = c; |
| 79 | pos += 1; | 85 | pos += 1; |
| 80 | }, | 86 | }, |
| @@ -84,8 +90,23 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type { | |||
| 84 | }, | 90 | }, |
| 85 | }, | 91 | }, |
| 86 | .inside_string => switch (c) { | 92 | .inside_string => switch (c) { |
| 87 | '\'', '"', ']', '`' => { | 93 | '\'' => { |
| 88 | state = .start; | 94 | if (string_starting_character == '\'') state = .start; |
| 95 | buf[pos] = c; | ||
| 96 | pos += 1; | ||
| 97 | }, | ||
| 98 | '"' => { | ||
| 99 | if (string_starting_character == '"') state = .start; | ||
| 100 | buf[pos] = c; | ||
| 101 | pos += 1; | ||
| 102 | }, | ||
| 103 | ']' => { | ||
| 104 | if (string_starting_character == '[') state = .start; | ||
| 105 | buf[pos] = c; | ||
| 106 | pos += 1; | ||
| 107 | }, | ||
| 108 | '`' => { | ||
| 109 | if (string_starting_character == '`') state = .start; | ||
| 89 | buf[pos] = c; | 110 | buf[pos] = c; |
| 90 | pos += 1; | 111 | pos += 1; |
| 91 | }, | 112 | }, |
| @@ -431,6 +452,11 @@ test "parsed query: bind marker character inside string" { | |||
| 431 | .exp_bind_markers = 1, | 452 | .exp_bind_markers = 1, |
| 432 | .exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name", | 453 | .exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name", |
| 433 | }, | 454 | }, |
| 455 | .{ | ||
| 456 | .query = "SELECT json_extract(metadata, '$[0]') AS name FROM foobar", | ||
| 457 | .exp_bind_markers = 0, | ||
| 458 | .exp = "SELECT json_extract(metadata, '$[0]') AS name FROM foobar", | ||
| 459 | }, | ||
| 434 | }; | 460 | }; |
| 435 | 461 | ||
| 436 | inline for (testCases) |tc| { | 462 | inline for (testCases) |tc| { |