forked from SebastienBoisard/godb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
46 lines (40 loc) · 1.07 KB
/
conn.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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package godb2
import (
"github.com/sebastienboisard/godb2/api"
"database/sql/driver"
"unsafe"
)
type Conn struct {
h api.SQLHDBC
tx *Tx
}
func (d *Driver) Open(dsn string) (driver.Conn, error) {
var out api.SQLHANDLE
ret := api.SQLAllocHandle(api.SQL_HANDLE_DBC, api.SQLHANDLE(d.h), &out)
if IsError(ret) {
return nil, NewError("SQLAllocHandle", d.h)
}
h := api.SQLHDBC(out)
drv.Stats.updateHandleCount(api.SQL_HANDLE_DBC, 1)
b := api.StringToUTF16(dsn)
ret = api.SQLDriverConnect(h, 0,
(*api.SQLWCHAR)(unsafe.Pointer(&b[0])), api.SQLSMALLINT(len(b)),
nil, 0, nil, api.SQL_DRIVER_NOPROMPT)
if IsError(ret) {
defer releaseHandle(h)
return nil, NewError("SQLDriverConnect", h)
}
return &Conn{h: h}, nil
}
func (c *Conn) Close() error {
ret := api.SQLDisconnect(c.h)
if IsError(ret) {
return NewError("SQLDisconnect", c.h)
}
h := c.h
c.h = api.SQLHDBC(api.SQL_NULL_HDBC)
return releaseHandle(h)
}