-
Notifications
You must be signed in to change notification settings - Fork 106
/
value.go
77 lines (74 loc) · 2.19 KB
/
value.go
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
67
68
69
70
71
72
73
74
75
76
77
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"time"
"unsafe"
)
func getValue(t TypeInfo, v C.duckdb_value) (any, error) {
switch t.(*typeInfo).Type {
case TYPE_BOOLEAN:
return bool(C.duckdb_get_bool(v)), nil
case TYPE_TINYINT:
return int8(C.duckdb_get_int8(v)), nil
case TYPE_SMALLINT:
return int16(C.duckdb_get_int16(v)), nil
case TYPE_INTEGER:
return int32(C.duckdb_get_int32(v)), nil
case TYPE_BIGINT:
return int64(C.duckdb_get_int64(v)), nil
case TYPE_UTINYINT:
return uint8(C.duckdb_get_uint8(v)), nil
case TYPE_USMALLINT:
return uint16(C.duckdb_get_uint16(v)), nil
case TYPE_UINTEGER:
return uint32(C.duckdb_get_uint32(v)), nil
case TYPE_UBIGINT:
return uint64(C.duckdb_get_uint64(v)), nil
case TYPE_FLOAT:
return float32(C.duckdb_get_float(v)), nil
case TYPE_DOUBLE:
return float64(C.duckdb_get_double(v)), nil
case TYPE_TIMESTAMP:
val := C.duckdb_get_timestamp(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
case TYPE_DATE:
primitiveDate := C.duckdb_get_date(v)
date := C.duckdb_from_date(primitiveDate)
return time.Date(int(date.year), time.Month(date.month), int(date.day), 0, 0, 0, 0, time.UTC), nil
case TYPE_TIME:
val := C.duckdb_get_time(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
case TYPE_INTERVAL:
interval := C.duckdb_get_interval(v)
return Interval{
Days: int32(interval.days),
Months: int32(interval.months),
Micros: int64(interval.micros),
}, nil
case TYPE_HUGEINT:
hugeint := C.duckdb_get_hugeint(v)
return hugeIntToNative(hugeint), nil
case TYPE_VARCHAR:
str := C.duckdb_get_varchar(v)
ret := C.GoString(str)
C.duckdb_free(unsafe.Pointer(str))
return ret, nil
case TYPE_TIMESTAMP_S:
val := C.duckdb_get_timestamp(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
case TYPE_TIMESTAMP_MS:
val := C.duckdb_get_timestamp(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
case TYPE_TIMESTAMP_NS:
val := C.duckdb_get_timestamp(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
case TYPE_TIMESTAMP_TZ:
val := C.duckdb_get_timestamp(v)
return time.UnixMicro(int64(val.micros)).UTC(), nil
default:
return nil, unsupportedTypeError(typeToStringMap[t.InternalType()])
}
}