Skip to content

Commit 7ce62b2

Browse files
authored
Replace namedValue with driver.NamedValue to avoid copying exec/query args (#1128)
1 parent 1603038 commit 7ce62b2

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

sqlite3.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -837,17 +837,17 @@ func lastError(db *C.sqlite3) error {
837837

838838
// Exec implements Execer.
839839
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
840-
list := make([]namedValue, len(args))
840+
list := make([]driver.NamedValue, len(args))
841841
for i, v := range args {
842-
list[i] = namedValue{
842+
list[i] = driver.NamedValue{
843843
Ordinal: i + 1,
844844
Value: v,
845845
}
846846
}
847847
return c.exec(context.Background(), query, list)
848848
}
849849

850-
func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) {
850+
func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
851851
start := 0
852852
for {
853853
s, err := c.prepare(ctx, query)
@@ -856,7 +856,7 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue)
856856
}
857857
var res driver.Result
858858
if s.(*SQLiteStmt).s != nil {
859-
stmtArgs := make([]namedValue, 0, len(args))
859+
stmtArgs := make([]driver.NamedValue, 0, len(args))
860860
na := s.NumInput()
861861
if len(args)-start < na {
862862
s.Close()
@@ -894,28 +894,22 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue)
894894
}
895895
}
896896

897-
type namedValue struct {
898-
Name string
899-
Ordinal int
900-
Value driver.Value
901-
}
902-
903897
// Query implements Queryer.
904898
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
905-
list := make([]namedValue, len(args))
899+
list := make([]driver.NamedValue, len(args))
906900
for i, v := range args {
907-
list[i] = namedValue{
901+
list[i] = driver.NamedValue{
908902
Ordinal: i + 1,
909903
Value: v,
910904
}
911905
}
912906
return c.query(context.Background(), query, list)
913907
}
914908

915-
func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
909+
func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
916910
start := 0
917911
for {
918-
stmtArgs := make([]namedValue, 0, len(args))
912+
stmtArgs := make([]driver.NamedValue, 0, len(args))
919913
s, err := c.prepare(ctx, query)
920914
if err != nil {
921915
return nil, err
@@ -1912,7 +1906,7 @@ func (s *SQLiteStmt) NumInput() int {
19121906

19131907
var placeHolder = []byte{0}
19141908

1915-
func (s *SQLiteStmt) bind(args []namedValue) error {
1909+
func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
19161910
rv := C.sqlite3_reset(s.s)
19171911
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
19181912
return s.c.lastError()
@@ -1982,17 +1976,17 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
19821976

19831977
// Query the statement with arguments. Return records.
19841978
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
1985-
list := make([]namedValue, len(args))
1979+
list := make([]driver.NamedValue, len(args))
19861980
for i, v := range args {
1987-
list[i] = namedValue{
1981+
list[i] = driver.NamedValue{
19881982
Ordinal: i + 1,
19891983
Value: v,
19901984
}
19911985
}
19921986
return s.query(context.Background(), list)
19931987
}
19941988

1995-
func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
1989+
func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
19961990
if err := s.bind(args); err != nil {
19971991
return nil, err
19981992
}
@@ -2022,9 +2016,9 @@ func (r *SQLiteResult) RowsAffected() (int64, error) {
20222016

20232017
// Exec execute the statement with arguments. Return result object.
20242018
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
2025-
list := make([]namedValue, len(args))
2019+
list := make([]driver.NamedValue, len(args))
20262020
for i, v := range args {
2027-
list[i] = namedValue{
2021+
list[i] = driver.NamedValue{
20282022
Ordinal: i + 1,
20292023
Value: v,
20302024
}
@@ -2041,7 +2035,7 @@ func isInterruptErr(err error) bool {
20412035
}
20422036

20432037
// exec executes a query that doesn't return rows. Attempts to honor context timeout.
2044-
func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
2038+
func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
20452039
if ctx.Done() == nil {
20462040
return s.execSync(args)
20472041
}
@@ -2073,7 +2067,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
20732067
return rv.r, rv.err
20742068
}
20752069

2076-
func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
2070+
func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
20772071
if err := s.bind(args); err != nil {
20782072
C.sqlite3_reset(s.s)
20792073
C.sqlite3_clear_bindings(s.s)

sqlite3_go18.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,12 @@ func (c *SQLiteConn) Ping(ctx context.Context) error {
2525

2626
// QueryContext implement QueryerContext.
2727
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
28-
list := make([]namedValue, len(args))
29-
for i, nv := range args {
30-
list[i] = namedValue(nv)
31-
}
32-
return c.query(ctx, query, list)
28+
return c.query(ctx, query, args)
3329
}
3430

3531
// ExecContext implement ExecerContext.
3632
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
37-
list := make([]namedValue, len(args))
38-
for i, nv := range args {
39-
list[i] = namedValue(nv)
40-
}
41-
return c.exec(ctx, query, list)
33+
return c.exec(ctx, query, args)
4234
}
4335

4436
// PrepareContext implement ConnPrepareContext.
@@ -53,18 +45,10 @@ func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
5345

5446
// QueryContext implement QueryerContext.
5547
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
56-
list := make([]namedValue, len(args))
57-
for i, nv := range args {
58-
list[i] = namedValue(nv)
59-
}
60-
return s.query(ctx, list)
48+
return s.query(ctx, args)
6149
}
6250

6351
// ExecContext implement ExecerContext.
6452
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
65-
list := make([]namedValue, len(args))
66-
for i, nv := range args {
67-
list[i] = namedValue(nv)
68-
}
69-
return s.exec(ctx, list)
53+
return s.exec(ctx, args)
7054
}

0 commit comments

Comments
 (0)