-
Notifications
You must be signed in to change notification settings - Fork 110
/
statement.go
282 lines (254 loc) · 7.84 KB
/
statement.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"math/big"
"time"
"unsafe"
)
// Stmt implements the driver.Stmt interface.
type Stmt struct {
c *Conn
stmt *C.duckdb_prepared_statement
closeOnRowsClose bool
closed bool
rows bool
}
// Close closes the statement.
// It implements the driver.Stmt interface.
func (s *Stmt) Close() error {
if s.rows {
panic("database/sql/driver: misuse of duckdb driver: Close with active Rows")
}
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: double Close of Stmt")
}
s.closed = true
C.duckdb_destroy_prepare(s.stmt)
return nil
}
// NumInput returns the number of placeholder parameters.
// It implements the driver.Stmt interface.
func (s *Stmt) NumInput() int {
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: NumInput after Close")
}
paramCount := C.duckdb_nparams(*s.stmt)
return int(paramCount)
}
func (s *Stmt) bind(args []driver.NamedValue) error {
if s.NumInput() > len(args) {
return fmt.Errorf("incorrect argument count for command: have %d want %d", len(args), s.NumInput())
}
// FIXME (feature): we can't pass nested types as parameters (bind_value) yet
// relaxed length check allow for unused parameters.
for i := 0; i < s.NumInput(); i++ {
name := C.duckdb_parameter_name(*s.stmt, C.idx_t(i+1))
paramName := C.GoString(name)
C.duckdb_free(unsafe.Pointer(name))
// fallback on index position
arg := args[i]
// override with ordinal if set
for _, v := range args {
if v.Ordinal == i+1 {
arg = v
}
}
// override with name if set
for _, v := range args {
if v.Name == paramName {
arg = v
}
}
switch v := arg.Value.(type) {
case bool:
if rv := C.duckdb_bind_boolean(*s.stmt, C.idx_t(i+1), C.bool(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case int8:
if rv := C.duckdb_bind_int8(*s.stmt, C.idx_t(i+1), C.int8_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case int16:
if rv := C.duckdb_bind_int16(*s.stmt, C.idx_t(i+1), C.int16_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case int32:
if rv := C.duckdb_bind_int32(*s.stmt, C.idx_t(i+1), C.int32_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case int64:
if rv := C.duckdb_bind_int64(*s.stmt, C.idx_t(i+1), C.int64_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case int:
if rv := C.duckdb_bind_int64(*s.stmt, C.idx_t(i+1), C.int64_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case *big.Int:
val, err := hugeIntFromNative(v)
if err != nil {
return err
}
if rv := C.duckdb_bind_hugeint(*s.stmt, C.idx_t(i+1), val); rv == C.DuckDBError {
return errCouldNotBind
}
case uint8:
if rv := C.duckdb_bind_uint8(*s.stmt, C.idx_t(i+1), C.uchar(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case uint16:
if rv := C.duckdb_bind_uint16(*s.stmt, C.idx_t(i+1), C.uint16_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case uint32:
if rv := C.duckdb_bind_uint32(*s.stmt, C.idx_t(i+1), C.uint32_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case uint64:
if rv := C.duckdb_bind_uint64(*s.stmt, C.idx_t(i+1), C.uint64_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case float32:
if rv := C.duckdb_bind_float(*s.stmt, C.idx_t(i+1), C.float(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case float64:
if rv := C.duckdb_bind_double(*s.stmt, C.idx_t(i+1), C.double(v)); rv == C.DuckDBError {
return errCouldNotBind
}
case string:
val := C.CString(v)
if rv := C.duckdb_bind_varchar(*s.stmt, C.idx_t(i+1), val); rv == C.DuckDBError {
C.duckdb_free(unsafe.Pointer(val))
return errCouldNotBind
}
C.duckdb_free(unsafe.Pointer(val))
case []byte:
val := C.CBytes(v)
l := len(v)
if rv := C.duckdb_bind_blob(*s.stmt, C.idx_t(i+1), val, C.uint64_t(l)); rv == C.DuckDBError {
C.duckdb_free(unsafe.Pointer(val))
return errCouldNotBind
}
C.duckdb_free(unsafe.Pointer(val))
case time.Time:
val := C.duckdb_timestamp{
micros: C.int64_t(v.UTC().UnixMicro()),
}
if rv := C.duckdb_bind_timestamp(*s.stmt, C.idx_t(i+1), val); rv == C.DuckDBError {
return errCouldNotBind
}
case Interval:
val := C.duckdb_interval{
months: C.int32_t(v.Months),
days: C.int32_t(v.Days),
micros: C.int64_t(v.Micros),
}
if rv := C.duckdb_bind_interval(*s.stmt, C.idx_t(i+1), val); rv == C.DuckDBError {
return errCouldNotBind
}
case nil:
if rv := C.duckdb_bind_null(*s.stmt, C.idx_t(i+1)); rv == C.DuckDBError {
return errCouldNotBind
}
default:
return driver.ErrSkip
}
}
return nil
}
// Deprecated: Use ExecContext instead.
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), argsToNamedArgs(args))
}
// ExecContext executes a query that doesn't return rows, such as an INSERT or UPDATE.
// It implements the driver.StmtExecContext interface.
func (s *Stmt) ExecContext(ctx context.Context, nargs []driver.NamedValue) (driver.Result, error) {
res, err := s.execute(ctx, nargs)
if err != nil {
return nil, err
}
defer C.duckdb_destroy_result(res)
ra := int64(C.duckdb_value_int64(res, 0, 0))
return &result{ra}, nil
}
// Deprecated: Use QueryContext instead.
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), argsToNamedArgs(args))
}
// QueryContext executes a query that may return rows, such as a SELECT.
// It implements the driver.StmtQueryContext interface.
func (s *Stmt) QueryContext(ctx context.Context, nargs []driver.NamedValue) (driver.Rows, error) {
res, err := s.execute(ctx, nargs)
if err != nil {
return nil, err
}
s.rows = true
return newRowsWithStmt(*res, s), nil
}
// This method executes the query in steps and checks if context is cancelled before executing each step.
// It uses Pending Result Interface C APIs to achieve this. Reference - https://duckdb.org/docs/api/c/api#pending-result-interface
func (s *Stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb_result, error) {
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: ExecContext or QueryContext after Close")
}
if s.rows {
panic("database/sql/driver: misuse of duckdb driver: ExecContext or QueryContext with active Rows")
}
if err := s.bind(args); err != nil {
return nil, err
}
var pendingRes C.duckdb_pending_result
if state := C.duckdb_pending_prepared(*s.stmt, &pendingRes); state == C.DuckDBError {
dbErr := getDuckDBError(C.GoString(C.duckdb_pending_error(pendingRes)))
C.duckdb_destroy_pending(&pendingRes)
return nil, dbErr
}
defer C.duckdb_destroy_pending(&pendingRes)
mainDoneCh := make(chan struct{})
bgDoneCh := make(chan struct{})
go func() {
select {
case <-ctx.Done():
C.duckdb_interrupt(s.c.duckdbCon)
close(bgDoneCh)
return
case <-mainDoneCh:
close(bgDoneCh)
return
}
}()
var res C.duckdb_result
state := C.duckdb_execute_pending(pendingRes, &res)
close(mainDoneCh)
// also wait for background goroutine to finish
// sometimes the bg goroutine is not scheduled immediately and by that time if another query is running on this connection
// it can cancel that query so need to wait for it to finish as well
<-bgDoneCh
if state == C.DuckDBError {
if ctx.Err() != nil {
C.duckdb_destroy_result(&res)
return nil, ctx.Err()
}
err := getDuckDBError(C.GoString(C.duckdb_result_error(&res)))
C.duckdb_destroy_result(&res)
return nil, err
}
return &res, nil
}
func argsToNamedArgs(values []driver.Value) []driver.NamedValue {
args := make([]driver.NamedValue, len(values))
for n, param := range values {
args[n].Value = param
args[n].Ordinal = n + 1
}
return args
}
var errCouldNotBind = errors.New("could not bind parameter")