forked from doug-martin/goqu
/
query_executor.go
282 lines (244 loc) · 7.51 KB
/
query_executor.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package exec
import (
"context"
gsql "database/sql"
"reflect"
"github.com/GlebBeloded/goqu/v9/internal/errors"
"github.com/GlebBeloded/goqu/v9/internal/util"
)
type (
QueryExecutor struct {
de DbExecutor
err error
query string
args []interface{}
}
)
var (
errUnsupportedScanStructType = errors.New("type must be a pointer to a struct when scanning into a struct")
errUnsupportedScanStructsType = errors.New("type must be a pointer to a slice when scanning into structs")
errUnsupportedScanValsType = errors.New("type must be a pointer to a slice when scanning into vals")
errScanValPointer = errors.New("type must be a pointer when scanning into val")
errScanValNonSlice = errors.New("type cannot be a pointer to a slice when scanning into val")
)
func newQueryExecutor(de DbExecutor, err error, query string, args ...interface{}) QueryExecutor {
return QueryExecutor{de: de, err: err, query: query, args: args}
}
func (q QueryExecutor) ToSQL() (sql string, args []interface{}, err error) {
return q.query, q.args, q.err
}
func (q QueryExecutor) Exec() (gsql.Result, error) {
return q.ExecContext(context.Background())
}
func (q QueryExecutor) ExecContext(ctx context.Context) (gsql.Result, error) {
if q.err != nil {
return nil, q.err
}
return q.de.ExecContext(ctx, q.query, q.args...)
}
func (q QueryExecutor) Query() (*gsql.Rows, error) {
return q.QueryContext(context.Background())
}
func (q QueryExecutor) QueryContext(ctx context.Context) (*gsql.Rows, error) {
if q.err != nil {
return nil, q.err
}
return q.de.QueryContext(ctx, q.query, q.args...)
}
// This will execute the SQL and append results to the slice
// var myStructs []MyStruct
// if err := db.From("test").ScanStructs(&myStructs); err != nil{
// panic(err.Error()
// }
// //use your structs
//
//
// i: A pointer to a slice of structs.
func (q QueryExecutor) ScanStructs(i interface{}) error {
return q.ScanStructsContext(context.Background(), i)
}
// This will execute the SQL and append results to the slice
// var myStructs []MyStruct
// if err := db.From("test").ScanStructsContext(ctx, &myStructs); err != nil{
// panic(err.Error()
// }
// //use your structs
//
//
// i: A pointer to a slice of structs.
func (q QueryExecutor) ScanStructsContext(ctx context.Context, i interface{}) error {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return errUnsupportedScanStructsType
}
val = reflect.Indirect(val)
if !util.IsSlice(val.Kind()) {
return errUnsupportedScanStructsType
}
return q.scanIntoSlice(ctx, val, func(sc Scanner, r interface{}) error {
return sc.ScanStruct(r)
})
}
// This will execute the SQL and fill out the struct with the fields returned.
// This method returns a boolean value that is false if no record was found
// var myStruct MyStruct
// found, err := db.From("test").Limit(1).ScanStruct(&myStruct)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: A pointer to a struct
func (q QueryExecutor) ScanStruct(i interface{}) (bool, error) {
return q.ScanStructContext(context.Background(), i)
}
// This will execute the SQL and fill out the struct with the fields returned.
// This method returns a boolean value that is false if no record was found
// var myStruct MyStruct
// found, err := db.From("test").Limit(1).ScanStructContext(ctx, &myStruct)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: A pointer to a struct
func (q QueryExecutor) ScanStructContext(ctx context.Context, i interface{}) (bool, error) {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return false, errUnsupportedScanStructType
}
val = reflect.Indirect(val)
if !util.IsStruct(val.Kind()) {
return false, errUnsupportedScanStructType
}
scanner, err := q.ScannerContext(ctx)
if err != nil {
return false, err
}
defer scanner.Close()
if scanner.Next() {
err = scanner.ScanStruct(i)
if err != nil {
return false, err
}
return true, scanner.Err()
}
return false, scanner.Err()
}
// This will execute the SQL and append results to the slice.
// var ids []uint32
// if err := db.From("test").Select("id").ScanVals(&ids); err != nil{
// panic(err.Error()
// }
//
// i: Takes a pointer to a slice of primitive values.
func (q QueryExecutor) ScanVals(i interface{}) error {
return q.ScanValsContext(context.Background(), i)
}
// This will execute the SQL and append results to the slice.
// var ids []uint32
// if err := db.From("test").Select("id").ScanValsContext(ctx, &ids); err != nil{
// panic(err.Error()
// }
//
// i: Takes a pointer to a slice of primitive values.
func (q QueryExecutor) ScanValsContext(ctx context.Context, i interface{}) error {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return errUnsupportedScanValsType
}
val = reflect.Indirect(val)
if !util.IsSlice(val.Kind()) {
return errUnsupportedScanValsType
}
return q.scanIntoSlice(ctx, val, func(sc Scanner, r interface{}) error {
return sc.ScanVal(r)
})
}
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
// var id uint32
// found, err := db.From("test").Select("id").Limit(1).ScanVal(&id)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: Takes a pointer to a primitive value.
func (q QueryExecutor) ScanVal(i interface{}) (bool, error) {
return q.ScanValContext(context.Background(), i)
}
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
// var id uint32
// found, err := db.From("test").Select("id").Limit(1).ScanValContext(ctx, &id)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: Takes a pointer to a primitive value.
func (q QueryExecutor) ScanValContext(ctx context.Context, i interface{}) (bool, error) {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return false, errScanValPointer
}
val = reflect.Indirect(val)
if util.IsSlice(val.Kind()) {
switch i.(type) {
case *gsql.RawBytes: // do nothing
case *[]byte: // do nothing
case gsql.Scanner: // do nothing
default:
return false, errScanValNonSlice
}
}
scanner, err := q.ScannerContext(ctx)
if err != nil {
return false, err
}
defer scanner.Close()
if scanner.Next() {
err = scanner.ScanVal(i)
if err != nil {
return false, err
}
return true, scanner.Err()
}
return false, scanner.Err()
}
// Scanner will return a Scanner that can be used for manually scanning rows.
func (q QueryExecutor) Scanner() (Scanner, error) {
return q.ScannerContext(context.Background())
}
// ScannerContext will return a Scanner that can be used for manually scanning rows.
func (q QueryExecutor) ScannerContext(ctx context.Context) (Scanner, error) {
rows, err := q.QueryContext(ctx)
if err != nil {
return nil, err
}
return NewScanner(rows), nil
}
func (q QueryExecutor) scanIntoSlice(ctx context.Context, val reflect.Value, it func(sc Scanner, i interface{}) error) error {
elemType := util.GetSliceElementType(val)
scanner, err := q.ScannerContext(ctx)
if err != nil {
return err
}
defer scanner.Close()
for scanner.Next() {
row := reflect.New(elemType)
err = it(scanner, row.Interface())
if err != nil {
return err
}
util.AppendSliceElement(val, row)
}
return scanner.Err()
}