Skip to content

Commit be8b496

Browse files
authored
Merge pull request #188 from gracen-writes-code/master
Allow for strings within strings in SQL queries
2 parents ce4976f + a813e5a commit be8b496

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

query.zig

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type {
5353
var pos = 0;
5454
var state = .start;
5555

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+
5661
var current_bind_marker_type: [256]u8 = undefined;
5762
var current_bind_marker_type_pos = 0;
5863

@@ -75,6 +80,7 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type {
7580
},
7681
'\'', '"', '[', '`' => {
7782
state = .inside_string;
83+
string_starting_character = c;
7884
buf[pos] = c;
7985
pos += 1;
8086
},
@@ -84,8 +90,23 @@ pub fn ParsedQuery(comptime tmp_query: []const u8) type {
8490
},
8591
},
8692
.inside_string => switch (c) {
87-
'\'', '"', ']', '`' => {
88-
state = .start;
93+
'\'' => {
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;
89110
buf[pos] = c;
90111
pos += 1;
91112
},
@@ -431,6 +452,11 @@ test "parsed query: bind marker character inside string" {
431452
.exp_bind_markers = 1,
432453
.exp = "SELECT json_extract(metadata, '$.name') AS name FROM foobar WHERE name = $name",
433454
},
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+
},
434460
};
435461

436462
inline for (testCases) |tc| {

0 commit comments

Comments
 (0)