forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
159 lines (127 loc) · 3.71 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
package duckdb
/*
#include <duckdb.h>
*/
import "C"
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"math/big"
"unsafe"
)
type conn struct {
con *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")
}
if len(args) == 0 {
// This should be removed once duckdb_extract_statements and related APIs are available to parse multiple statements
// so query cancellation works for this case as well
return c.execUnprepared(query)
}
stmt, err := c.prepareStmt(query)
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")
}
if len(args) == 0 {
// This should be removed once duckdb_extract_statements and related APIs are available to parse multiple statements
// so query cancellation works for this case as well
return c.queryUnprepared(query)
}
stmt, err := c.prepareStmt(query)
if err != nil {
return nil, err
}
return stmt.QueryContext(ctx, args)
}
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.con)
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.con, 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) execUnprepared(cmd string) (driver.Result, error) {
cmdstr := C.CString(cmd)
defer C.free(unsafe.Pointer(cmdstr))
var res C.duckdb_result
err := C.duckdb_query(*c.con, cmdstr, &res)
defer C.duckdb_destroy_result(&res)
if err == C.DuckDBError {
dbErr := C.GoString(C.duckdb_result_error(&res))
return nil, errors.New(dbErr)
}
ra := int64(C.duckdb_value_int64(&res, 0, 0))
return &result{ra}, nil
}
func (c *conn) queryUnprepared(cmd string) (driver.Rows, 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 {
dbErr := C.GoString(C.duckdb_result_error(&res))
C.duckdb_destroy_result(&res)
return nil, errors.New(dbErr)
}
return newRows(res), nil
}