Skip to content

Commit

Permalink
Merge pull request #219 from slnt/slnt/query-rower
Browse files Browse the repository at this point in the history
Add support for standard `database/sql` runners sans Context methods
  • Loading branch information
lann authored Dec 6, 2019
2 parents a9f8687 + 83644f2 commit f867448
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
24 changes: 15 additions & 9 deletions squirrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package squirrel

import (
"bytes"
"context"
"database/sql"
"fmt"
"strings"
Expand Down Expand Up @@ -61,27 +60,34 @@ type Runner interface {
QueryRower
}

type stdsql interface {
// WrapStdSql wraps a type implementing the standard SQL interface with methods that
// squirrel expects.
func WrapStdSql(stdSql StdSql) Runner {
return &stdsqlRunner{stdSql}
}

// StdSql encompasses the standard methods of the *sql.DB type, and other types that
// wrap these methods.
type StdSql interface {
Query(string, ...interface{}) (*sql.Rows, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
Exec(string, ...interface{}) (sql.Result, error)
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

type stdsqlRunner struct {
stdsql
StdSql
}

func (r *stdsqlRunner) QueryRow(query string, args ...interface{}) RowScanner {
return r.stdsql.QueryRow(query, args...)
return r.StdSql.QueryRow(query, args...)
}

func setRunWith(b interface{}, runner BaseRunner) interface{} {
switch r := runner.(type) {
case stdsql:
runner = &stdsqlRunner{r}
case StdSqlCtx:
runner = WrapStdSqlCtx(r)
case StdSql:
runner = WrapStdSql(r)
}
return builder.Set(b, "RunWith", runner)
}
Expand Down
36 changes: 34 additions & 2 deletions squirrel_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,40 @@ type QueryRowerContext interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
}

func (r *stdsqlRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
return r.stdsql.QueryRowContext(ctx, query, args...)
// RunnerContext groups the Runner interface, along with the Contect versions of each of
// its methods
type RunnerContext interface {
Runner
QueryerContext
QueryRowerContext
ExecerContext
}

// WrapStdSqlCtx wraps a type implementing the standard SQL interface plus the context
// versions of the methods with methods that squirrel expects.
func WrapStdSqlCtx(stdSqlCtx StdSqlCtx) RunnerContext {
return &stdsqlCtxRunner{stdSqlCtx}
}

// StdSqlCtx encompasses the standard methods of the *sql.DB type, along with the Context
// versions of those methods, and other types that wrap these methods.
type StdSqlCtx interface {
StdSql
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

type stdsqlCtxRunner struct {
StdSqlCtx
}

func (r *stdsqlCtxRunner) QueryRow(query string, args ...interface{}) RowScanner {
return r.StdSqlCtx.QueryRow(query, args...)
}

func (r *stdsqlCtxRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
return r.StdSqlCtx.QueryRowContext(ctx, query, args...)
}

// ExecContextWith ExecContexts the SQL returned by s with db.
Expand Down

0 comments on commit f867448

Please sign in to comment.