From a0a9ab77375fb82d15ed21dfcab1a1274cbc215a Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 4 Aug 2023 14:12:36 +0100 Subject: [PATCH] Avoid unnecessary alloc. --- driver/driver.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/driver/driver.go b/driver/driver.go index 45c4b85..4dc8182 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -256,9 +256,7 @@ func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { } func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { - // Use QueryContext to setup bindings. - // No need to close rows: that simply resets the statement, exec does the same. - _, err := s.QueryContext(ctx, args) + err := s.setupBindings(ctx, args) if err != nil { return nil, err } @@ -272,11 +270,20 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive } func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { - err := s.Stmt.ClearBindings() + err := s.setupBindings(ctx, args) if err != nil { return nil, err } + return &rows{ctx, s.Stmt, s.Conn}, nil +} + +func (s *stmt) setupBindings(ctx context.Context, args []driver.NamedValue) error { + err := s.Stmt.ClearBindings() + if err != nil { + return err + } + var ids [3]int for _, arg := range args { ids := ids[:0] @@ -315,11 +322,10 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv } } if err != nil { - return nil, err + return err } } - - return &rows{ctx, s.Stmt, s.Conn}, nil + return nil } func (s *stmt) CheckNamedValue(arg *driver.NamedValue) error {