summaryrefslogtreecommitdiff
path: root/src/duckdb/Types.zig
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-08-02 13:23:21 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-08-03 13:44:49 +0200
commitdabbb107d719cb753673e394fbd4da090f74d458 (patch)
tree1beba6ec67922882db0cf6606d712195dcfb62d1 /src/duckdb/Types.zig
parentf225c34d82cb6ea4133af64c34f977be00769afc (diff)
WIP String queries and proper conversion:
Still have to make the type checking better, not just an assert. Rethink.
Diffstat (limited to 'src/duckdb/Types.zig')
-rw-r--r--src/duckdb/Types.zig66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/duckdb/Types.zig b/src/duckdb/Types.zig
new file mode 100644
index 0000000..2a12cd2
--- /dev/null
+++ b/src/duckdb/Types.zig
@@ -0,0 +1,66 @@
+const std = @import("std");
+const c = @cImport({
+ @cInclude("duckdb.h");
+});
+
+pub fn unpack_type(T: type) type {
+ return switch(@typeInfo(T)) {
+ .Bool, .Int, .Float => T,
+ .Pointer => |p| switch (p.size) {
+ .Slice => if (p.child == u8) c.duckdb_string_t else
+ @compileError("Invalid type for output data"),
+ else => @compileError("Invalid type for output data")
+ },
+ // .Array =>
+ // .Struct =>
+ // .Enum =>
+ // .Union =>
+ else => @compileError("Invalid type for output data")
+ };
+}
+
+pub fn valid_unpack(column_type: c.DUCKDB_TYPE, T: type) bool {
+ switch (column_type) {
+ c.DUCKDB_TYPE_BOOLEAN => if(T == bool) return true else return false,
+ c.DUCKDB_TYPE_TINYINT => if(T == i8 ) return true else return false,
+ c.DUCKDB_TYPE_SMALLINT => if(T == i16 ) return true else return false,
+ c.DUCKDB_TYPE_INTEGER => if(T == i32 ) return true else return false,
+ c.DUCKDB_TYPE_BIGINT => if(T == i64 ) return true else return false,
+ c.DUCKDB_TYPE_UTINYINT => if(T == u8 ) return true else return false,
+ c.DUCKDB_TYPE_USMALLINT => if(T == u16 ) return true else return false,
+ c.DUCKDB_TYPE_UINTEGER => if(T == u32 ) return true else return false,
+ c.DUCKDB_TYPE_UBIGINT => if(T == u64 ) return true else return false,
+ c.DUCKDB_TYPE_FLOAT => if(T == f32 ) return true else return false,
+ c.DUCKDB_TYPE_DOUBLE => if(T == f64 ) return true else return false,
+ c.DUCKDB_TYPE_VARCHAR => if(T == []const u8) return true else return false,
+ c.DUCKDB_TYPE_BLOB => if(T == []const u8) return true else return false,
+ else => return false,
+ }
+}
+
+/// Receives a pointer of the element (!)
+pub fn cast(el: anytype, T: type) !T {
+ return switch (@typeInfo(T)) {
+ .Bool, .Int, .Float => el.*,
+ .Pointer => |p| switch (p.size) {
+ .Slice => blk: {
+ if ( p.child == u8 ) {
+ var result: T = undefined;
+ if (c.duckdb_string_is_inlined(el.*)){
+ result = &el.value.inlined.inlined;
+ result.len = el.value.inlined.length;
+ break :blk result;
+ } else {
+ result.len = el.value.pointer.length;
+ result.ptr = el.value.pointer.ptr;
+ break :blk result;
+ }
+ } else {
+ @compileError("Invalid type for output data");
+ }
+ },
+ else => @compileError("Invalid type for output data")
+ },
+ else => @compileError("Invalid type for output data")
+ };
+}