@@ -17,13 +17,12 @@ type Conn struct {
17
17
// It will trigger PrePing, Ping, PostPing hooks.
18
18
//
19
19
// If the original connection does not satisfy "database/sql/driver".Pinger, it does nothing.
20
- func (conn * Conn ) Ping (c context.Context ) error {
21
- var err error
20
+ func (conn * Conn ) Ping (c context.Context ) (err error ) {
22
21
var ctx interface {}
23
22
hooks := conn .Proxy .getHooks (c )
24
23
25
24
if hooks != nil {
26
- defer func () { hooks .postPing (c , ctx , conn , err ) }()
25
+ defer func () { err = hooks .postPing (c , ctx , conn , err ) }()
27
26
if ctx , err = hooks .prePing (c , conn ); err != nil {
28
27
return err
29
28
}
@@ -49,31 +48,30 @@ func (conn *Conn) Prepare(query string) (driver.Stmt, error) {
49
48
}
50
49
51
50
// PrepareContext returns a prepared statement which is wrapped by Stmt.
52
- func (conn * Conn ) PrepareContext (c context.Context , query string ) (driver.Stmt , error ) {
51
+ func (conn * Conn ) PrepareContext (c context.Context , query string ) (stmt driver.Stmt , err error ) {
53
52
var ctx interface {}
54
- var stmt = & Stmt {
53
+ var stmtAux = & Stmt {
55
54
QueryString : query ,
56
55
Proxy : conn .Proxy ,
57
56
Conn : conn ,
58
57
}
59
- var err error
60
58
hooks := conn .Proxy .getHooks (c )
61
59
if hooks != nil {
62
- defer func () { hooks .postPrepare (c , ctx , stmt , err ) }()
63
- if ctx , err = hooks .prePrepare (c , stmt ); err != nil {
60
+ defer func () { err = hooks .postPrepare (c , ctx , stmtAux , err ) }()
61
+ if ctx , err = hooks .prePrepare (c , stmtAux ); err != nil {
64
62
return nil , err
65
63
}
66
64
}
67
65
68
66
if connCtx , ok := conn .Conn .(driver.ConnPrepareContext ); ok {
69
- stmt .Stmt , err = connCtx .PrepareContext (c , stmt .QueryString )
67
+ stmtAux .Stmt , err = connCtx .PrepareContext (c , stmtAux .QueryString )
70
68
} else {
71
- stmt .Stmt , err = conn .Conn .Prepare (stmt .QueryString )
69
+ stmtAux .Stmt , err = conn .Conn .Prepare (stmtAux .QueryString )
72
70
if err == nil {
73
71
select {
74
72
default :
75
73
case <- c .Done ():
76
- stmt .Stmt .Close ()
74
+ stmtAux .Stmt .Close ()
77
75
return nil , c .Err ()
78
76
}
79
77
}
@@ -83,21 +81,20 @@ func (conn *Conn) PrepareContext(c context.Context, query string) (driver.Stmt,
83
81
}
84
82
85
83
if hooks != nil {
86
- if err = hooks .prepare (c , ctx , stmt ); err != nil {
84
+ if err = hooks .prepare (c , ctx , stmtAux ); err != nil {
87
85
return nil , err
88
86
}
89
87
}
90
- return stmt , nil
88
+ return stmtAux , nil
91
89
}
92
90
93
91
// Close calls the original Close method.
94
- func (conn * Conn ) Close () error {
92
+ func (conn * Conn ) Close () ( err error ) {
95
93
ctx := context .Background ()
96
- var err error
97
94
var myctx interface {}
98
95
99
96
if hooks := conn .Proxy .hooks ; hooks != nil {
100
- defer func () { hooks .postClose (ctx , myctx , conn , err ) }()
97
+ defer func () { err = hooks .postClose (ctx , myctx , conn , err ) }()
101
98
if myctx , err = hooks .preClose (ctx , conn ); err != nil {
102
99
return err
103
100
}
@@ -123,14 +120,12 @@ func (conn *Conn) Begin() (driver.Tx, error) {
123
120
124
121
// BeginTx starts and returns a new transaction which is wrapped by Tx.
125
122
// It will trigger PreBegin, Begin, PostBegin hooks.
126
- func (conn * Conn ) BeginTx (c context.Context , opts driver.TxOptions ) (driver.Tx , error ) {
123
+ func (conn * Conn ) BeginTx (c context.Context , opts driver.TxOptions ) (tx driver.Tx , err error ) {
127
124
// set the hooks.
128
- var err error
129
125
var ctx interface {}
130
- var tx driver.Tx
131
126
hooks := conn .Proxy .getHooks (c )
132
127
if hooks != nil {
133
- defer func () { hooks .postBegin (c , ctx , conn , err ) }()
128
+ defer func () { err = hooks .postBegin (c , ctx , conn , err ) }()
134
129
if ctx , err = hooks .preBegin (c , conn ); err != nil {
135
130
return nil , err
136
131
}
@@ -255,7 +250,7 @@ func (conn *Conn) Query(query string, args []driver.Value) (driver.Rows, error)
255
250
// It wil trigger PreQuery, Query, PostQuery hooks.
256
251
//
257
252
// If the original connection does not satisfy "database/sql/driver".QueryerContext nor "database/sql/driver".Queryer, it return ErrSkip error.
258
- func (conn * Conn ) QueryContext (c context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
253
+ func (conn * Conn ) QueryContext (c context.Context , query string , args []driver.NamedValue ) (rows driver.Rows , err error ) {
259
254
queryer , qok := conn .Conn .(driver.Queryer )
260
255
queryerCtx , qCtxOk := conn .Conn .(driver.QueryerContext )
261
256
if ! qok && ! qCtxOk {
@@ -268,11 +263,9 @@ func (conn *Conn) QueryContext(c context.Context, query string, args []driver.Na
268
263
Conn : conn ,
269
264
}
270
265
var ctx interface {}
271
- var err error
272
- var rows driver.Rows
273
266
hooks := conn .Proxy .getHooks (c )
274
267
if hooks != nil {
275
- defer func () { hooks .postQuery (c , ctx , stmt , args , rows , err ) }()
268
+ defer func () { err = hooks .postQuery (c , ctx , stmt , args , rows , err ) }()
276
269
if ctx , err = hooks .preQuery (c , stmt , args ); err != nil {
277
270
return nil , err
278
271
}
@@ -340,13 +333,12 @@ type sessionResetter interface {
340
333
}
341
334
342
335
// ResetSession resets the state of Conn.
343
- func (conn * Conn ) ResetSession (ctx context.Context ) error {
344
- var err error
336
+ func (conn * Conn ) ResetSession (ctx context.Context ) (err error ) {
345
337
var myctx interface {}
346
338
hooks := conn .Proxy .getHooks (ctx )
347
339
348
340
if hooks != nil {
349
- defer func () { hooks .postResetSession (ctx , myctx , conn , err ) }()
341
+ defer func () { err = hooks .postResetSession (ctx , myctx , conn , err ) }()
350
342
if myctx , err = hooks .preResetSession (ctx , conn ); err != nil {
351
343
return err
352
344
}
0 commit comments