Replace namedValue with driver.NamedValue to avoid copying exec/query args (#1128)

This commit is contained in:
Charlie Vieth 2023-02-11 17:14:42 -05:00 committed by GitHub
parent 1603038a4d
commit 7ce62b2ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 42 deletions

View File

@ -837,9 +837,9 @@ func lastError(db *C.sqlite3) error {
// Exec implements Execer. // Exec implements Execer.
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { 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 { for i, v := range args {
list[i] = namedValue{ list[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, 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) 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 start := 0
for { for {
s, err := c.prepare(ctx, query) 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 var res driver.Result
if s.(*SQLiteStmt).s != nil { if s.(*SQLiteStmt).s != nil {
stmtArgs := make([]namedValue, 0, len(args)) stmtArgs := make([]driver.NamedValue, 0, len(args))
na := s.NumInput() na := s.NumInput()
if len(args)-start < na { if len(args)-start < na {
s.Close() 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. // Query implements Queryer.
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) { 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 { for i, v := range args {
list[i] = namedValue{ list[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, 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) 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 start := 0
for { for {
stmtArgs := make([]namedValue, 0, len(args)) stmtArgs := make([]driver.NamedValue, 0, len(args))
s, err := c.prepare(ctx, query) s, err := c.prepare(ctx, query)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1912,7 +1906,7 @@ func (s *SQLiteStmt) NumInput() int {
var placeHolder = []byte{0} 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) rv := C.sqlite3_reset(s.s)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
return s.c.lastError() return s.c.lastError()
@ -1982,9 +1976,9 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
// Query the statement with arguments. Return records. // Query the statement with arguments. Return records.
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) { 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 { for i, v := range args {
list[i] = namedValue{ list[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, Value: v,
} }
@ -1992,7 +1986,7 @@ func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
return s.query(context.Background(), list) 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 { if err := s.bind(args); err != nil {
return nil, err return nil, err
} }
@ -2022,9 +2016,9 @@ func (r *SQLiteResult) RowsAffected() (int64, error) {
// Exec execute the statement with arguments. Return result object. // Exec execute the statement with arguments. Return result object.
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { 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 { for i, v := range args {
list[i] = namedValue{ list[i] = driver.NamedValue{
Ordinal: i + 1, Ordinal: i + 1,
Value: v, 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. // 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 { if ctx.Done() == nil {
return s.execSync(args) return s.execSync(args)
} }
@ -2073,7 +2067,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
return rv.r, rv.err 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 { if err := s.bind(args); err != nil {
C.sqlite3_reset(s.s) C.sqlite3_reset(s.s)
C.sqlite3_clear_bindings(s.s) C.sqlite3_clear_bindings(s.s)

View File

@ -25,20 +25,12 @@ func (c *SQLiteConn) Ping(ctx context.Context) error {
// QueryContext implement QueryerContext. // QueryContext implement QueryerContext.
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
list := make([]namedValue, len(args)) return c.query(ctx, query, args)
for i, nv := range args {
list[i] = namedValue(nv)
}
return c.query(ctx, query, list)
} }
// ExecContext implement ExecerContext. // ExecContext implement ExecerContext.
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
list := make([]namedValue, len(args)) return c.exec(ctx, query, args)
for i, nv := range args {
list[i] = namedValue(nv)
}
return c.exec(ctx, query, list)
} }
// PrepareContext implement ConnPrepareContext. // PrepareContext implement ConnPrepareContext.
@ -53,18 +45,10 @@ func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
// QueryContext implement QueryerContext. // QueryContext implement QueryerContext.
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
list := make([]namedValue, len(args)) return s.query(ctx, args)
for i, nv := range args {
list[i] = namedValue(nv)
}
return s.query(ctx, list)
} }
// ExecContext implement ExecerContext. // ExecContext implement ExecerContext.
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
list := make([]namedValue, len(args)) return s.exec(ctx, args)
for i, nv := range args {
list[i] = namedValue(nv)
}
return s.exec(ctx, list)
} }