diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-08-19 00:47:34 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-08-19 00:47:34 +0200 |
commit | 7208d3e9ebc05591059f8244dee88bd91a73dee3 (patch) | |
tree | b77a315a33ca1c693e695c210c86242b834d788f /src | |
parent | a4227e7c8fbbe9c24443e269b459d367d59ef85a (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/duckdb/Database.zig | 28 | ||||
-rw-r--r-- | src/duckdb/PreparedStatement.zig | 24 |
2 files changed, 46 insertions, 6 deletions
diff --git a/src/duckdb/Database.zig b/src/duckdb/Database.zig index ffdd052..8a34696 100644 --- a/src/duckdb/Database.zig +++ b/src/duckdb/Database.zig @@ -175,3 +175,31 @@ test "Prepared queries" { r = try result.next(); try std.testing.expect(r.primer == dos); } + +test "Prepared queries with strings" { + var database = try Self.init(":memory:"); + defer database.deinit(); + var connection = try database.connect(); + defer connection.deinit(); + + try connection.run("CREATE TABLE ints (i STRING NOT NULL );"); + var prepared = try connection.prepareStatement("INSERT INTO ints VALUES (?), (?);"); + defer prepared.deinit(); + + const uno = "1"; + const dos = "2"; + try prepared.bindAll(.{uno, dos}); + var res = try prepared.exec(void); + res.deinit(); + + const s: type = struct { + primer: []const u8, + }; + var result = try connection.query("SELECT * FROM ints;", s); + defer result.deinit(); + + var w = try result.next(); + try std.testing.expect(std.mem.eql(u8, w.primer, "1")); + w = try result.next(); + try std.testing.expect(std.mem.eql(u8, w.primer, "2")); +} diff --git a/src/duckdb/PreparedStatement.zig b/src/duckdb/PreparedStatement.zig index 5e3229e..237152c 100644 --- a/src/duckdb/PreparedStatement.zig +++ b/src/duckdb/PreparedStatement.zig @@ -73,12 +73,14 @@ pub fn bindString(self: *Self, param: []const u8) !void{ self._current_binding += 1; } + pub fn bind(self: *Self, param: anytype) !void { switch (@typeInfo(@TypeOf(param))) { .Null => return try self.bindNull(), .Bool => return try self.bindBool(param), .Int => return try self.bindInt(param), .Float => return try self.bindFloat(param), + .Vector, .Array => |arr| { if (arr.child == u8){ return try self.bindString(param); @@ -86,12 +88,22 @@ pub fn bind(self: *Self, param: anytype) !void { return error.UnbindableType; } }, - .Pointer => |ptr| { - if (ptr.size == .Slice and ptr.child == u8){ - return try self.bindString(param); - } else { - return error.UnbindableType; - } + .Pointer => |info| switch (info.size) { + .One, .Many, .C, .Slice => switch (@typeInfo(info.child)) { + .Vector => |arr| + if (arr.child == u8){ + return try self.bindString(param); + } else { + return error.UnbindableType; + }, + .Array => |arr| + if (arr.child == u8){ + return try self.bindString(param); + } else { + return error.UnbindableType; + }, + else => {}, + }, }, else => @compileError("Invalid type for binding: " ++ @typeName(@TypeOf(param))), |