forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
152 lines (132 loc) · 3.3 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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"database/sql/driver"
"errors"
"fmt"
"time"
"unsafe"
)
type stmt struct {
c *conn
stmt *C.duckdb_prepared_statement
closed bool
rows bool
}
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
}
func (s *stmt) NumInput() int {
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: NumInput after Close")
}
var pc C.idx_t
C.duckdb_nparams(*s.stmt, &pc)
return int(pc)
}
func (s *stmt) start(args []driver.Value) error {
if s.NumInput() != len(args) {
return fmt.Errorf("incorrect argument count for command: have %d want %d", len(args), s.NumInput())
}
for i, v := range args {
switch v := v.(type) {
case int8:
if rv := C.duckdb_bind_int8(*s.stmt, C.idx_t(i+1), C.int8_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case int16:
if rv := C.duckdb_bind_int16(*s.stmt, C.idx_t(i+1), C.int16_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case int32:
if rv := C.duckdb_bind_int32(*s.stmt, C.idx_t(i+1), C.int32_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case int64:
if rv := C.duckdb_bind_int64(*s.stmt, C.idx_t(i+1), C.int64_t(v)); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case float64:
if rv := C.duckdb_bind_double(*s.stmt, C.idx_t(i+1), C.double(v)); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case bool:
if rv := C.duckdb_bind_boolean(*s.stmt, C.idx_t(i+1), true); rv == C.DuckDBError {
return errCouldNotBind
}
continue
case time.Time:
// TODO
case string:
str := C.CString(v)
if rv := C.duckdb_bind_varchar(*s.stmt, C.idx_t(i+1), str); rv == C.DuckDBError {
C.free(unsafe.Pointer(str))
return errCouldNotBind
}
C.free(unsafe.Pointer(str))
continue
default:
return driver.ErrSkip
}
}
return nil
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: Exec after Close")
}
if s.rows {
panic("database/sql/driver: misuse of duckdb driver: Exec with active Rows")
}
err := s.start(args)
if err != nil {
return nil, err
}
var res C.duckdb_result
if err := C.duckdb_execute_prepared(*s.stmt, &res); err == C.DuckDBError {
return nil, errors.New(C.GoString(res.error_message))
}
if err != nil {
return nil, err
}
defer C.duckdb_destroy_result(&res)
ra := int64(C.duckdb_value_int64(&res, 0, 0))
return &result{ra: ra}, nil
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
if s.closed {
panic("database/sql/driver: misuse of duckdb driver: Query after Close")
}
if s.rows {
panic("database/sql/driver: misuse of duckdb driver: Query with active Rows")
}
err := s.start(args)
if err != nil {
return nil, err
}
s.rows = true
var res C.duckdb_result
if err := C.duckdb_execute_prepared(*s.stmt, &res); err == C.DuckDBError {
return nil, errors.New(C.GoString(res.error_message))
}
return &rows{r: &res, s: s}, nil
}
var (
errCouldNotBind = errors.New("could not bind parameter")
)