Skip to content

Commit 84ff06a

Browse files
charlieviethmeiyang1123
authored andcommitted
Replace namedValue with driver.NamedValue to avoid copying exec/query args (mattn#1128)
1 parent acf96be commit 84ff06a

2 files changed

Lines changed: 20 additions & 42 deletions

File tree

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()
@@ -1985,17 +1979,17 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
19851979

19861980
// Query the statement with arguments. Return records.
19871981
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
1988-
list := make([]namedValue, len(args))
1982+
list := make([]driver.NamedValue, len(args))
19891983
for i, v := range args {
1990-
list[i] = namedValue{
1984+
list[i] = driver.NamedValue{
19911985
Ordinal: i + 1,
19921986
Value: v,
19931987
}
19941988
}
19951989
return s.query(context.Background(), list)
19961990
}
19971991

1998-
func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
1992+
func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
19991993
if err := s.bind(args); err != nil {
20001994
return nil, err
20011995
}
@@ -2025,9 +2019,9 @@ func (r *SQLiteResult) RowsAffected() (int64, error) {
20252019

20262020
// Exec execute the statement with arguments. Return result object.
20272021
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
2028-
list := make([]namedValue, len(args))
2022+
list := make([]driver.NamedValue, len(args))
20292023
for i, v := range args {
2030-
list[i] = namedValue{
2024+
list[i] = driver.NamedValue{
20312025
Ordinal: i + 1,
20322026
Value: v,
20332027
}
@@ -2044,7 +2038,7 @@ func isInterruptErr(err error) bool {
20442038
}
20452039

20462040
// exec executes a query that doesn't return rows. Attempts to honor context timeout.
2047-
func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
2041+
func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
20482042
if ctx.Done() == nil {
20492043
return s.execSync(args)
20502044
}
@@ -2076,7 +2070,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
20762070
return rv.r, rv.err
20772071
}
20782072

2079-
func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
2073+
func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
20802074
if err := s.bind(args); err != nil {
20812075
C.sqlite3_reset(s.s)
20822076
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)