forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appender.go
162 lines (140 loc) · 4.47 KB
/
appender.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"database/sql/driver"
"errors"
"fmt"
"time"
"unsafe"
)
// Appender holds the duckdb appender. It allows to load bulk data into a DuckDB database.
type Appender struct {
c *conn
schema string
table string
appender *C.duckdb_appender
closed bool
}
// NewAppenderFromConn returns a new Appender from a DuckDB driver connection.
func NewAppenderFromConn(driverConn driver.Conn, schema string, table string) (*Appender, error) {
dbConn, ok := driverConn.(*conn)
if !ok {
return nil, fmt.Errorf("not a duckdb driver connection")
}
if dbConn.closed {
panic("database/sql/driver: misuse of duckdb driver: Appender after Close")
}
var schemastr *(C.char)
if schema != "" {
schemastr = C.CString(schema)
defer C.free(unsafe.Pointer(schemastr))
}
tablestr := C.CString(table)
defer C.free(unsafe.Pointer(tablestr))
var a C.duckdb_appender
if state := C.duckdb_appender_create(*dbConn.con, schemastr, tablestr, &a); state == C.DuckDBError {
return nil, fmt.Errorf("can't create appender")
}
return &Appender{c: dbConn, schema: schema, table: table, appender: &a}, nil
}
// Error returns the last duckdb appender error.
func (a *Appender) Error() error {
dbErr := C.GoString(C.duckdb_appender_error(*a.appender))
return errors.New(dbErr)
}
// Flush the appender to the underlying table and clear the internal cache.
func (a *Appender) Flush() error {
if state := C.duckdb_appender_flush(*a.appender); state == C.DuckDBError {
dbErr := C.GoString(C.duckdb_appender_error(*a.appender))
return errors.New(dbErr)
}
return nil
}
// Closes closes the appender.
func (a *Appender) Close() error {
if a.closed {
panic("database/sql/driver: misuse of duckdb driver: double Close of Appender")
}
a.closed = true
if state := C.duckdb_appender_destroy(a.appender); state == C.DuckDBError {
dbErr := C.GoString(C.duckdb_appender_error(*a.appender))
return errors.New(dbErr)
}
return nil
}
// AppendRow loads a row of values into the appender. The values are provided as separate arguments.
func (a *Appender) AppendRow(args ...driver.Value) error {
return a.AppendRowArray(args)
}
// AppendRowArray loads a row of values into the appender. The values are provided as an array.
func (a *Appender) AppendRowArray(args []driver.Value) error {
if a.closed {
panic("database/sql/driver: misuse of duckdb driver: use of closed Appender")
}
for i, v := range args {
if v == nil {
if rv := C.duckdb_append_null(*a.appender); rv == C.DuckDBError {
return fmt.Errorf("couldn't append parameter %d", i)
}
continue
}
var rv C.duckdb_state
switch v := v.(type) {
case uint8:
rv = C.duckdb_append_uint8(*a.appender, C.uint8_t(v))
case int8:
rv = C.duckdb_append_int8(*a.appender, C.int8_t(v))
case uint16:
rv = C.duckdb_append_uint16(*a.appender, C.uint16_t(v))
case int16:
rv = C.duckdb_append_int16(*a.appender, C.int16_t(v))
case uint32:
rv = C.duckdb_append_uint32(*a.appender, C.uint32_t(v))
case int32:
rv = C.duckdb_append_int32(*a.appender, C.int32_t(v))
case uint64:
rv = C.duckdb_append_uint64(*a.appender, C.uint64_t(v))
case int64:
rv = C.duckdb_append_int64(*a.appender, C.int64_t(v))
case uint:
rv = C.duckdb_append_uint64(*a.appender, C.uint64_t(v))
case int:
rv = C.duckdb_append_int64(*a.appender, C.int64_t(v))
case float32:
rv = C.duckdb_append_float(*a.appender, C.float(v))
case float64:
rv = C.duckdb_append_double(*a.appender, C.double(v))
case bool:
rv = C.duckdb_append_bool(*a.appender, C.bool(v))
case []byte:
rv = C.duckdb_append_blob(*a.appender, unsafe.Pointer(&v[0]), C.uint64_t(len(v)))
case string:
str := C.CString(v)
if rv = C.duckdb_append_varchar(*a.appender, str); rv == C.DuckDBError {
C.free(unsafe.Pointer(str))
return fmt.Errorf("couldn't append parameter %d (type %T)", i, v)
}
C.free(unsafe.Pointer(str))
case time.Time:
var dt C.duckdb_timestamp
dt.micros = C.int64_t(v.UTC().UnixMicro())
rv = C.duckdb_append_timestamp(*a.appender, dt)
default:
return fmt.Errorf("couldn't append unsupported parameter %d (type %T)", i, v)
}
if rv == C.DuckDBError {
return fmt.Errorf("couldn't append parameter %d (type %T)", i, v)
}
}
if state := C.duckdb_appender_end_row(*a.appender); state == C.DuckDBError {
dbErr := C.GoString(C.duckdb_appender_error(*a.appender))
return errors.New(dbErr)
}
return nil
}
var (
errCouldNotAppend = errors.New("could not append parameter")
)