Skip to content

Commit

Permalink
Replace namedValue with driver.NamedValue to avoid copying exec/query…
Browse files Browse the repository at this point in the history
… args (#1128)
  • Loading branch information
charlievieth committed Feb 11, 2023
1 parent 1603038 commit 7ce62b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
38 changes: 16 additions & 22 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,17 +837,17 @@ 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,
}
}
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)
Expand All @@ -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()
Expand Down Expand Up @@ -894,28 +894,22 @@ 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,
}
}
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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1982,17 +1976,17 @@ 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,
}
}
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
}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 4 additions & 20 deletions sqlite3_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

0 comments on commit 7ce62b2

Please sign in to comment.