diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-08-03 13:43:41 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-08-03 13:44:49 +0200 |
commit | c60c42fc64d9d95b707fc90b7bc08beffd5416af (patch) | |
tree | 0cd936e132ee4c15bfcd58df58eb5f4f90fc0fc4 /src/duckdb/Connection.zig | |
parent | 9061b8864d5bf99871622d741a422356bcc82120 (diff) |
Reorganize duckdb
Diffstat (limited to 'src/duckdb/Connection.zig')
-rw-r--r-- | src/duckdb/Connection.zig | 47 |
1 files changed, 47 insertions, 0 deletions
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); +} |