@@ -17,13 +17,12 @@ type Conn struct {
1717// It will trigger PrePing, Ping, PostPing hooks.
1818//
1919// 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 ) {
2221 var ctx interface {}
2322 hooks := conn .Proxy .getHooks (c )
2423
2524 if hooks != nil {
26- defer func () { hooks .postPing (c , ctx , conn , err ) }()
25+ defer func () { err = hooks .postPing (c , ctx , conn , err ) }()
2726 if ctx , err = hooks .prePing (c , conn ); err != nil {
2827 return err
2928 }
@@ -49,31 +48,30 @@ func (conn *Conn) Prepare(query string) (driver.Stmt, error) {
4948}
5049
5150// 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 ) {
5352 var ctx interface {}
54- var stmt = & Stmt {
53+ var stmtAux = & Stmt {
5554 QueryString : query ,
5655 Proxy : conn .Proxy ,
5756 Conn : conn ,
5857 }
59- var err error
6058 hooks := conn .Proxy .getHooks (c )
6159 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 {
6462 return nil , err
6563 }
6664 }
6765
6866 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 )
7068 } else {
71- stmt .Stmt , err = conn .Conn .Prepare (stmt .QueryString )
69+ stmtAux .Stmt , err = conn .Conn .Prepare (stmtAux .QueryString )
7270 if err == nil {
7371 select {
7472 default :
7573 case <- c .Done ():
76- stmt .Stmt .Close ()
74+ stmtAux .Stmt .Close ()
7775 return nil , c .Err ()
7876 }
7977 }
@@ -83,21 +81,20 @@ func (conn *Conn) PrepareContext(c context.Context, query string) (driver.Stmt,
8381 }
8482
8583 if hooks != nil {
86- if err = hooks .prepare (c , ctx , stmt ); err != nil {
84+ if err = hooks .prepare (c , ctx , stmtAux ); err != nil {
8785 return nil , err
8886 }
8987 }
90- return stmt , nil
88+ return stmtAux , nil
9189}
9290
9391// Close calls the original Close method.
94- func (conn * Conn ) Close () error {
92+ func (conn * Conn ) Close () ( err error ) {
9593 ctx := context .Background ()
96- var err error
9794 var myctx interface {}
9895
9996 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 ) }()
10198 if myctx , err = hooks .preClose (ctx , conn ); err != nil {
10299 return err
103100 }
@@ -123,14 +120,12 @@ func (conn *Conn) Begin() (driver.Tx, error) {
123120
124121// BeginTx starts and returns a new transaction which is wrapped by Tx.
125122// 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 ) {
127124 // set the hooks.
128- var err error
129125 var ctx interface {}
130- var tx driver.Tx
131126 hooks := conn .Proxy .getHooks (c )
132127 if hooks != nil {
133- defer func () { hooks .postBegin (c , ctx , conn , err ) }()
128+ defer func () { err = hooks .postBegin (c , ctx , conn , err ) }()
134129 if ctx , err = hooks .preBegin (c , conn ); err != nil {
135130 return nil , err
136131 }
@@ -193,7 +188,7 @@ func (conn *Conn) Exec(query string, args []driver.Value) (driver.Result, error)
193188// It will trigger PreExec, Exec, PostExec hooks.
194189//
195190// If the original connection does not satisfy "database/sql/driver".ExecerContext nor "database/sql/driver".Execer, it return ErrSkip error.
196- func (conn * Conn ) ExecContext (c context.Context , query string , args []driver.NamedValue ) (driver.Result , error ) {
191+ func (conn * Conn ) ExecContext (c context.Context , query string , args []driver.NamedValue ) (drv driver.Result , err error ) {
197192 execer , exOk := conn .Conn .(driver.Execer )
198193 execerCtx , exCtxOk := conn .Conn .(driver.ExecerContext )
199194 if ! exOk && ! exCtxOk {
@@ -207,19 +202,17 @@ func (conn *Conn) ExecContext(c context.Context, query string, args []driver.Nam
207202 Conn : conn ,
208203 }
209204 var ctx interface {}
210- var err error
211- var result driver.Result
212205 hooks := conn .Proxy .getHooks (c )
213206 if hooks != nil {
214- defer func () { hooks .postExec (c , ctx , stmt , args , result , err ) }()
207+ defer func () { err = hooks .postExec (c , ctx , stmt , args , drv , err ) }()
215208 if ctx , err = hooks .preExec (c , stmt , args ); err != nil {
216209 return nil , err
217210 }
218211 }
219212
220213 // call the original method.
221214 if execerCtx != nil {
222- result , err = execerCtx .ExecContext (c , stmt .QueryString , args )
215+ drv , err = execerCtx .ExecContext (c , stmt .QueryString , args )
223216 } else {
224217 select {
225218 default :
@@ -230,19 +223,18 @@ func (conn *Conn) ExecContext(c context.Context, query string, args []driver.Nam
230223 if err0 != nil {
231224 return nil , err0
232225 }
233- result , err = execer .Exec (stmt .QueryString , dargs )
226+ drv , err = execer .Exec (stmt .QueryString , dargs )
234227 }
235228 if err != nil {
236229 return nil , err
237230 }
238231
239232 if hooks != nil {
240- if err = hooks .exec (c , ctx , stmt , args , result ); err != nil {
233+ if err = hooks .exec (c , ctx , stmt , args , drv ); err != nil {
241234 return nil , err
242235 }
243236 }
244-
245- return result , nil
237+ return drv , err
246238}
247239
248240// Query executes a query that may return rows.
@@ -258,7 +250,7 @@ func (conn *Conn) Query(query string, args []driver.Value) (driver.Rows, error)
258250// It wil trigger PreQuery, Query, PostQuery hooks.
259251//
260252// If the original connection does not satisfy "database/sql/driver".QueryerContext nor "database/sql/driver".Queryer, it return ErrSkip error.
261- 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 ) {
262254 queryer , qok := conn .Conn .(driver.Queryer )
263255 queryerCtx , qCtxOk := conn .Conn .(driver.QueryerContext )
264256 if ! qok && ! qCtxOk {
@@ -271,11 +263,9 @@ func (conn *Conn) QueryContext(c context.Context, query string, args []driver.Na
271263 Conn : conn ,
272264 }
273265 var ctx interface {}
274- var err error
275- var rows driver.Rows
276266 hooks := conn .Proxy .getHooks (c )
277267 if hooks != nil {
278- defer func () { hooks .postQuery (c , ctx , stmt , args , rows , err ) }()
268+ defer func () { err = hooks .postQuery (c , ctx , stmt , args , rows , err ) }()
279269 if ctx , err = hooks .preQuery (c , stmt , args ); err != nil {
280270 return nil , err
281271 }
@@ -343,13 +333,12 @@ type sessionResetter interface {
343333}
344334
345335// ResetSession resets the state of Conn.
346- func (conn * Conn ) ResetSession (ctx context.Context ) error {
347- var err error
336+ func (conn * Conn ) ResetSession (ctx context.Context ) (err error ) {
348337 var myctx interface {}
349338 hooks := conn .Proxy .getHooks (ctx )
350339
351340 if hooks != nil {
352- defer func () { hooks .postResetSession (ctx , myctx , conn , err ) }()
341+ defer func () { err = hooks .postResetSession (ctx , myctx , conn , err ) }()
353342 if myctx , err = hooks .preResetSession (ctx , conn ); err != nil {
354343 return err
355344 }
0 commit comments