forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
199 lines (164 loc) · 3.77 KB
/
connection.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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"bytes"
"database/sql/driver"
"errors"
"strconv"
"strings"
"time"
"unsafe"
)
type conn struct {
db *C.duckdb_database
con *C.duckdb_connection
closed bool
tx bool
}
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: Exec after Close")
}
res, err := c.exec(query)
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 (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
return c.query(query, args)
}
func (c *conn) Prepare(cmd string) (driver.Stmt, error) {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: Prepare after Close")
}
cmdstr := C.CString(cmd)
defer C.free(unsafe.Pointer(cmdstr))
var s C.duckdb_prepared_statement
C.duckdb_prepare(*c.con, cmdstr, &s)
return &stmt{c: c, stmt: &s}, nil
}
func (c *conn) Begin() (driver.Tx, error) {
if c.tx {
panic("database/sql/driver: misuse of duckdb driver: multiple Tx")
}
if _, err := c.exec("BEGIN TRANSACTION"); err != nil {
return nil, err
}
c.tx = true
return &tx{c}, nil
}
func (c *conn) Close() error {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: Close of already closed connection")
}
c.closed = true
C.duckdb_disconnect(c.con)
C.duckdb_close(c.db)
c.db = nil
return nil
}
func (c *conn) query(query string, args []driver.Value) (driver.Rows, error) {
queryStr, err := c.interpolateParams(query, args)
if err != nil {
return nil, err
}
res, err := c.exec(queryStr)
if err != nil {
return nil, err
}
return &rows{r: res}, nil
}
func (c *conn) exec(cmd string) (*C.duckdb_result, error) {
cmdstr := C.CString(cmd)
defer C.free(unsafe.Pointer(cmdstr))
var res C.duckdb_result
if err := C.duckdb_query(*c.con, cmdstr, &res); err == C.DuckDBError {
return nil, errors.New(C.GoString(res.error_message))
}
return &res, nil
}
// interpolateParams is taken from
// https://github.com/go-sql-driver/mysql
func (c *conn) interpolateParams(query string, args []driver.Value) (string, error) {
if strings.Count(query, "?") != len(args) {
return "", driver.ErrSkip
}
buf := []byte{}
argPos := 0
for i := 0; i < len(query); i++ {
q := strings.IndexByte(query[i:], '?')
if q == -1 {
buf = append(buf, query[i:]...)
break
}
buf = append(buf, query[i:i+q]...)
i += q
arg := args[argPos]
argPos++
if arg == nil {
buf = append(buf, "NULL"...)
continue
}
switch v := arg.(type) {
case int8:
buf = strconv.AppendInt(buf, int64(v), 10)
case int16:
buf = strconv.AppendInt(buf, int64(v), 10)
case int32:
buf = strconv.AppendInt(buf, int64(v), 10)
case int64:
buf = strconv.AppendInt(buf, int64(v), 10)
case float64:
buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
case bool:
if v {
buf = append(buf, '1')
} else {
buf = append(buf, '0')
}
case time.Time:
buf = strconv.AppendInt(buf, v.Unix(), 10)
case string:
buf = append(buf, '\'')
buf = append(buf, escapeValue(v)...)
buf = append(buf, '\'')
default:
return "", driver.ErrSkip
}
}
if argPos != len(args) {
return "", driver.ErrSkip
}
return string(buf), nil
}
func escapeValue(v string) []byte {
buf := bytes.NewBuffer(nil)
for i := 0; i < len(v); i++ {
c := v[i]
switch c {
case '\x00':
buf.WriteString("\\\\0")
case '\n':
buf.WriteString("\\\\n")
case '\r':
buf.WriteString("\\\\r")
case '\x1a':
buf.WriteString("\\\\Z")
case '\'':
buf.WriteString("\\\\'")
case '"':
buf.WriteString("\\\"")
case '\\':
buf.WriteString("\\\\")
default:
buf.WriteByte(c)
}
}
return buf.Bytes()
}