-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
model_slice.go
82 lines (66 loc) · 1.39 KB
/
model_slice.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
package bun
import (
"context"
"database/sql"
"reflect"
"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/schema"
)
type sliceInfo struct {
nextElem func() reflect.Value
scan schema.ScannerFunc
}
type sliceModel struct {
dest []interface{}
values []reflect.Value
scanIndex int
info []sliceInfo
}
var _ Model = (*sliceModel)(nil)
func newSliceModel(db *DB, dest []interface{}, values []reflect.Value) *sliceModel {
return &sliceModel{
dest: dest,
values: values,
}
}
func (m *sliceModel) Value() interface{} {
return m.dest
}
func (m *sliceModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
columns, err := rows.Columns()
if err != nil {
return 0, err
}
m.info = make([]sliceInfo, len(m.values))
for i, v := range m.values {
if v.IsValid() && v.Len() > 0 {
v.Set(v.Slice(0, 0))
}
m.info[i] = sliceInfo{
nextElem: internal.MakeSliceNextElemFunc(v),
scan: schema.Scanner(v.Type().Elem()),
}
}
if len(columns) == 0 {
return 0, nil
}
dest := makeDest(m, len(columns))
var n int
for rows.Next() {
m.scanIndex = 0
if err := rows.Scan(dest...); err != nil {
return 0, err
}
n++
}
if err := rows.Err(); err != nil {
return 0, err
}
return n, nil
}
func (m *sliceModel) Scan(src interface{}) error {
info := m.info[m.scanIndex]
m.scanIndex++
dest := info.nextElem()
return info.scan(dest, src)
}