-
Notifications
You must be signed in to change notification settings - Fork 92
/
rows.go
256 lines (215 loc) · 6.28 KB
/
rows.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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package dbx
import (
"database/sql"
"reflect"
)
// VarTypeError indicates a variable type error when trying to populating a variable with DB result.
type VarTypeError string
// Error returns the error message.
func (s VarTypeError) Error() string {
return "Invalid variable type: " + string(s)
}
// NullStringMap is a map of sql.NullString that can be used to hold DB query result.
// The map keys correspond to the DB column names, while the map values are their corresponding column values.
type NullStringMap map[string]sql.NullString
// Rows enhances sql.Rows by providing additional data query methods.
// Rows can be obtained by calling Query.Rows(). It is mainly used to populate data row by row.
type Rows struct {
*sql.Rows
fieldMapFunc FieldMapFunc
}
// ScanMap populates the current row of data into a NullStringMap.
// Note that the NullStringMap must not be nil, or it will panic.
// The NullStringMap will be populated using column names as keys and their values as
// the corresponding element values.
func (r *Rows) ScanMap(a NullStringMap) error {
cols, _ := r.Columns()
var refs []interface{}
for i := 0; i < len(cols); i++ {
var t sql.NullString
refs = append(refs, &t)
}
if err := r.Scan(refs...); err != nil {
return err
}
for i, col := range cols {
a[col] = *refs[i].(*sql.NullString)
}
return nil
}
// ScanStruct populates the current row of data into a struct.
// The struct must be given as a pointer.
//
// ScanStruct associates struct fields with DB table columns through a field mapping function.
// It populates a struct field with the data of its associated column.
// Note that only exported struct fields will be populated.
//
// By default, DefaultFieldMapFunc() is used to map struct fields to table columns.
// This function separates each word in a field name with a underscore and turns every letter into lower case.
// For example, "LastName" is mapped to "last_name", "MyID" is mapped to "my_id", and so on.
// To change the default behavior, set DB.FieldMapper with your custom mapping function.
// You may also set Query.FieldMapper to change the behavior for particular queries.
func (r *Rows) ScanStruct(a interface{}) error {
rv := reflect.ValueOf(a)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return VarTypeError("must be a pointer")
}
rv = indirect(rv)
if rv.Kind() != reflect.Struct {
return VarTypeError("must be a pointer to a struct")
}
si := getStructInfo(rv.Type(), r.fieldMapFunc)
cols, _ := r.Columns()
refs := make([]interface{}, len(cols))
for i, col := range cols {
if fi, ok := si.dbNameMap[col]; ok {
refs[i] = fi.getField(rv).Addr().Interface()
} else {
refs[i] = &sql.NullString{}
}
}
return r.Scan(refs...)
}
// all populates all rows of query result into a slice of struct or NullStringMap.
// Note that the slice must be given as a pointer.
func (r *Rows) all(slice interface{}) error {
defer r.Close()
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Ptr || v.IsNil() {
return VarTypeError("must be a pointer")
}
v = indirect(v)
if v.Kind() != reflect.Slice {
return VarTypeError("must be a slice of struct or NullStringMap")
}
if v.IsNil() {
// create an empty slice
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
}
et := v.Type().Elem()
if et.Kind() == reflect.Map {
for r.Next() {
ev, ok := reflect.MakeMap(et).Interface().(NullStringMap)
if !ok {
return VarTypeError("must be a slice of struct or NullStringMap")
}
if err := r.ScanMap(ev); err != nil {
return err
}
v.Set(reflect.Append(v, reflect.ValueOf(ev)))
}
return r.Close()
}
if et.Kind() != reflect.Struct {
return VarTypeError("must be a slice of struct or NullStringMap")
}
si := getStructInfo(et, r.fieldMapFunc)
cols, _ := r.Columns()
for r.Next() {
ev := reflect.New(et).Elem()
refs := make([]interface{}, len(cols))
for i, col := range cols {
if fi, ok := si.dbNameMap[col]; ok {
refs[i] = fi.getField(ev).Addr().Interface()
} else {
refs[i] = &sql.NullString{}
}
}
if err := r.Scan(refs...); err != nil {
return err
}
v.Set(reflect.Append(v, ev))
}
return r.Close()
}
// column populates the given slice with the first column of the query result.
// Note that the slice must be given as a pointer.
func (r *Rows) column(slice interface{}) error {
defer r.Close()
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Ptr || v.IsNil() {
return VarTypeError("must be a pointer to a slice")
}
v = indirect(v)
if v.Kind() != reflect.Slice {
return VarTypeError("must be a pointer to a slice")
}
et := v.Type().Elem()
cols, _ := r.Columns()
for r.Next() {
ev := reflect.New(et)
refs := make([]interface{}, len(cols))
for i := range cols {
if i == 0 {
refs[i] = ev.Interface()
} else {
refs[i] = &sql.NullString{}
}
}
if err := r.Scan(refs...); err != nil {
return err
}
v.Set(reflect.Append(v, ev.Elem()))
}
return r.Close()
}
// one populates a single row of query result into a struct or a NullStringMap.
// Note that if a struct is given, it should be a pointer.
func (r *Rows) one(a interface{}) error {
defer r.Close()
if !r.Next() {
if err := r.Err(); err != nil {
return err
}
return sql.ErrNoRows
}
var err error
rt := reflect.TypeOf(a)
if rt.Kind() == reflect.Ptr && rt.Elem().Kind() == reflect.Map {
// pointer to map
v := indirect(reflect.ValueOf(a))
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
a = v.Interface()
rt = reflect.TypeOf(a)
}
if rt.Kind() == reflect.Map {
v, ok := a.(NullStringMap)
if !ok {
return VarTypeError("must be a NullStringMap")
}
if v == nil {
return VarTypeError("NullStringMap is nil")
}
err = r.ScanMap(v)
} else {
err = r.ScanStruct(a)
}
if err != nil {
return err
}
return r.Close()
}
// row populates a single row of query result into a list of variables.
func (r *Rows) row(a ...interface{}) error {
defer r.Close()
for _, dp := range a {
if _, ok := dp.(*sql.RawBytes); ok {
return VarTypeError("RawBytes isn't allowed on Row()")
}
}
if !r.Next() {
if err := r.Err(); err != nil {
return err
}
return sql.ErrNoRows
}
if err := r.Scan(a...); err != nil {
return err
}
return r.Close()
}