-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathconn.go
More file actions
161 lines (144 loc) · 4.39 KB
/
conn.go
File metadata and controls
161 lines (144 loc) · 4.39 KB
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
160
161
// 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"
"errors"
"strings"
"unsafe"
"github.com/alexbrainman/odbc/api"
)
type Conn struct {
h api.SQLHDBC
tx *Tx
bad bool
isMSAccessDriver bool
}
var accessDriverSubstr = strings.ToUpper(strings.Replace("DRIVER={Microsoft Access Driver", " ", "", -1))
func (d *Driver) Open(dsn string) (driver.Conn, error) {
if d.initErr != nil {
return nil, d.initErr
}
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.SQL_NTS,
nil, 0, nil, api.SQL_DRIVER_NOPROMPT)
if IsError(ret) {
defer releaseHandle(h)
return nil, NewError("SQLDriverConnect", h)
}
isAccess := strings.Contains(strings.ToUpper(strings.Replace(dsn, " ", "", -1)), accessDriverSubstr)
return &Conn{h: h, isMSAccessDriver: isAccess}, nil
}
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 = true
}
return err
}
// QueryContext implements the driver.QueryerContext interface.
// As per the specifications, it honours the context timeout and returns when the context is cancelled.
// When the context is cancelled, it first cancels the statement, closes it, and then returns an error.
func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
// prepare the statement
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
os, err := c.PrepareODBCStmt(query)
if err != nil {
return nil, err
}
defer os.closeByStmt()
// check if context is canceled before executing the query
if ctx.Err() != nil {
return nil, ctx.Err()
}
// execute the statement
rowsChan := make(chan driver.Rows)
errorChan := make(chan error)
go func() {
err := c.wrapQuery(ctx, os, dargs)
if err != nil {
errorChan <- err
return
}
os.usedByRows = true
rowsChan <- &Rows{os: os}
}()
return c.waitQuery(ctx, os, rowsChan, errorChan)
}
// wrapQuery is following the same logic as `stmt.Query()` except that we don't use a lock
// because the ODBC statement doesn't get exposed externally.
func (c *Conn) wrapQuery(ctx context.Context, os *ODBCStmt, dargs []driver.Value) error {
if err := os.Exec(dargs, c); err != nil {
return err
}
if err := os.BindColumns(); err != nil {
return err
}
return nil
}
// waitQuery waits for either os rows or error to arrive from rowsChan and errorChan.
// waitQuery also waits for ctx to signal completion.
// The function returns received rows or the error.
func (c *Conn) waitQuery(ctx context.Context, os *ODBCStmt, rowsChan <-chan driver.Rows, errorChan <-chan error) (driver.Rows, error) {
select {
case <-ctx.Done():
// context has been cancelled or has expired, cancel the statement and ignore the os.Cancel error
os.Cancel()
// the statement has been cancelled, the query execution should eventually succeed or fail now
select {
// ignore the ODBC error and return ctx.Err() instead
case <-errorChan:
return nil, ctx.Err()
case rows := <-rowsChan:
return rows, nil
}
case err := <-errorChan:
return nil, err
case rows := <-rowsChan:
return rows, nil
}
}
// namedValueToValue is a utility function that converts a driver.NamedValue into a driver.Value.
// Source:
// https://github.com/golang/go/blob/03ac39ce5e6af4c4bca58b54d5b160a154b7aa0e/src/database/sql/ctxutil.go#L137-L146
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
dargs := make([]driver.Value, len(named))
for n, param := range named {
if len(param.Name) > 0 {
return nil, errors.New("sql: driver does not support the use of Named Parameters")
}
dargs[n] = param.Value
}
return dargs, nil
}