summaryrefslogtreecommitdiff
path: root/src/duckdb/db.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/duckdb/db.zig')
-rw-r--r--src/duckdb/db.zig41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/duckdb/db.zig b/src/duckdb/db.zig
index 4b5ebe3..4fad7cb 100644
--- a/src/duckdb/db.zig
+++ b/src/duckdb/db.zig
@@ -4,7 +4,7 @@ const c = @cImport({
@cInclude("duckdb.h");
});
const Result = @import("Result.zig").Result;
-
+const PreparedStatement = @import("PreparedStatement.zig");
const Connection = struct {
_conn: c.duckdb_connection,
@@ -37,6 +37,17 @@ const Connection = struct {
var x = try self.query(q, void);
defer x.deinit();
}
+
+ /// Make a prepared query. Caller needs to call prepared_query.deinit()
+ pub fn prepareStatement(self: *Connection, q: [:0]const u8)
+ !PreparedStatement {
+ var stmt: c.duckdb_prepared_statement = undefined;
+ const state = c.duckdb_prepare(self._conn, q, &stmt);
+ if ( state == c.DuckDBError ){
+ return error.DuckDBError;
+ }
+ return PreparedStatement.init(stmt);
+ }
};
@@ -142,3 +153,31 @@ test "String queries" {
w = try result.next();
try std.testing.expect(std.mem.eql(u8, w.primer, "A very long string that is not inlined"));
}
+
+test "Prepared queries" {
+ var database = try Database.init(":memory:");
+ defer database.deinit();
+ var connection = try database.connect();
+ defer connection.deinit();
+
+ try connection.run("CREATE TABLE ints (i INTEGER NOT NULL );");
+ var prepared = try connection.prepareStatement("INSERT INTO ints VALUES (?), (?);");
+ defer prepared.deinit();
+
+ const uno: i32 = 1;
+ const dos: i32 = 2;
+ try prepared.bindAll(.{uno, dos});
+ var res = try prepared.exec(void);
+ res.deinit();
+
+ const s: type = struct {
+ primer: i32,
+ };
+ var result = try connection.query("SELECT * FROM ints;", s);
+ defer result.deinit();
+
+ var r = try result.next();
+ try std.testing.expect(r.primer == uno);
+ r = try result.next();
+ try std.testing.expect(r.primer == dos);
+}