From c60c42fc64d9d95b707fc90b7bc08beffd5416af Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 3 Aug 2024 13:43:41 +0200 Subject: Reorganize duckdb --- src/duckdb/Connection.zig | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/duckdb/Connection.zig (limited to 'src/duckdb/Connection.zig') diff --git a/src/duckdb/Connection.zig b/src/duckdb/Connection.zig new file mode 100644 index 0000000..e62b2db --- /dev/null +++ b/src/duckdb/Connection.zig @@ -0,0 +1,47 @@ +const c = @cImport({ + @cInclude("duckdb.h"); +}); +const Result = @import("Result.zig").Result; +const PreparedStatement = @import("PreparedStatement.zig"); + +_conn: c.duckdb_connection, + +const Self = @This(); + +pub fn init(conn: c.duckdb_connection) Self { + return .{ + ._conn = conn + }; +} + +pub fn deinit(self: *Self) void { + c.duckdb_disconnect(&self._conn); +} + +/// Query returning a result. Caller needs to call result.deinit() +pub fn query(self: *Self, q: [:0]const u8, comptime res_type: type) + !Result(res_type) { + var result: c.duckdb_result = undefined; + const state = c.duckdb_query(self._conn, q, &result); + if ( state == c.DuckDBError ){ + return error.DuckDBError; + } + return try Result(res_type).init(result); +} + +/// Query with no results and autoclean +pub fn run(self: *Self, q: [:0]const u8) !void{ + 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: *Self, 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); +} -- cgit v1.2.3