diff options
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 48 |
1 files changed, 43 insertions, 5 deletions
| @@ -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 | |||
| 4040 | test "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 | } | ||