From 8efa7cb0013aae1708e2d815b7fa65f055062159 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Mon, 5 Aug 2024 22:20:19 +0200 Subject: Results with proper conversion --- src/duckdb/Result.zig | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'src/duckdb/Result.zig') diff --git a/src/duckdb/Result.zig b/src/duckdb/Result.zig index 61a30ce..87d9f6f 100644 --- a/src/duckdb/Result.zig +++ b/src/duckdb/Result.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const types = @import("Types.zig"); +const Column = @import("Column.zig").Column; const c = @cImport({ @cInclude("duckdb.h"); }); @@ -98,10 +98,7 @@ pub fn Result(comptime T: type) type{ inline for (fields, 0..) |field, i| { // TODO: check compatibility between the column type and // the struct provided as result container - // const column_type = c.duckdb_column_type(&self._res, i); - // std.debug.print("tipo => {any}\n", .{column_type}); - // std.debug.print("{any}\n", .{self._data}); // Check validity const entry_idx :usize = self._current_row / 64; @@ -115,21 +112,20 @@ pub fn Result(comptime T: type) type{ // Store the column in current row if (is_valid){ - // Unwrap the Optional + // Unwrap the output Optional const t = switch (@typeInfo(field.type)){ .Optional => |t| t.child, else => field.type }; - // Unpack the C array of duckdb_type - const col: [*]types.unpack_type(t) = @alignCast(@ptrCast(self._data[i])); - std.debug.assert(types.valid_unpack(column_type, t)); + // Obtain and convert column to something we can process + const column = Column.fromType(column_type, self._data[i].?); - // Convert to Zig data type - @field(result, field.name) = try types.cast(&col[self._current_row], t); + // Convert to Zig data type and store in output struct + @field(result, field.name) = try column.getAs(self._current_row, t); } else { // Got a NULL from the DB if (@typeInfo(field.type) != .Optional){ - // Cannot return it because it's not optional + // Cannot return it because output it's not optional return error.NullInNotOptional; } @field(result, field.name) = null; -- cgit v1.2.3