-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloading.go
166 lines (154 loc) · 4.17 KB
/
loading.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
package dali
import (
"database/sql"
"fmt"
"reflect"
)
// One executes the query that should return rows and loads the
// resulting data from the first row into dest which must be a struct.
// Only fields that match the column names (after filtering through
// the mapperFunc) are filled. One returns sql.ErrNoRows if there are
// no rows.
func (q *Query) One(dest interface{}) error {
destv := reflect.ValueOf(dest)
if destv.Kind() != reflect.Ptr {
panic("dali: dest must be a pointer to a struct")
}
v := reflect.Indirect(destv)
if v.Kind() != reflect.Struct {
panic("dali: dest must be a pointer to a struct")
}
return q.loadStruct(v)
}
// All executes the query that should return rows, and loads the
// resulting data into dest which must be a slice of structs.
// Only fields that match the column names (after filtering through
// the mapperFunc) are filled.
func (q *Query) All(dest interface{}) error {
const errMsg = "dali: dest must be a pointer to a slice of structs or pointers to structs"
destv := reflect.ValueOf(dest)
if destv.Kind() != reflect.Ptr {
panic(errMsg)
}
slicev := reflect.Indirect(destv)
if slicev.Kind() != reflect.Slice {
panic(errMsg)
}
elemt := slicev.Type().Elem()
isPtr := false
if isPtr = elemt.Kind() == reflect.Ptr; isPtr {
elemt = elemt.Elem()
}
switch elemt.Kind() {
case reflect.Ptr:
panic("dali: a pointer to a pointer is not allowed as an element of dest")
case reflect.Struct:
return q.loadStructs(slicev, elemt, isPtr)
}
panic(errMsg)
}
func (q *Query) loadStruct(v reflect.Value) error { return q.load(v, v.Type(), true, false) }
func (q *Query) loadStructs(slicev reflect.Value, elemt reflect.Type, isPtr bool) error {
return q.load(slicev, elemt, false, isPtr)
}
func (q *Query) load(v reflect.Value, elemt reflect.Type, loadJustOne, isPtr bool) error {
rows, err := q.Rows()
if err != nil {
return err
}
defer rows.Close()
rowCols, err := rows.Columns()
if err != nil {
return err
}
cols, indexes := colNamesAndFieldIndexes(elemt, false)
fieldIndexes := make([][]int, len(rowCols))
for coln, rowCol := range rowCols {
var index []int
for i, col := range cols {
if rowCol == col {
index = indexes[i]
break
}
}
fieldIndexes[coln] = index
}
fields := make([]interface{}, len(fieldIndexes))
err = nil
if loadJustOne {
err = sql.ErrNoRows
}
for rows.Next() {
elemvptr := reflect.New(elemt)
elemv := reflect.Indirect(elemvptr)
noMatch := true
for i, index := range fieldIndexes {
if index == nil {
fields[i] = new(interface{})
continue
}
noMatch = false
fields[i] = elemv.FieldByIndex(index).Addr().Interface()
}
if noMatch {
return fmt.Errorf("dali: no match between columns and struct fields")
}
if err := rows.Scan(fields...); err != nil {
return err
}
if loadJustOne {
// v is a struct.
v.Set(elemv)
err = nil
break
}
// Otherwise, v must be a slice.
if isPtr {
v.Set(reflect.Append(v, elemvptr))
} else {
v.Set(reflect.Append(v, elemv))
}
}
if err := rows.Err(); err != nil {
return err
}
return err
}
// ScanAllRows executes the query that is expected to return rows.
// It copies the columns from the matched rows into the slices
// pointed at by dests.
func (q *Query) ScanAllRows(dests ...interface{}) error {
slicevals := make([]reflect.Value, len(dests))
elemtypes := make([]reflect.Type, len(dests))
for i, dests := range dests {
destv := reflect.ValueOf(dests)
if destv.Kind() != reflect.Ptr {
panic("dali: dests must be a pointer to a slice")
}
slicevals[i] = reflect.Indirect(destv)
if slicevals[i].Kind() != reflect.Slice {
panic("dali: dests must be a pointer to a slice")
}
elemtypes[i] = slicevals[i].Type().Elem()
}
rows, err := q.Rows()
if err != nil {
return err
}
defer rows.Close()
elemvptrs := make([]reflect.Value, len(dests))
args := make([]interface{}, len(dests))
for rows.Next() {
for i := range args {
elemvptrs[i] = reflect.New(elemtypes[i])
args[i] = elemvptrs[i].Interface()
}
if err := rows.Scan(args...); err != nil {
return err
}
for i := range args {
slicevals[i].Set(reflect.Append(slicevals[i], reflect.Indirect(elemvptrs[i])))
}
}
return rows.Err()
}