-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnamed_context.go
More file actions
194 lines (177 loc) · 6.37 KB
/
named_context.go
File metadata and controls
194 lines (177 loc) · 6.37 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package sqlx
import (
"context"
"database/sql"
"iter"
)
// A union interface of contextPreparer and binder, required to be able to
// prepare named statements with context (as the bindtype must be determined).
type namedPreparerContext interface {
PreparerContext
binder
}
// PrepareNamedContext prepares a named statement for use on the database. Use `PrepareContext` on
// the statement to ready a prepared statement to be used in a transaction.
//
// The returned statement operates within the transaction and will be closed
// when the transaction has been committed or rolled back (you do not need to close it).
func PrepareNamedContext[T any](ctx context.Context, p namedPreparerContext, query string) (*GenericNamedStmt[T], error) {
bindType := BindType(p.DriverName())
compiled, err := compileNamedQuery([]byte(query), bindType)
if err != nil {
return nil, err
}
stmt, err := PreparexContext[T](ctx, p, compiled.query)
if err != nil {
return nil, err
}
return &GenericNamedStmt[T]{
QueryString: compiled.query,
Params: compiled.names,
Stmt: stmt,
}, nil
}
// PrepareContext returns a transaction-specific prepared statement from
// an existing statement.
//
// It's preferred to use this method over `Prepare` (without context) due to go internals, it
// uses the connection found in context.
//
// The returned statement operates within the transaction and will be closed
// when the transaction has been committed or rolled back (you do not need to close it).
func (n *GenericNamedStmt[T]) PrepareContext(ctx context.Context, ndb Queryable) *GenericNamedStmt[T] {
tx, ok := ndb.(*Tx)
if !ok {
// not needed
return n
}
return &GenericNamedStmt[T]{
Params: n.Params,
QueryString: n.QueryString,
Stmt: &GenericStmt[T]{
Stmt: tx.StmtContext(ctx, n.Stmt.Stmt),
options: n.Stmt.options,
Mapper: n.Stmt.Mapper,
},
}
}
// ExecContext executes a named statement using the struct passed.
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) ExecContext(ctx context.Context, arg any) (sql.Result, error) {
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
if err != nil {
return *new(sql.Result), err
}
return n.Stmt.ExecContext(ctx, args...)
}
// QueryContext executes a named statement using the struct argument, returning rows.
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) QueryContext(ctx context.Context, arg any) (*sql.Rows, error) {
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
if err != nil {
return nil, err
}
return n.Stmt.QueryContext(ctx, args...)
}
// QueryRowContext executes a named statement against the database. Because sqlx cannot
// create a *sql.Row with an error condition pre-set for binding errors, sqlx
// returns a *sqlx.Row instead.
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) QueryRowContext(ctx context.Context, arg any) *Row {
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
if err != nil {
return &Row{err: err}
}
return n.Stmt.QueryRowxContext(ctx, args...)
}
// MustExecContext execs a NamedStmt, panicing on error
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) MustExecContext(ctx context.Context, arg any) sql.Result {
res, err := n.ExecContext(ctx, arg)
if err != nil {
panic(err)
}
return res
}
// QueryxContext using this NamedStmt
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) QueryxContext(ctx context.Context, arg any) (*Rows, error) {
r, err := n.QueryContext(ctx, arg)
if err != nil {
return nil, err
}
return &Rows{Rows: r, Mapper: n.Stmt.Mapper, options: n.Stmt.options}, err
}
// QueryRowxContext this NamedStmt. Because of limitations with QueryRow, this is
// an alias for QueryRow.
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) QueryRowxContext(ctx context.Context, arg any) *Row {
return n.QueryRowContext(ctx, arg)
}
// SelectContext using this NamedStmt
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) SelectContext(ctx context.Context, dest any, arg any) error {
rows, err := n.QueryxContext(ctx, arg)
if err != nil {
return err
}
// if something happens here, we want to make sure the rows are Closed
defer rows.Close()
return scanAll(rows, dest, false)
}
// GetContext using this NamedStmt
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) GetContext(ctx context.Context, dest any, arg any) error {
r := n.QueryRowxContext(ctx, arg)
return r.scanAny(dest, false)
}
// OneContext get a single row using this NamedStmt
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) OneContext(ctx context.Context, arg any) (T, error) {
r := n.QueryRowxContext(ctx, arg)
var dest T
err := r.scanAny(&dest, false)
return dest, err
}
// AllContext performs a query using the NamedStmt and returns all rows for use with range.
func (n *GenericNamedStmt[T]) AllContext(ctx context.Context, arg any) iter.Seq2[T, error] {
rows, err := n.QueryxContext(ctx, arg)
if err != nil {
panic(err)
}
return func(yield func(T, error) bool) {
defer func(rows *Rows) {
_ = rows.Close()
}(rows)
for rows.Next() {
if ctx.Err() != nil {
return
}
var dest T
err := rows.StructScan(&dest)
if !yield(dest, err) {
return
}
}
}
}
// NamedQueryContext binds a named query and then runs Query on the result using the
// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with
// map[string]interface{} types.
func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg any) (*Rows, error) {
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
if err != nil {
return nil, err
}
return e.QueryxContext(ctx, q, args...)
}
// NamedExecContext uses BindStruct to get a query executable by the driver and
// then runs Exec on the result. Returns an error from the binding
// or the query execution itself.
func NamedExecContext(ctx context.Context, e ExtContext, query string, arg any) (sql.Result, error) {
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
if err != nil {
return nil, err
}
return e.ExecContext(ctx, q, args...)
}