forked from stephenafamo/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
199 lines (160 loc) · 4.2 KB
/
stmt.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
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
195
196
197
198
199
package bob
import (
"context"
"database/sql"
"github.com/stephenafamo/scan"
)
type Preparer interface {
Executor
PrepareContext(ctx context.Context, query string) (Statement, error)
}
type Statement interface {
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, args ...any) (scan.Rows, error)
}
// NewStmt wraps an [*sql.Stmt] and returns a type that implements [Queryer] but still
// retains the expected methods used by *sql.Stmt
// This is useful when an existing *sql.Stmt is used in other places in the codebase
func Prepare(ctx context.Context, exec Preparer, q Query) (Stmt, error) {
query, args, err := Build(q)
if err != nil {
return Stmt{}, err
}
stmt, err := exec.PrepareContext(ctx, query)
if err != nil {
return Stmt{}, err
}
s := Stmt{
exec: exec,
stmt: stmt,
lenArgs: len(args),
}
if l, ok := q.(Loadable); ok {
loaders := l.GetLoaders()
s.loaders = make([]Loader, len(loaders))
copy(s.loaders, loaders)
}
return s, nil
}
// Stmt is similar to *sql.Stmt but implements [Queryer]
type Stmt struct {
stmt Statement
exec Executor
lenArgs int
loaders []Loader
}
// Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (s Stmt) Exec(ctx context.Context, args ...any) (sql.Result, error) {
result, err := s.stmt.ExecContext(ctx, args...)
if err != nil {
return nil, err
}
for _, loader := range s.loaders {
if err := loader.Load(ctx, s.exec, nil); err != nil {
return nil, err
}
}
return result, nil
}
func PrepareQuery[T any](ctx context.Context, exec Preparer, q Query, m scan.Mapper[T], opts ...ExecOption[T]) (QueryStmt[T, []T], error) {
return PrepareQueryx[T, []T](ctx, exec, q, m, opts...)
}
func PrepareQueryx[T any, Ts ~[]T](ctx context.Context, exec Preparer, q Query, m scan.Mapper[T], opts ...ExecOption[T]) (QueryStmt[T, Ts], error) {
var qs QueryStmt[T, Ts]
s, err := Prepare(ctx, exec, q)
if err != nil {
return qs, err
}
settings := ExecSettings[T]{}
for _, opt := range opts {
opt(&settings)
}
if l, ok := q.(MapperModder); ok {
if loaders := l.GetMapperMods(); len(loaders) > 0 {
m = scan.Mod(m, loaders...)
}
}
qs = QueryStmt[T, Ts]{
Stmt: s,
mapper: m,
settings: settings,
}
return qs, nil
}
type QueryStmt[T any, Ts ~[]T] struct {
Stmt
mapper scan.Mapper[T]
settings ExecSettings[T]
}
func (s QueryStmt[T, Ts]) One(ctx context.Context, args ...any) (T, error) {
var t T
rows, err := s.stmt.QueryContext(ctx, args...)
if err != nil {
return t, err
}
t, err = scan.OneFromRows(ctx, s.mapper, rows)
if err != nil {
return t, err
}
for _, loader := range s.loaders {
if err := loader.Load(ctx, s.exec, t); err != nil {
return t, err
}
}
if s.settings.AfterSelect != nil {
if err := s.settings.AfterSelect(ctx, []T{t}); err != nil {
return t, err
}
}
return t, err
}
func (s QueryStmt[T, Ts]) All(ctx context.Context, args ...any) (Ts, error) {
rows, err := s.stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
rawSlice, err := scan.AllFromRows(ctx, s.mapper, rows)
if err != nil {
return nil, err
}
typedSlice := Ts(rawSlice)
for _, loader := range s.loaders {
if err := loader.Load(ctx, s.exec, typedSlice); err != nil {
return nil, err
}
}
if s.settings.AfterSelect != nil {
if err := s.settings.AfterSelect(ctx, typedSlice); err != nil {
return nil, err
}
}
return typedSlice, err
}
func (s QueryStmt[T, Ts]) Cursor(ctx context.Context, args ...any) (scan.ICursor[T], error) {
rows, err := s.stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
m2 := scan.Mapper[T](func(ctx context.Context, c []string) (scan.BeforeFunc, func(any) (T, error)) {
before, after := s.mapper(ctx, c)
return before, func(link any) (T, error) {
t, err := after(link)
if err != nil {
return t, err
}
for _, loader := range s.loaders {
err = loader.Load(ctx, s.exec, t)
if err != nil {
return t, err
}
}
if s.settings.AfterSelect != nil {
if err := s.settings.AfterSelect(ctx, []T{t}); err != nil {
return t, err
}
}
return t, err
}
})
return scan.CursorFromRows(ctx, m2, rows)
}