forked from alexbrainman/odbc
/
conn.go
79 lines (68 loc) · 1.87 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
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
// 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 odbc
import (
"context"
"database/sql/driver"
"strings"
"github.com/ninthclowd/odbc/api"
"go.uber.org/atomic"
)
type Conn struct {
h api.SQLHDBC
tx *Tx
bad *atomic.Bool
closingInBG *atomic.Bool
isMSAccessDriver bool
}
var accessDriverSubstr = strings.ToUpper(strings.Replace("DRIVER={Microsoft Access Driver", " ", "", -1))
// implement driver.Conn
func (c *Conn) Close() (err error) {
if c.closingInBG.Load() {
//if we are cancelling/closing in a background thread, ignore requests to Close this connection from the driver
return nil
}
return c.close()
}
func (c *Conn) close() (err error) {
if c.tx != nil {
c.tx.Rollback()
}
h := c.h
defer func() {
c.h = api.SQLHDBC(api.SQL_NULL_HDBC)
e := releaseHandle(h)
if err == nil {
err = e
}
}()
ret := api.SQLDisconnect(c.h)
if IsError(ret) {
return c.newError("SQLDisconnect", h)
}
return err
}
func (c *Conn) newError(apiName string, handle interface{}) error {
err := NewError(apiName, handle)
if err == driver.ErrBadConn {
c.bad.Store(true)
}
return err
}
// implement driver.Conn
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}
// implement driver.Conn
func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
//implement driver.Execer
func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error) {
return c.ExecContext(context.Background(), query, toNamedValues(args))
}
//implement driver.Queryer
func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) {
return c.QueryContext(context.Background(), query, toNamedValues(args))
}