forked from stephenafamo/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
186 lines (151 loc) · 3.9 KB
/
exec.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
package bob
import (
"context"
"database/sql"
"github.com/stephenafamo/scan"
)
type (
MapperModder interface {
GetMapperMods() []scan.MapperMod
}
ExecSettings[T any] struct {
AfterSelect func(ctx context.Context, retrieved []T) error
}
ExecOption[T any] func(*ExecSettings[T])
)
type Executor interface {
scan.Queryer
ExecContext(context.Context, string, ...any) (sql.Result, error)
}
func Exec(ctx context.Context, exec Executor, q Query) (sql.Result, error) {
sql, args, err := Build(q)
if err != nil {
return nil, err
}
result, err := exec.ExecContext(ctx, sql, args...)
if err != nil {
return nil, err
}
if l, ok := q.(Loadable); ok {
for _, loader := range l.GetLoaders() {
if err := loader.Load(ctx, exec, nil); err != nil {
return nil, err
}
}
}
return result, nil
}
func One[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T], opts ...ExecOption[T]) (T, error) {
settings := ExecSettings[T]{}
for _, opt := range opts {
opt(&settings)
}
var t T
sql, args, err := Build(q)
if err != nil {
return t, err
}
if l, ok := q.(MapperModder); ok {
if loaders := l.GetMapperMods(); len(loaders) > 0 {
m = scan.Mod(m, loaders...)
}
}
t, err = scan.One(ctx, exec, m, sql, args...)
if err != nil {
return t, err
}
if l, ok := q.(Loadable); ok {
for _, loader := range l.GetLoaders() {
if err := loader.Load(ctx, exec, t); err != nil {
return t, err
}
}
}
if settings.AfterSelect != nil {
if err := settings.AfterSelect(ctx, []T{t}); err != nil {
return t, err
}
}
return t, err
}
func All[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T], opts ...ExecOption[T]) ([]T, error) {
return Allx[T, []T](ctx, exec, q, m, opts...)
}
// Allx takes 2 type parameters. The second is a special return type of the returned slice
// this is especially useful for when the the [Query] is [Loadable] and the loader depends on the
// return value implementing an interface
func Allx[T any, Ts ~[]T](ctx context.Context, exec Executor, q Query, m scan.Mapper[T], opts ...ExecOption[T]) (Ts, error) {
settings := ExecSettings[T]{}
for _, opt := range opts {
opt(&settings)
}
sql, args, err := Build(q)
if err != nil {
return nil, err
}
if l, ok := q.(MapperModder); ok {
if loaders := l.GetMapperMods(); len(loaders) > 0 {
m = scan.Mod(m, loaders...)
}
}
rawSlice, err := scan.All(ctx, exec, m, sql, args...)
if err != nil {
return nil, err
}
typedSlice := Ts(rawSlice)
if l, ok := q.(Loadable); ok {
for _, loader := range l.GetLoaders() {
if err := loader.Load(ctx, exec, typedSlice); err != nil {
return typedSlice, err
}
}
}
if settings.AfterSelect != nil {
if err := settings.AfterSelect(ctx, typedSlice); err != nil {
return typedSlice, err
}
}
return typedSlice, nil
}
// Cursor returns a cursor that works similar to *sql.Rows
func Cursor[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T], opts ...ExecOption[T]) (scan.ICursor[T], error) {
settings := ExecSettings[T]{}
for _, opt := range opts {
opt(&settings)
}
sql, args, err := Build(q)
if err != nil {
return nil, err
}
if l, ok := q.(MapperModder); ok {
if loaders := l.GetMapperMods(); len(loaders) > 0 {
m = scan.Mod(m, loaders...)
}
}
l, ok := q.(Loadable)
if !ok {
return scan.Cursor(ctx, exec, m, sql, args...)
}
m2 := scan.Mapper[T](func(ctx context.Context, c []string) (scan.BeforeFunc, func(any) (T, error)) {
before, after := m(ctx, c)
return before, func(link any) (T, error) {
t, err := after(link)
if err != nil {
return t, err
}
for _, loader := range l.GetLoaders() {
err = loader.Load(ctx, exec, t)
if err != nil {
return t, err
}
}
if settings.AfterSelect != nil {
if err := settings.AfterSelect(ctx, []T{t}); err != nil {
return t, err
}
}
return t, err
}
})
return scan.Cursor(ctx, exec, m2, sql, args...)
}