Skip to content

Commit a0a9ab7

Browse files
committed
Avoid unnecessary alloc.
1 parent a77727a commit a0a9ab7

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

driver/driver.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
256256
}
257257

258258
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
259-
// Use QueryContext to setup bindings.
260-
// No need to close rows: that simply resets the statement, exec does the same.
261-
_, err := s.QueryContext(ctx, args)
259+
err := s.setupBindings(ctx, args)
262260
if err != nil {
263261
return nil, err
264262
}
@@ -272,11 +270,20 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
272270
}
273271

274272
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
275-
err := s.Stmt.ClearBindings()
273+
err := s.setupBindings(ctx, args)
276274
if err != nil {
277275
return nil, err
278276
}
279277

278+
return &rows{ctx, s.Stmt, s.Conn}, nil
279+
}
280+
281+
func (s *stmt) setupBindings(ctx context.Context, args []driver.NamedValue) error {
282+
err := s.Stmt.ClearBindings()
283+
if err != nil {
284+
return err
285+
}
286+
280287
var ids [3]int
281288
for _, arg := range args {
282289
ids := ids[:0]
@@ -315,11 +322,10 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
315322
}
316323
}
317324
if err != nil {
318-
return nil, err
325+
return err
319326
}
320327
}
321-
322-
return &rows{ctx, s.Stmt, s.Conn}, nil
328+
return nil
323329
}
324330

325331
func (s *stmt) CheckNamedValue(arg *driver.NamedValue) error {

0 commit comments

Comments
 (0)