diff options
Diffstat (limited to 'src/duckdb/Types.zig')
-rw-r--r-- | src/duckdb/Types.zig | 66 |
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") + }; +} |