summaryrefslogtreecommitdiff
path: root/src/duckdb/Types.zig
blob: 2a12cd23ec41cb283066848f9f279e9c14bebe18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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")
    };
}