summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar IamSanjid2024-10-23 02:50:55 +0600
committerGravatar IamSanjid2024-10-23 02:50:55 +0600
commite964c6eb2b95f692875ae344374bc99c99013120 (patch)
tree593b8d5869f99a9971ba5fa400e2a7b475ef76e1 /sqlite.zig
parentMerge pull request #166 from vrischmann/update-sqlite (diff)
downloadzig-sqlite-e964c6eb2b95f692875ae344374bc99c99013120.tar.gz
zig-sqlite-e964c6eb2b95f692875ae344374bc99c99013120.tar.xz
zig-sqlite-e964c6eb2b95f692875ae344374bc99c99013120.zip
Reuse field for binding
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig48
1 files changed, 43 insertions, 5 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 4d7457b..62950c8 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -2069,11 +2069,26 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: anytype) type
2069 2069
2070 const StructTypeInfo = @typeInfo(StructType).@"struct"; 2070 const StructTypeInfo = @typeInfo(StructType).@"struct";
2071 2071
2072 if (comptime query.bind_markers.len != StructTypeInfo.fields.len) { 2072 comptime marker_len_check: {
2073 @compileError(std.fmt.comptimePrint("expected {d} bind parameters but got {d}", .{ 2073 if (query.bind_markers.len != StructTypeInfo.fields.len) {
2074 query.bind_markers.len, 2074 if (query.bind_markers.len > StructTypeInfo.fields.len) {
2075 StructTypeInfo.fields.len, 2075 var found_markers = 0;
2076 })); 2076 for (query.bind_markers) |bind_marker| {
2077 if (bind_marker.name) |name| {
2078 if (@hasField(StructType, name)) {
2079 found_markers += 1;
2080 }
2081 }
2082 }
2083 if (found_markers == query.bind_markers.len) {
2084 break :marker_len_check;
2085 }
2086 }
2087 @compileError(std.fmt.comptimePrint("expected {d} bind parameters but got {d}", .{
2088 query.bind_markers.len,
2089 StructTypeInfo.fields.len,
2090 }));
2091 }
2077 } 2092 }
2078 2093
2079 inline for (StructTypeInfo.fields, 0..) |struct_field, _i| { 2094 inline for (StructTypeInfo.fields, 0..) |struct_field, _i| {
@@ -4021,3 +4036,26 @@ test "tagged union" {
4021 try testing.expectEqual(foobar.age, result.?.value); 4036 try testing.expectEqual(foobar.age, result.?.value);
4022 } 4037 }
4023} 4038}
4039
4040test "reuse same field twice in query string" {
4041 var db = try getTestDb();
4042 defer db.deinit();
4043 try addTestData(&db);
4044
4045 const update_name = "NewUpdatedName";
4046
4047 const update_name_query = "UPDATE user SET id = $id, name = $name WHERE id = $id";
4048 try db.exec(update_name_query, .{}, .{ .id = 20, .name = update_name });
4049
4050 const fetch_name_query = "SELECT name FROM user WHERE id = $id";
4051 const name = try db.oneAlloc(
4052 []const u8,
4053 testing.allocator,
4054 fetch_name_query,
4055 .{},
4056 .{ .id = 20 },
4057 );
4058 try testing.expect(name != null);
4059 defer testing.allocator.free(name.?);
4060 try testing.expectEqualStrings(name.?, update_name);
4061}