-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
68 lines (55 loc) · 1.16 KB
/
driver.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
package frontbase
/*
#cgo pkg-config: FBCAccess
#include "clib.h"
*/
import "C"
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"runtime"
"unsafe"
)
type Driver struct {
}
func (drv *Driver) OpenConnector(name string) (Connector, error) {
return Connector{
name: name,
driver: drv,
}, nil
}
func (drv *Driver) Open(name string) (driver.Conn, error) {
connector, err := drv.OpenConnector(name)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
func (drv *Driver) open(name string) (driver.Conn, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
conn := C.GoFBOpen(cname)
if conn == nil {
return nil, fmt.Errorf("drv %p: unable to open connection to '%s'", drv, name)
}
var newDrvConn = &Conn{
conn: conn,
}
if err := newDrvConn.setUTC(); err != nil {
newDrvConn.Close()
return nil, err
}
_, file, line, _ := runtime.Caller(1)
runtime.SetFinalizer(newDrvConn, func(dc *Conn) {
panic(fmt.Sprintf("%v: %s:%d: open connection never closed", dc, file, line))
})
return newDrvConn, nil
}
//
// Module Initialization
//
func init() {
sql.Register("frontbase", &Driver{})
}