From 7ce62b2ade0b712c46b1ecc6a0ee179dc417816c Mon Sep 17 00:00:00 2001 From: Charlie Vieth Date: Sat, 11 Feb 2023 17:14:42 -0500 Subject: [PATCH] Replace namedValue with driver.NamedValue to avoid copying exec/query args (#1128) --- sqlite3.go | 38 ++++++++++++++++---------------------- sqlite3_go18.go | 24 ++++-------------------- 2 files changed, 20 insertions(+), 42 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 9c0f4d8..5e4e2ff 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -837,9 +837,9 @@ func lastError(db *C.sqlite3) error { // Exec implements Execer. func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { - list := make([]namedValue, len(args)) + list := make([]driver.NamedValue, len(args)) for i, v := range args { - list[i] = namedValue{ + list[i] = driver.NamedValue{ Ordinal: i + 1, Value: v, } @@ -847,7 +847,7 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err return c.exec(context.Background(), query, list) } -func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) { +func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { start := 0 for { s, err := c.prepare(ctx, query) @@ -856,7 +856,7 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) } var res driver.Result if s.(*SQLiteStmt).s != nil { - stmtArgs := make([]namedValue, 0, len(args)) + stmtArgs := make([]driver.NamedValue, 0, len(args)) na := s.NumInput() if len(args)-start < na { s.Close() @@ -894,17 +894,11 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) } } -type namedValue struct { - Name string - Ordinal int - Value driver.Value -} - // Query implements Queryer. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) { - list := make([]namedValue, len(args)) + list := make([]driver.NamedValue, len(args)) for i, v := range args { - list[i] = namedValue{ + list[i] = driver.NamedValue{ Ordinal: i + 1, Value: v, } @@ -912,10 +906,10 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro return c.query(context.Background(), query, list) } -func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) { +func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { start := 0 for { - stmtArgs := make([]namedValue, 0, len(args)) + stmtArgs := make([]driver.NamedValue, 0, len(args)) s, err := c.prepare(ctx, query) if err != nil { return nil, err @@ -1912,7 +1906,7 @@ func (s *SQLiteStmt) NumInput() int { var placeHolder = []byte{0} -func (s *SQLiteStmt) bind(args []namedValue) error { +func (s *SQLiteStmt) bind(args []driver.NamedValue) error { rv := C.sqlite3_reset(s.s) if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { return s.c.lastError() @@ -1982,9 +1976,9 @@ func (s *SQLiteStmt) bind(args []namedValue) error { // Query the statement with arguments. Return records. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) { - list := make([]namedValue, len(args)) + list := make([]driver.NamedValue, len(args)) for i, v := range args { - list[i] = namedValue{ + list[i] = driver.NamedValue{ Ordinal: i + 1, Value: v, } @@ -1992,7 +1986,7 @@ func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) { return s.query(context.Background(), list) } -func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) { +func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { if err := s.bind(args); err != nil { return nil, err } @@ -2022,9 +2016,9 @@ func (r *SQLiteResult) RowsAffected() (int64, error) { // Exec execute the statement with arguments. Return result object. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { - list := make([]namedValue, len(args)) + list := make([]driver.NamedValue, len(args)) for i, v := range args { - list[i] = namedValue{ + list[i] = driver.NamedValue{ Ordinal: i + 1, Value: v, } @@ -2041,7 +2035,7 @@ func isInterruptErr(err error) bool { } // exec executes a query that doesn't return rows. Attempts to honor context timeout. -func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) { +func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { if ctx.Done() == nil { return s.execSync(args) } @@ -2073,7 +2067,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result return rv.r, rv.err } -func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) { +func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) { if err := s.bind(args); err != nil { C.sqlite3_reset(s.s) C.sqlite3_clear_bindings(s.s) diff --git a/sqlite3_go18.go b/sqlite3_go18.go index bd97cb8..514fd7e 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -25,20 +25,12 @@ func (c *SQLiteConn) Ping(ctx context.Context) error { // QueryContext implement QueryerContext. func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { - list := make([]namedValue, len(args)) - for i, nv := range args { - list[i] = namedValue(nv) - } - return c.query(ctx, query, list) + return c.query(ctx, query, args) } // ExecContext implement ExecerContext. func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { - list := make([]namedValue, len(args)) - for i, nv := range args { - list[i] = namedValue(nv) - } - return c.exec(ctx, query, list) + return c.exec(ctx, query, args) } // PrepareContext implement ConnPrepareContext. @@ -53,18 +45,10 @@ func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver // QueryContext implement QueryerContext. func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { - list := make([]namedValue, len(args)) - for i, nv := range args { - list[i] = namedValue(nv) - } - return s.query(ctx, list) + return s.query(ctx, args) } // ExecContext implement ExecerContext. func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { - list := make([]namedValue, len(args)) - for i, nv := range args { - list[i] = namedValue(nv) - } - return s.exec(ctx, list) + return s.exec(ctx, args) }