-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdml_select_rows_binder.go
371 lines (303 loc) · 13.9 KB
/
dml_select_rows_binder.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2025 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlx
import (
"fmt"
"reflect"
"time"
)
// RowsBinder is an interface to bind the rows to dst that may be a map or slice.
type RowsBinder interface {
BindRows(scanner RowScanner, dst any) error
}
// RowsBinderFunc is a function to bind the rows to dst that may be a map or slice.
type RowsBinderFunc func(scanner RowScanner, dst any) (err error)
// BindRows implements the interface RowsBinder.
func (f RowsBinderFunc) BindRows(scanner RowScanner, dst any) error { return f(scanner, dst) }
var (
// DefaultRowsCap is the default capacity to allocate a map or slice for scanned rows.
DefaultRowsCap = 16
// DefaultSliceCap is the default mixed rows binder to bind the rows to a map or slice.
//
// It has registered some rows binders for specific-types, such as
// []struct
// []int, []int64, []string
// map[string]int, map[string]string
// map[string]bool, map[string]struct{}
DefaultMixRowsBinder = NewMixRowsBinder()
// CommonSliceRowsBinder is the common rows binder to bind the rows to a slice.
//
// Notice: it uses the reflect package for the default implementation.
CommonSliceRowsBinder RowsBinder = RowsBinderFunc(commonSliceRowsBinder)
)
// MixRowsBinder is a mixed rows binder based on the reflected type.
type MixRowsBinder struct {
types map[reflect.Type]RowsBinder
}
// NewMixRowsBinder returns a new MixRowsBinder.
func NewMixRowsBinder() *MixRowsBinder {
return &MixRowsBinder{types: make(map[reflect.Type]RowsBinder, 64)}
}
// Register registers a rows binder for a specific type.
func (b *MixRowsBinder) Register(vtype reflect.Type, binder RowsBinder) (old RowsBinder) {
if binder == nil {
panic("sqlx.MixRowsBinder: binder-typed must not be nil")
}
old = b.types[vtype]
b.types[vtype] = binder
return
}
// BindRows implements the interface RowsBinder.
func (b *MixRowsBinder) BindRows(scanner RowScanner, dst any) (err error) {
vtype := reflect.TypeOf(dst)
if binder, ok := b.types[vtype]; ok {
return binder.BindRows(scanner, dst)
}
if vtype.Kind() == reflect.Pointer && vtype.Elem().Kind() == reflect.Slice {
return CommonSliceRowsBinder.BindRows(scanner, dst)
}
return fmt.Errorf("sqlx.MixRowsBinder.BindRows: unsupport the type %s for rows binder", vtype)
}
func init() {
// []int, []uint, []int32, []uint32, []int64, []uint64, []string, []time.Time
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]int](), NewSliceRowsBinder[[]int]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]uint](), NewSliceRowsBinder[[]uint]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]int32](), NewSliceRowsBinder[[]int32]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]uint32](), NewSliceRowsBinder[[]uint32]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]int64](), NewSliceRowsBinder[[]int64]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]uint64](), NewSliceRowsBinder[[]uint64]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]string](), NewSliceRowsBinder[[]string]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*[]time.Time](), NewSliceRowsBinder[[]time.Time]())
/// ------------------------------------- map[K]bool -------------------------------------- ///
// map[int]bool
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]bool](), NewMapRowsBinderForKey[map[int]bool](fixedvaluebooltrue[int]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]bool](), NewMapRowsBinderForKey[map[int]bool](fixedvaluebooltrue[int]))
// map[int32]bool
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int32]bool](), NewMapRowsBinderForKey[map[int32]bool](fixedvaluebooltrue[int32]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int32]bool](), NewMapRowsBinderForKey[map[int32]bool](fixedvaluebooltrue[int32]))
// map[int64]bool
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]bool](), NewMapRowsBinderForKey[map[int64]bool](fixedvaluebooltrue[int64]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]bool](), NewMapRowsBinderForKey[map[int64]bool](fixedvaluebooltrue[int64]))
// map[string]bool
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]bool](), NewMapRowsBinderForKey[map[string]bool](fixedvaluebooltrue[string]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]bool](), NewMapRowsBinderForKey[map[string]bool](fixedvaluebooltrue[string]))
/// ----------------------------------- map[K]struct{} ------------------------------------ ///
// map[int]struct{}
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]struct{}](), NewMapRowsBinderForKey[map[int]struct{}](fixedvaluestructempty[int]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]struct{}](), NewMapRowsBinderForKey[map[int]struct{}](fixedvaluestructempty[int]))
// map[int32]struct{}
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int32]struct{}](), NewMapRowsBinderForKey[map[int32]struct{}](fixedvaluestructempty[int32]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int32]struct{}](), NewMapRowsBinderForKey[map[int32]struct{}](fixedvaluestructempty[int32]))
// map[int64]struct{}
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]struct{}](), NewMapRowsBinderForKey[map[int64]struct{}](fixedvaluestructempty[int64]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]struct{}](), NewMapRowsBinderForKey[map[int64]struct{}](fixedvaluestructempty[int64]))
// map[string]struct{}
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]struct{}](), NewMapRowsBinderForKey[map[string]struct{}](fixedvaluestructempty[string]))
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]struct{}](), NewMapRowsBinderForKey[map[string]struct{}](fixedvaluestructempty[string]))
/// -------------------------------------- map[int]V -------------------------------------- ///
// map[int]int
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]int](), NewMapRowsBinderForKeyValue[map[int]int]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]int](), NewMapRowsBinderForKeyValue[map[int]int]())
// map[int]int32
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]int32](), NewMapRowsBinderForKeyValue[map[int]int32]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]int32](), NewMapRowsBinderForKeyValue[map[int]int32]())
// map[int]int64
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]int64](), NewMapRowsBinderForKeyValue[map[int]int64]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]int64](), NewMapRowsBinderForKeyValue[map[int]int64]())
// map[int]string
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int]string](), NewMapRowsBinderForKeyValue[map[int]string]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int]string](), NewMapRowsBinderForKeyValue[map[int]string]())
/// ------------------------------------- map[int64]V ------------------------------------- ///
// map[int64]int
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]int](), NewMapRowsBinderForKeyValue[map[int64]int]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]int](), NewMapRowsBinderForKeyValue[map[int64]int]())
// map[int64]int32
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]int32](), NewMapRowsBinderForKeyValue[map[int64]int32]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]int32](), NewMapRowsBinderForKeyValue[map[int64]int32]())
// map[int64]int64
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]int64](), NewMapRowsBinderForKeyValue[map[int64]int64]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]int64](), NewMapRowsBinderForKeyValue[map[int64]int64]())
// map[int64]string
DefaultMixRowsBinder.Register(reflect.TypeFor[map[int64]string](), NewMapRowsBinderForKeyValue[map[int64]string]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[int64]string](), NewMapRowsBinderForKeyValue[map[int64]string]())
/// ------------------------------------ map[string]V ------------------------------------- ///
// map[string]int
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]int](), NewMapRowsBinderForKeyValue[map[string]int]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]int](), NewMapRowsBinderForKeyValue[map[string]int]())
// map[string]int32
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]int32](), NewMapRowsBinderForKeyValue[map[string]int32]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]int32](), NewMapRowsBinderForKeyValue[map[string]int32]())
// map[string]int64
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]int64](), NewMapRowsBinderForKeyValue[map[string]int64]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]int64](), NewMapRowsBinderForKeyValue[map[string]int64]())
// map[string]string
DefaultMixRowsBinder.Register(reflect.TypeFor[map[string]string](), NewMapRowsBinderForKeyValue[map[string]string]())
DefaultMixRowsBinder.Register(reflect.TypeFor[*map[string]string](), NewMapRowsBinderForKeyValue[map[string]string]())
}
func fixedvaluebooltrue[K comparable](K) bool { return true }
func fixedvaluestructempty[K comparable](K) struct{} { return struct{}{} }
// NewMapRowsBinderForKeyAndFixedValue is short for NewMapRowsBinderForKey, which is equal to
//
// NewMapRowsBinderForKey[M](func(K) V { return value })
func NewMapRowsBinderForKeyAndFixedValue[M ~map[K]V, K comparable, V any](value V) RowsBinder {
return NewMapRowsBinderForKey[M](func(K) V { return value })
}
// NewMapRowsBinderForKey returns a rows binder which binds the rows as the map keys
// and extracts the map values from the keys.
func NewMapRowsBinderForKey[M ~map[K]V, K comparable, V any](valuef func(K) V) RowsBinder {
return RowsBinderFunc(func(scanner RowScanner, dst any) (err error) {
var m M
switch v := dst.(type) {
case M:
if v == nil {
panic("sqlx. NewMapRowsBinderForKey: map value must not be nil")
}
m = v
case *M:
if *v == nil {
*v = make(M, getrowscap(scanner, DefaultRowsCap))
}
m = *v
default:
panic(fmt.Errorf("sqlx. NewMapRowsBinderForKey: unsupport the type %T", dst))
}
for scanner.Next() {
var key K
if err = scanner.Scan(&key); err != nil {
return
}
m[key] = valuef(key)
}
return
})
}
// NewMapRowsBinderForValue returns a rows binder which binds the rows as the map values
// and extracts the map keys from the values.
func NewMapRowsBinderForValue[M ~map[K]V, K comparable, V any](keyf func(V) K) RowsBinder {
return RowsBinderFunc(func(scanner RowScanner, dst any) (err error) {
var m M
switch v := dst.(type) {
case M:
if v == nil {
panic("sqlx.NewMapRowsBinderForValue: map value must not be nil")
}
m = v
case *M:
if *v == nil {
*v = make(M, getrowscap(scanner, DefaultRowsCap))
}
m = *v
default:
panic(fmt.Errorf("sqlx.NewMapRowsBinderForValue: unsupport the type %T", dst))
}
for scanner.Next() {
var value V
if err = scanner.Scan(&value); err != nil {
return
}
m[keyf(value)] = value
}
return
})
}
// NewMapRowsBinderForKeyValue returns a rows binder which binds the rows as the map keys and values.
//
// Notice: each row must have two columns as key and value from front to back.
func NewMapRowsBinderForKeyValue[M ~map[K]V, K comparable, V any]() RowsBinder {
return RowsBinderFunc(func(scanner RowScanner, dst any) (err error) {
var m M
switch v := dst.(type) {
case M:
if v == nil {
panic("sqlx. NewMapRowsBinderForKeyValue: map value must not be nil")
}
m = v
case *M:
if *v == nil {
*v = make(M, getrowscap(scanner, DefaultRowsCap))
}
m = *v
default:
panic(fmt.Errorf("sqlx. NewMapRowsBinderForKeyValue: unsupport the type %T", dst))
}
for scanner.Next() {
var key K
var value V
if err = scanner.Scan(&key, &value); err != nil {
return
}
m[key] = value
}
return
})
}
// NewSliceRowsBinder returns a rows binder which binds the rows as the slice.
//
// It does not use the reflect package.
func NewSliceRowsBinder[S ~[]T, T any]() RowsBinder {
return RowsBinderFunc(func(scanner RowScanner, dst any) (err error) {
dstps, ok := dst.(*[]T)
if !ok {
panic(fmt.Errorf("sqlx. NewSliceRowsBinder: expect type %T, but got %T", (*S)(nil), dst))
}
dsts := *dstps
if cap(dsts) == 0 {
dsts = make(S, 0, getrowscap(scanner, DefaultRowsCap))
}
for scanner.Next() {
var value T
if err := scanner.Scan(&value); err != nil {
return err
}
dsts = append(dsts, value)
}
*dstps = dsts
return
})
}
func commonSliceRowsBinder(scanner RowScanner, dst any) (err error) {
oldvf := reflect.ValueOf(dst)
if oldvf.Kind() != reflect.Pointer {
panic("sqlx.CommonSliceRowsBinder: the value must be a pointer to a slice")
}
vf := oldvf.Elem()
if vf.Kind() != reflect.Slice {
panic("sqlx.CommonSliceRowsBinder: the value must be a pointer to a slice")
}
vt := vf.Type()
if vf.Cap() == 0 {
vf.Set(reflect.MakeSlice(vt, 0, getrowscap(scanner, DefaultRowsCap)))
}
et := vt.Elem()
for scanner.Next() {
e := reflect.New(et)
if err := scanner.Scan(e.Interface()); err != nil {
return err
}
vf = reflect.Append(vf, e.Elem())
}
oldvf.Elem().Set(vf)
return
}
// NewDegradedSliceRowsBinder returns a rows binder which prefers to try to
// bind *S to the rows, or use the degraded rows binder to bind the rows.
func NewDegradedSliceRowsBinder[S ~[]T, T any](degraded RowsBinder) RowsBinder {
binder := NewSliceRowsBinder[S]()
return RowsBinderFunc(func(scanner RowScanner, dst any) error {
if dstps, ok := dst.(*S); ok {
return binder.BindRows(scanner, dstps)
}
return degraded.BindRows(scanner, dst)
})
}