forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduckdb.go
45 lines (35 loc) · 846 Bytes
/
duckdb.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
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package duckdb implements a databse/sql driver for the DuckDB database.
package duckdb
/*
#cgo LDFLAGS: -lduckdb
#include <duckdb.h>
*/
import "C"
import (
"database/sql"
"database/sql/driver"
"errors"
"unsafe"
)
func init() {
sql.Register("duckdb", impl{})
}
type impl struct{}
func (impl) Open(name string) (driver.Conn, error) {
var db C.duckdb_database
var con C.duckdb_connection
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if err := C.duckdb_open(cname, &db); err == C.DuckDBError {
return nil, errError
}
if err := C.duckdb_connect(db, &con); err == C.DuckDBError {
return nil, errError
}
return &conn{db: &db, con: &con}, nil
}
var (
errError = errors.New("could not open database")
)