Skip to content

Commit

Permalink
add methods to wrap standard sql runners for use with QueryRowWith me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
Simon Lantinga committed Dec 6, 2019
1 parent 6877020 commit 5d1d880
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
20 changes: 14 additions & 6 deletions squirrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,33 @@ 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)
QueryRow(string, ...interface{}) *sql.Row
Exec(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:
case StdSql:
runner = WrapStdSql(r)
case StdSqlCtx:
runner = &stdsqlCtxRunner{r}
}
return builder.Set(b, "RunWith", runner)
Expand Down
29 changes: 22 additions & 7 deletions squirrel_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,40 @@ type QueryRowerContext interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
}

type stdsqlCtx interface {
Query(string, ...interface{}) (*sql.Rows, error)
// 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)
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 stdsqlCtxRunner struct {
stdsqlCtx
StdSqlCtx
}

func (r *stdsqlCtxRunner) QueryRow(query string, args ...interface{}) RowScanner {
return r.stdsqlCtx.QueryRow(query, args...)
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...)
return r.StdSqlCtx.QueryRowContext(ctx, query, args...)
}

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

0 comments on commit 5d1d880

Please sign in to comment.