summaryrefslogtreecommitdiff
path: root/src/db.zig
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-07-29 23:19:52 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-07-29 23:19:52 +0200
commita205a7ce6c65ead2b489800bf7e9faf3802b46d1 (patch)
tree4b3b0395e05da4abc788a3c5dabb90519c3d52e6 /src/db.zig
parentdf5e800284b708cce97c309ccf3ac04743551b32 (diff)
Abstract away the DataChunk
Diffstat (limited to 'src/db.zig')
-rw-r--r--src/db.zig28
1 files changed, 11 insertions, 17 deletions
diff --git a/src/db.zig b/src/db.zig
index 4c4da8d..9f9e22e 100644
--- a/src/db.zig
+++ b/src/db.zig
@@ -4,29 +4,25 @@ const c = @cImport({
});
-const DataChunk = struct {
+const Result = struct {
+ _res: c.duckdb_result,
_chunk: c.duckdb_data_chunk,
- pub fn deinit(self: * DataChunk) void {
+ pub fn deinit(self: *Result) void {
+ c.duckdb_destroy_result(&self._res);
c.duckdb_destroy_data_chunk(&self._chunk);
}
- pub fn getSize(self: DataChunk) usize {
+ pub fn getSize(self: Result) usize {
return c.duckdb_data_chunk_get_size(self._chunk);
}
-};
-const Result = struct {
- _res: c.duckdb_result,
-
- pub fn deinit(self: *Result) void {
- c.duckdb_destroy_result(&self._res);
+ pub fn fetchDataChunk(self: *Result) void{
+ self._chunk = c.duckdb_fetch_chunk(self._res);
}
- pub fn fetchDataChunk(self: Result) ?DataChunk{
- var chunk : DataChunk = undefined;
- chunk._chunk = c.duckdb_fetch_chunk(self._res);
- return chunk;
+ pub fn exausted(self: Result) bool{
+ return self._chunk != null;
}
};
@@ -52,6 +48,7 @@ const Connection = struct {
if ( state == c.DuckDBError){
return error.DuckDBError;
}
+ result.fetchDataChunk();
return result;
}
};
@@ -95,8 +92,5 @@ test "Query size" {
var result = try connection.query("SELECT * FROM integers;");
defer result.deinit();
- var chunk = result.fetchDataChunk() orelse return error.DuckDBError;
- defer chunk.deinit();
-
- try std.testing.expect(3 == chunk.getSize());
+ try std.testing.expect(3 == result.getSize());
}