Skip to content

Commit 45494f5

Browse files
committed
Redundant defers.
1 parent 1b0bf34 commit 45494f5

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

driver/driver.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ func (n *connector) Connect(ctx context.Context) (ret driver.Conn, err error) {
241241
}
242242
}()
243243

244-
old := c.Conn.SetInterrupt(ctx)
245-
defer c.Conn.SetInterrupt(old)
244+
if old := c.Conn.SetInterrupt(ctx); old != ctx {
245+
defer c.Conn.SetInterrupt(old)
246+
}
246247

247248
if !n.pragmas {
248249
err = c.Conn.BusyTimeout(time.Minute)
@@ -362,8 +363,9 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
362363
c.txReset = `; PRAGMA query_only=` + string(c.readOnly)
363364
}
364365

365-
old := c.Conn.SetInterrupt(ctx)
366-
defer c.Conn.SetInterrupt(old)
366+
if old := c.Conn.SetInterrupt(ctx); old != ctx {
367+
defer c.Conn.SetInterrupt(old)
368+
}
367369

368370
err := c.Conn.Exec(txBegin)
369371
if err != nil {
@@ -382,8 +384,10 @@ func (c *conn) Commit() error {
382384

383385
func (c *conn) Rollback() error {
384386
// ROLLBACK even if interrupted.
385-
old := c.Conn.SetInterrupt(context.Background())
386-
defer c.Conn.SetInterrupt(old)
387+
ctx := context.Background()
388+
if old := c.Conn.SetInterrupt(ctx); old != ctx {
389+
defer c.Conn.SetInterrupt(old)
390+
}
387391
return c.Conn.Exec(`ROLLBACK` + c.txReset)
388392
}
389393

@@ -393,8 +397,9 @@ func (c *conn) Prepare(query string) (driver.Stmt, error) {
393397
}
394398

395399
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
396-
old := c.Conn.SetInterrupt(ctx)
397-
defer c.Conn.SetInterrupt(old)
400+
if old := c.Conn.SetInterrupt(ctx); old != ctx {
401+
defer c.Conn.SetInterrupt(old)
402+
}
398403

399404
s, tail, err := c.Conn.Prepare(query)
400405
if err != nil {
@@ -419,8 +424,9 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
419424
return resultRowsAffected(0), nil
420425
}
421426

422-
old := c.Conn.SetInterrupt(ctx)
423-
defer c.Conn.SetInterrupt(old)
427+
if old := c.Conn.SetInterrupt(ctx); old != ctx {
428+
defer c.Conn.SetInterrupt(old)
429+
}
424430

425431
err := c.Conn.Exec(query)
426432
if err != nil {
@@ -483,8 +489,10 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
483489
return nil, err
484490
}
485491

486-
old := s.Stmt.Conn().SetInterrupt(ctx)
487-
defer s.Stmt.Conn().SetInterrupt(old)
492+
c := s.Stmt.Conn()
493+
if old := c.SetInterrupt(ctx); old != ctx {
494+
defer c.SetInterrupt(old)
495+
}
488496

489497
err = errors.Join(
490498
s.Stmt.Exec(),
@@ -493,7 +501,7 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
493501
return nil, err
494502
}
495503

496-
return newResult(s.Stmt.Conn()), nil
504+
return newResult(c), nil
497505
}
498506

499507
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
@@ -678,13 +686,14 @@ func (r *rows) scanType(index int) scantype {
678686

679687
func (r *rows) loadColumnMetadata() {
680688
if r.nulls == nil {
689+
c := r.Stmt.Conn()
681690
count := r.Stmt.ColumnCount()
682691
nulls := make([]bool, count)
683692
types := make([]string, count)
684693
scans := make([]scantype, count)
685694
for i := range nulls {
686695
if col := r.Stmt.ColumnOriginName(i); col != "" {
687-
types[i], _, nulls[i], _, _, _ = r.Stmt.Conn().TableColumnMetadata(
696+
types[i], _, nulls[i], _, _, _ = c.TableColumnMetadata(
688697
r.Stmt.ColumnDatabaseName(i),
689698
r.Stmt.ColumnTableName(i),
690699
col)
@@ -762,8 +771,10 @@ func (r *rows) ColumnTypeScanType(index int) (typ reflect.Type) {
762771
}
763772

764773
func (r *rows) Next(dest []driver.Value) error {
765-
old := r.Stmt.Conn().SetInterrupt(r.ctx)
766-
defer r.Stmt.Conn().SetInterrupt(old)
774+
c := r.Stmt.Conn()
775+
if old := c.SetInterrupt(r.ctx); old != r.ctx {
776+
defer c.SetInterrupt(old)
777+
}
767778

768779
if !r.Stmt.Step() {
769780
if err := r.Stmt.Err(); err != nil {

0 commit comments

Comments
 (0)