-
Notifications
You must be signed in to change notification settings - Fork 4
/
connection.go
163 lines (129 loc) · 3.96 KB
/
connection.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
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
162
163
package lodbc
import (
"database/sql/driver"
"fmt"
"github.com/LukeMauldin/lodbc/odbc"
"runtime"
"unsafe"
)
// Implements type database/sql/driver Conn interface
type connection struct {
// Connection handle
handle odbc.SQLHandle
// Is transaction active
isTransactionActive bool
// Statements owned by the connection
statements map[driver.Stmt]bool
// Is closed -- allows Close() to be called multiple times without error
isClosed bool
}
// Prepare returns a prepared statement, bound to this connection
func (c *connection) Prepare(query string) (driver.Stmt, error) {
// Allocate the statement handle
var stmtHandle odbc.SQLHandle
ret := odbc.SQLAllocHandle(odbc.SQL_HANDLE_STMT, c.handle, &stmtHandle)
if isError(ret) {
return nil, errorConnection(c.handle)
}
// Set the query timeout
ret = odbc.SQLSetStmtAttr(stmtHandle, odbc.SQL_ATTR_QUERY_TIMEOUT, odbc.SQLPOINTER(queryTimeout.Seconds()), odbc.SQL_IS_INTEGER)
if isError(ret) {
return nil, errorStatement(stmtHandle, query)
}
// Get the statement descriptor table
var stmtDescHandle odbc.SQLHandle
ret = odbc.SQLGetStmtAttr(stmtHandle, odbc.SQL_ATTR_APP_PARAM_DESC, uintptr(unsafe.Pointer(&stmtDescHandle)), 0, nil)
if isError(ret) {
return nil, errorConnection(c.handle)
}
// Parse query options
queryOptions, err := parseQueryOptions(query)
if err != nil {
return nil, err
}
// Remove query options from SQL query
query = removeOptions(query)
// Create new statement
stmt := &statement{handle: stmtHandle, stmtDescHandle: stmtDescHandle, sqlStmt: query, conn: c, queryOptions: queryOptions}
// Add to map of statements owned by the connection
c.statements[stmt] = true
//Add a finalizer
runtime.SetFinalizer(stmt, (*statement).Close)
return stmt, nil
}
// Close invalidates and potentially stops any current
// prepared statements and transactions, marking this
// connection as no longer in use.
func (c *connection) Close() error {
// Verify that connHandle is valid
if c.handle == 0 {
return nil
}
// Verify that connection has not already been closed
if c.isClosed {
return nil
}
var err error
// Close all of the statements owned by the connection
for key, _ := range c.statements {
// Skip the statement if it is already nil
if isNil(key) {
continue
}
key.Close()
}
c.statements = nil
// If the transaction is active, roll it back
if c.isTransactionActive {
ret := odbc.SQLEndTran(odbc.SQL_HANDLE_DBC, c.handle, odbc.SQL_ROLLBACK)
if isError(ret) {
err = errorConnection(c.handle)
}
//Turn AutoCommit back on
ret = odbc.SQLSetConnectAttr(c.handle, odbc.SQL_ATTR_AUTOCOMMIT, odbc.SQLPOINTER(odbc.SQL_AUTOCOMMIT_ON), 0, nil)
if isError(ret) {
err = errorConnection(c.handle)
}
}
// Disconnect connection
ret := odbc.SQLDisconnect(c.handle)
if isError(ret) {
err = errorConnection(c.handle)
}
// Deallocate connection
ret = odbc.SQLFreeHandle(odbc.SQL_HANDLE_DBC, c.handle)
if isError(ret) {
err = errorConnection(c.handle)
}
// Clear the handle
c.handle = 0
// Set connection to closed
c.isClosed = true
//Clear the finalizer
runtime.SetFinalizer(c, nil)
// Return any error
if err != nil {
return err
}
return nil
}
// Begin starts and returns a new transaction
// Only one transaction is supported at a time for a connection
func (c *connection) Begin() (driver.Tx, error) {
// Do not allow a new transaction if one already exists
if c.isTransactionActive {
return nil, fmt.Errorf("Transaction already active for connection")
}
ret := odbc.SQLSetConnectAttr(c.handle, odbc.SQL_ATTR_AUTOCOMMIT, odbc.SQLPOINTER(odbc.SQL_AUTOCOMMIT_OFF), 0, nil)
if isError(ret) {
return nil, errorConnection(c.handle)
}
c.isTransactionActive = true
tx := &transaction{conn: c}
return tx, nil
}
// To be called by the statements owned by the connection when the statement is closed
// Removed the statement from the connection's list of statements'
func (c *connection) closeStatement(stmt driver.Stmt) {
delete(c.statements, stmt)
}