-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathconn.go
269 lines (230 loc) · 6.56 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sql
import (
"context"
"database/sql/driver"
"seata.apache.org/seata-go/pkg/datasource/sql/exec"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
"seata.apache.org/seata-go/pkg/datasource/sql/util"
)
// Conn is a connection to a database. It is not used concurrently
// by multiple goroutines.
//
// Conn is assumed to be stateful.
type Conn struct {
res *DBResource
txCtx *types.TransactionContext
targetConn driver.Conn
autoCommit bool
dbName string
dbType types.DBType
}
// ResetSession is called prior to executing a query on the connection
// if the connection has been used before. If the driver returns ErrBadConn
// the connection is discarded.
func (c *Conn) ResetSession(ctx context.Context) error {
conn, ok := c.targetConn.(driver.SessionResetter)
if !ok {
return driver.ErrSkip
}
c.autoCommit = true
c.txCtx = types.NewTxCtx()
return conn.ResetSession(ctx)
}
// Prepare returns a prepared statement, bound to this connection.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
s, err := c.targetConn.Prepare(query)
if err != nil {
return nil, err
}
return &Stmt{
conn: c,
stmt: s,
query: query,
res: c.res,
txCtx: c.txCtx,
}, nil
}
// PrepareContext
func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
conn, ok := c.targetConn.(driver.ConnPrepareContext)
if !ok {
stmt, err := c.targetConn.Prepare(query)
if err != nil {
return nil, err
}
return &Stmt{stmt: stmt, query: query, res: c.res, txCtx: c.txCtx}, nil
}
s, err := conn.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &Stmt{
conn: c,
stmt: s,
query: query,
res: c.res,
txCtx: c.txCtx,
}, nil
}
// Exec warning: if you want to use global transaction, please use ExecContext function
func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error) {
conn, ok := c.targetConn.(driver.Execer)
if !ok {
return nil, driver.ErrSkip
}
ret, err := conn.Exec(query, args)
if err != nil {
return nil, err
}
return types.NewResult(types.WithResult(ret)).GetResult(), nil
}
// ExecContext
func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
targetConn, ok := c.targetConn.(driver.ExecerContext)
if !ok {
values := make([]driver.Value, 0, len(args))
for i := range args {
values = append(values, args[i].Value)
}
return c.Exec(query, values)
}
ret, err := targetConn.ExecContext(ctx, query, args)
if err != nil {
return nil, err
}
return types.NewResult(types.WithResult(ret)).GetResult(), nil
}
// Query
func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) {
conn, ok := c.targetConn.(driver.Queryer)
if !ok {
return nil, driver.ErrSkip
}
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TransactionMode, query)
if err != nil {
return nil, err
}
execCtx := &types.ExecContext{
TxCtx: c.txCtx,
Query: query,
Values: args,
}
ret, err := executor.ExecWithValue(context.Background(), execCtx,
func(ctx context.Context, query string, args []driver.NamedValue) (types.ExecResult, error) {
ret, err := conn.Query(query, util.NamedValueToValue(args))
if err != nil {
return nil, err
}
return types.NewResult(types.WithRows(ret)), nil
})
if err != nil {
return nil, err
}
return ret.GetRows(), nil
}
// QueryContext
func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
conn, ok := c.targetConn.(driver.QueryerContext)
if !ok {
values := make([]driver.Value, 0, len(args))
for i := range args {
values = append(values, args[i].Value)
}
return c.Query(query, values)
}
ret, err := conn.QueryContext(ctx, query, args)
if err != nil {
return nil, err
}
return types.NewResult(types.WithRows(ret)).GetRows(), nil
}
// Begin starts and returns a new transaction.
//
// Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
func (c *Conn) Begin() (driver.Tx, error) {
c.autoCommit = false
tx, err := c.targetConn.Begin()
if err != nil {
return nil, err
}
if c.txCtx == nil {
c.txCtx = types.NewTxCtx()
c.txCtx.DBType = c.res.dbType
c.txCtx.TxOpt = driver.TxOptions{}
}
return newTx(
withDriverConn(c),
withTxCtx(c.txCtx),
withOriginTx(tx),
)
}
// BeginTx Open a transaction and judge whether the current transaction needs to open a
//
// global transaction according to tranCtx. If so, it needs to be included in the transaction management of seata
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if c.txCtx.TransactionMode == types.XAMode {
c.autoCommit = false
return newTx(
withDriverConn(c),
withTxCtx(c.txCtx),
withOriginTx(nil),
)
}
if conn, ok := c.targetConn.(driver.ConnBeginTx); ok {
tx, err := conn.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return newTx(
withDriverConn(c),
withTxCtx(c.txCtx),
withOriginTx(tx),
)
}
txi, err := c.Begin()
if err != nil {
return nil, err
}
return newTx(
withDriverConn(c),
withTxCtx(c.txCtx),
withOriginTx(txi),
)
}
func (c *Conn) GetDbVersion() string {
return c.res.GetDbVersion()
}
func (c *Conn) GetAutoCommit() bool {
return c.autoCommit
}
// 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.
//
// Drivers must ensure all network calls made by Close
// do not block indefinitely (e.g. apply a timeout).
func (c *Conn) Close() error {
c.txCtx = types.NewTxCtx()
return c.targetConn.Close()
}