diff options
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 48 |
1 files changed, 43 insertions, 5 deletions
| @@ -2078,11 +2078,26 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: anytype) type | |||
| 2078 | 2078 | ||
| 2079 | const StructTypeInfo = @typeInfo(StructType).@"struct"; | 2079 | const StructTypeInfo = @typeInfo(StructType).@"struct"; |
| 2080 | 2080 | ||
| 2081 | if (comptime query.bind_markers.len != StructTypeInfo.fields.len) { | 2081 | comptime marker_len_check: { |
| 2082 | @compileError(std.fmt.comptimePrint("expected {d} bind parameters but got {d}", .{ | 2082 | if (query.bind_markers.len != StructTypeInfo.fields.len) { |
| 2083 | query.bind_markers.len, | 2083 | if (query.bind_markers.len > StructTypeInfo.fields.len) { |
| 2084 | StructTypeInfo.fields.len, | 2084 | var found_markers = 0; |
| 2085 | })); | 2085 | for (query.bind_markers) |bind_marker| { |
| 2086 | if (bind_marker.name) |name| { | ||
| 2087 | if (@hasField(StructType, name)) { | ||
| 2088 | found_markers += 1; | ||
| 2089 | } | ||
| 2090 | } | ||
| 2091 | } | ||
| 2092 | if (found_markers == query.bind_markers.len) { | ||
| 2093 | break :marker_len_check; | ||
| 2094 | } | ||
| 2095 | } | ||
| 2096 | @compileError(std.fmt.comptimePrint("expected {d} bind parameters but got {d}", .{ | ||
| 2097 | query.bind_markers.len, | ||
| 2098 | StructTypeInfo.fields.len, | ||
| 2099 | })); | ||
| 2100 | } | ||
| 2086 | } | 2101 | } |
| 2087 | 2102 | ||
| 2088 | inline for (StructTypeInfo.fields, 0..) |struct_field, _i| { | 2103 | inline for (StructTypeInfo.fields, 0..) |struct_field, _i| { |
| @@ -4030,3 +4045,26 @@ test "tagged union" { | |||
| 4030 | try testing.expectEqual(foobar.age, result.?.value); | 4045 | try testing.expectEqual(foobar.age, result.?.value); |
| 4031 | } | 4046 | } |
| 4032 | } | 4047 | } |
| 4048 | |||
| 4049 | test "reuse same field twice in query string" { | ||
| 4050 | var db = try getTestDb(); | ||
| 4051 | defer db.deinit(); | ||
| 4052 | try addTestData(&db); | ||
| 4053 | |||
| 4054 | const update_name = "NewUpdatedName"; | ||
| 4055 | |||
| 4056 | const update_name_query = "UPDATE user SET id = $id, name = $name WHERE id = $id"; | ||
| 4057 | try db.exec(update_name_query, .{}, .{ .id = 20, .name = update_name }); | ||
| 4058 | |||
| 4059 | const fetch_name_query = "SELECT name FROM user WHERE id = $id"; | ||
| 4060 | const name = try db.oneAlloc( | ||
| 4061 | []const u8, | ||
| 4062 | testing.allocator, | ||
| 4063 | fetch_name_query, | ||
| 4064 | .{}, | ||
| 4065 | .{ .id = 20 }, | ||
| 4066 | ); | ||
| 4067 | try testing.expect(name != null); | ||
| 4068 | defer testing.allocator.free(name.?); | ||
| 4069 | try testing.expectEqualStrings(name.?, update_name); | ||
| 4070 | } | ||