forked from nakagami/firebirdsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
219 lines (175 loc) · 6.07 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2013-2019 Hajime Nakagami
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"context"
"database/sql/driver"
"math/big"
)
type firebirdsqlConn struct {
wp *wireProtocol
tx *firebirdsqlTx
dsn *firebirdDsn
columnNameToLower bool
isAutocommit bool
clientPublic *big.Int
clientSecret *big.Int
transactionSet map[*firebirdsqlTx]struct{}
}
// ============ driver.Conn implementation
func (fc *firebirdsqlConn) begin(isolationLevel int) (driver.Tx, error) {
tx, err := newFirebirdsqlTx(fc, isolationLevel, false, true)
fc.tx = tx
return driver.Tx(tx), err
}
// Begin starts and returns a new transaction.
//
// Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
// -> is implemented in driver_go18.go with BeginTx()
func (fc *firebirdsqlConn) Begin() (driver.Tx, error) {
return fc.begin(ISOLATION_LEVEL_READ_COMMITED)
}
// Close invalidates and potentially stops any current
// prepared statements and transactions, marking this
// connection as no longer in use.
//
// Because the sql package maintains a free pool of
// connections and only calls Close when there's a surplus of
// idle connections, it shouldn't be necessary for drivers to
// do their own connection caching.
func (fc *firebirdsqlConn) Close() (err error) {
for tx := range fc.transactionSet {
tx.Rollback()
}
err = fc.wp.opDetach()
if err != nil {
return
}
_, _, _, err = fc.wp.opResponse()
fc.wp.conn.Close()
return
}
func (fc *firebirdsqlConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
if fc.tx.needBegin {
err := fc.tx.begin()
if err != nil {
return nil, err
}
}
return newFirebirdsqlStmt(fc, query)
}
// Prepare returns a prepared statement, bound to this connection.
func (fc *firebirdsqlConn) Prepare(query string) (driver.Stmt, error) {
return fc.prepare(context.Background(), query)
}
// ============ driver.Tx implementation
func (fc *firebirdsqlConn) exec(ctx context.Context, query string, args []driver.Value) (result driver.Result, err error) {
stmt, err := fc.prepare(ctx, query)
if err != nil {
return
}
result, err = stmt.(*firebirdsqlStmt).exec(ctx, args)
if err != nil {
return
}
stmt.Close()
return
}
func (fc *firebirdsqlConn) Exec(query string, args []driver.Value) (result driver.Result, err error) {
return fc.exec(context.Background(), query, args)
}
func (fc *firebirdsqlConn) query(ctx context.Context, query string, args []driver.Value) (rows driver.Rows, err error) {
stmt, err := fc.prepare(ctx, query)
if err != nil {
return
}
rows, err = stmt.(*firebirdsqlStmt).query(ctx, args)
return
}
func (fc *firebirdsqlConn) Query(query string, args []driver.Value) (rows driver.Rows, err error) {
return fc.query(context.Background(), query, args)
}
func newFirebirdsqlConn(dsn *firebirdDsn) (fc *firebirdsqlConn, err error) {
wp, err := newWireProtocol(dsn.addr, dsn.options["timezone"], dsn.options["charset"])
if err != nil {
return
}
column_name_to_lower := convertToBool(dsn.options["column_name_to_lower"], false)
clientPublic, clientSecret := getClientSeed()
err = wp.opConnect(dsn.dbName, dsn.user, dsn.passwd, dsn.options, clientPublic)
if err != nil {
return
}
err = wp._parse_connect_response(dsn.user, dsn.passwd, dsn.options, clientPublic, clientSecret)
if err != nil {
return
}
err = wp.opAttach(dsn.dbName, dsn.user, dsn.passwd, dsn.options["role"])
if err != nil {
return
}
wp.dbHandle, _, _, err = wp.opResponse()
if err != nil {
return
}
fc = new(firebirdsqlConn)
fc.transactionSet = make(map[*firebirdsqlTx]struct{})
fc.wp = wp
fc.dsn = dsn
fc.columnNameToLower = column_name_to_lower
fc.isAutocommit = true
fc.tx, err = newFirebirdsqlTx(fc, ISOLATION_LEVEL_READ_COMMITED, fc.isAutocommit, false)
fc.clientPublic = clientPublic
fc.clientSecret = clientSecret
return fc, err
}
func createFirebirdsqlConn(dsn *firebirdDsn) (fc *firebirdsqlConn, err error) {
wp, err := newWireProtocol(dsn.addr, dsn.options["timezone"], dsn.options["charset"])
if err != nil {
return
}
column_name_to_lower := convertToBool(dsn.options["column_name_to_lower"], false)
clientPublic, clientSecret := getClientSeed()
err = wp.opConnect(dsn.dbName, dsn.user, dsn.passwd, dsn.options, clientPublic)
if err != nil {
return
}
err = wp._parse_connect_response(dsn.user, dsn.passwd, dsn.options, clientPublic, clientSecret)
if err != nil {
return
}
err = wp.opCreate(dsn.dbName, dsn.user, dsn.passwd, dsn.options["role"])
if err != nil {
return
}
wp.dbHandle, _, _, err = wp.opResponse()
if err != nil {
return
}
fc = new(firebirdsqlConn)
fc.transactionSet = make(map[*firebirdsqlTx]struct{})
fc.wp = wp
fc.dsn = dsn
fc.columnNameToLower = column_name_to_lower
fc.isAutocommit = true
fc.tx, err = newFirebirdsqlTx(fc, ISOLATION_LEVEL_READ_COMMITED, fc.isAutocommit, false)
fc.clientPublic = clientPublic
fc.clientSecret = clientSecret
return fc, err
}