forked from Masterminds/squirrel
/
squirrel_ctx.go
48 lines (39 loc) · 1.27 KB
/
squirrel_ctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// +build go1.8
package squirrel
import (
"context"
"errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
// NoContextSupport is returned if a db doesn't support Context.
var NoContextSupport = errors.New("DB does not support Context")
// WrapStdSqlCtx wraps a type implementing the standard SQL interface plus the context
// versions of the methods with methods that squirrel expects.
func WrapStdSqlCtx(stdSqlCtx StdSql) Runner {
return &stdsqlCtxRunner{stdSqlCtx}
}
type stdsqlCtxRunner struct {
StdSql
}
// ExecContextWith ExecContexts the SQL returned by s with db.
func ExecContextWith(ctx context.Context, db Execer, s Sqlizer) (res pgconn.CommandTag, err error) {
query, args, err := s.ToSql()
if err != nil {
return
}
return db.Exec(ctx, query, args...)
}
// QueryContextWith QueryContexts the SQL returned by s with db.
func QueryContextWith(ctx context.Context, db Queryer, s Sqlizer) (rows pgx.Rows, err error) {
query, args, err := s.ToSql()
if err != nil {
return
}
return db.Query(ctx, query, args...)
}
// QueryRowContextWith QueryRowContexts the SQL returned by s with db.
func QueryRowContextWith(ctx context.Context, db QueryRower, s Sqlizer) pgx.Row {
query, args, err := s.ToSql()
return &Row{Row: db.QueryRow(ctx, query, args...), err: err}
}