forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
195 lines (163 loc) · 4.76 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
package duckdb
/*
#include <stdlib.h>
#include <duckdb.h>
*/
import "C"
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"math/big"
"unsafe"
)
type conn struct {
duckdbCon C.duckdb_connection
closed bool
tx bool
}
func (c *conn) CheckNamedValue(nv *driver.NamedValue) error {
switch nv.Value.(type) {
case *big.Int, Interval:
return nil
}
return driver.ErrSkip
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: ExecContext after Close")
}
stmts, size, err := c.extractStmts(query)
if err != nil {
return nil, err
}
defer C.duckdb_destroy_extracted(&stmts)
// execute all statements without args, except the last one
for i := C.idx_t(0); i < size-1; i++ {
stmt, err := c.prepareExtractedStmt(stmts, i)
if err != nil {
return nil, err
}
// send nil args to execute statement and ignore result
_, err = stmt.ExecContext(ctx, nil)
stmt.Close()
if err != nil {
return nil, err
}
}
// prepare and execute last statement with args and return result
stmt, err := c.prepareExtractedStmt(stmts, size-1)
if err != nil {
return nil, err
}
defer stmt.Close()
return stmt.ExecContext(ctx, args)
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: QueryContext after Close")
}
stmts, size, err := c.extractStmts(query)
if err != nil {
return nil, err
}
defer C.duckdb_destroy_extracted(&stmts)
// execute all statements without args, except the last one
for i := C.idx_t(0); i < size-1; i++ {
stmt, err := c.prepareExtractedStmt(stmts, i)
if err != nil {
return nil, err
}
// send nil args to execute statement and ignore result (using ExecContext since we're ignoring the result anyway)
_, err = stmt.ExecContext(ctx, nil)
stmt.Close()
if err != nil {
return nil, err
}
}
// prepare and execute last statement with args and return result
stmt, err := c.prepareExtractedStmt(stmts, size-1)
if err != nil {
return nil, err
}
rows, err := stmt.QueryContext(ctx, args)
if err != nil {
stmt.Close()
return nil, err
}
// we can't close the statement before the query result rows are closed
stmt.closeOnRowsClose = true
return rows, err
}
func (c *conn) Prepare(cmd string) (driver.Stmt, error) {
if c.closed {
panic("database/sql/driver: misuse of duckdb driver: Prepare after Close")
}
return c.prepareStmt(cmd)
}
// Deprecated: Use BeginTx instead.
func (c *conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if c.tx {
panic("database/sql/driver: misuse of duckdb driver: multiple Tx")
}
if opts.ReadOnly {
return nil, errors.New("read-only transactions are not supported")
}
switch sql.IsolationLevel(opts.Isolation) {
case sql.LevelDefault:
default:
return nil, errors.New("isolation levels other than default are not supported")
}
if _, err := c.ExecContext(ctx, "BEGIN TRANSACTION", nil); 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.duckdbCon)
return nil
}
func (c *conn) prepareStmt(cmd string) (*stmt, error) {
cmdstr := C.CString(cmd)
defer C.free(unsafe.Pointer(cmdstr))
var s C.duckdb_prepared_statement
if state := C.duckdb_prepare(c.duckdbCon, cmdstr, &s); state == C.DuckDBError {
dbErr := C.GoString(C.duckdb_prepare_error(s))
C.duckdb_destroy_prepare(&s)
return nil, errors.New(dbErr)
}
return &stmt{c: c, stmt: &s}, nil
}
func (c *conn) extractStmts(query string) (C.duckdb_extracted_statements, C.idx_t, error) {
cquery := C.CString(query)
defer C.free(unsafe.Pointer(cquery))
var stmts C.duckdb_extracted_statements
stmtsCount := C.duckdb_extract_statements(c.duckdbCon, cquery, &stmts)
if stmtsCount == 0 {
err := C.GoString(C.duckdb_extract_statements_error(stmts))
C.duckdb_destroy_extracted(&stmts)
if err != "" {
return nil, 0, errors.New(err)
}
return nil, 0, errors.New("no statements found")
}
return stmts, stmtsCount, nil
}
func (c *conn) prepareExtractedStmt(extractedStmts C.duckdb_extracted_statements, index C.idx_t) (*stmt, error) {
var s C.duckdb_prepared_statement
if state := C.duckdb_prepare_extracted_statement(c.duckdbCon, extractedStmts, index, &s); state == C.DuckDBError {
dbErr := C.GoString(C.duckdb_prepare_error(s))
C.duckdb_destroy_prepare(&s)
return nil, errors.New(dbErr)
}
return &stmt{c: c, stmt: &s}, nil
}