diff options
| author | 2021-01-02 00:57:48 +0100 | |
|---|---|---|
| committer | 2021-01-02 00:57:48 +0100 | |
| commit | 5fae8889351b36a20880d363cfa665bec02d7256 (patch) | |
| tree | 1f10d86879b089ca04f94704a95e6631a050fa85 /sqlite.zig | |
| parent | add bindField (diff) | |
| parent | stop special casing []const u8 and []u8, do it in the .Pointer switch arm (diff) | |
| download | zig-sqlite-5fae8889351b36a20880d363cfa665bec02d7256.tar.gz zig-sqlite-5fae8889351b36a20880d363cfa665bec02d7256.tar.xz zig-sqlite-5fae8889351b36a20880d363cfa665bec02d7256.zip | |
Merge pull request #13 from vrischmann/bind-string-literal
Bind string literal
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 30 |
1 files changed, 27 insertions, 3 deletions
| @@ -722,9 +722,6 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 722 | const column = i + 1; | 722 | const column = i + 1; |
| 723 | 723 | ||
| 724 | switch (FieldType) { | 724 | switch (FieldType) { |
| 725 | []const u8, []u8 => { | ||
| 726 | _ = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), null); | ||
| 727 | }, | ||
| 728 | Text => _ = c.sqlite3_bind_text(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), | 725 | Text => _ = c.sqlite3_bind_text(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), |
| 729 | Blob => _ = c.sqlite3_bind_blob(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), | 726 | Blob => _ = c.sqlite3_bind_blob(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), |
| 730 | else => switch (field_type_info) { | 727 | else => switch (field_type_info) { |
| @@ -732,6 +729,13 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 732 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field), | 729 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field), |
| 733 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field)), | 730 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field)), |
| 734 | .Pointer => |ptr| switch (ptr.size) { | 731 | .Pointer => |ptr| switch (ptr.size) { |
| 732 | .One => self.bindField(ptr.child, field_name, i, field.*), | ||
| 733 | .Slice => switch (ptr.child) { | ||
| 734 | u8 => { | ||
| 735 | _ = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), null); | ||
| 736 | }, | ||
| 737 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), | ||
| 738 | }, | ||
| 735 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), | 739 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), |
| 736 | }, | 740 | }, |
| 737 | .Array => |arr| { | 741 | .Array => |arr| { |
| @@ -1289,6 +1293,26 @@ test "sqlite: insert bool and bind bool" { | |||
| 1289 | testing.expect(b.?); | 1293 | testing.expect(b.?); |
| 1290 | } | 1294 | } |
| 1291 | 1295 | ||
| 1296 | test "sqlite: bind string literal" { | ||
| 1297 | var db: Db = undefined; | ||
| 1298 | try db.init(initOptions()); | ||
| 1299 | try addTestData(&db); | ||
| 1300 | |||
| 1301 | try db.exec("INSERT INTO article(id, data) VALUES(?, ?)", .{ | ||
| 1302 | @as(usize, 10), | ||
| 1303 | "foobar", | ||
| 1304 | }); | ||
| 1305 | |||
| 1306 | const query = "SELECT id FROM article WHERE data = ?"; | ||
| 1307 | |||
| 1308 | var stmt = try db.prepare(query); | ||
| 1309 | defer stmt.deinit(); | ||
| 1310 | |||
| 1311 | const b = try stmt.one(usize, .{}, .{"foobar"}); | ||
| 1312 | testing.expect(b != null); | ||
| 1313 | testing.expectEqual(@as(usize, 10), b.?); | ||
| 1314 | } | ||
| 1315 | |||
| 1292 | test "sqlite: statement reset" { | 1316 | test "sqlite: statement reset" { |
| 1293 | var db: Db = undefined; | 1317 | var db: Db = undefined; |
| 1294 | try db.init(initOptions()); | 1318 | try db.init(initOptions()); |