/
fetch.go
240 lines (189 loc) · 5.3 KB
/
fetch.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
// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package sqlbuilder
import (
"reflect"
db "github.com/upper/db/v4"
"github.com/upper/db/v4/internal/reflectx"
)
type hasConvertValues interface {
ConvertValues(values []interface{}) []interface{}
}
type hasReflect interface {
Reflect(db.Model)
}
var Mapper = reflectx.NewMapper("db")
// fetchRow receives a *sql.Rows value and tries to map all the rows into a
// single struct given by the pointer `dst`.
func fetchRow(iter *iterator, dst interface{}) error {
var columns []string
var err error
rows := iter.cursor
dstv := reflect.ValueOf(dst)
if dstv.IsNil() || dstv.Kind() != reflect.Ptr {
return ErrExpectingPointer
}
itemV := dstv.Elem()
if columns, err = rows.Columns(); err != nil {
return err
}
reset(dst)
next := rows.Next()
if !next {
if err = rows.Err(); err != nil {
return err
}
return db.ErrNoMoreRows
}
itemT := itemV.Type()
item, err := fetchResult(iter, itemT, columns)
if err != nil {
return err
}
if itemT.Kind() == reflect.Ptr {
itemV.Set(item)
} else {
itemV.Set(reflect.Indirect(item))
}
if reflector, ok := dst.(hasReflect); ok {
reflector.Reflect(dst.(db.Model))
}
return nil
}
// fetchRows receives a *sql.Rows value and tries to map all the rows into a
// slice of structs given by the pointer `dst`.
func fetchRows(iter *iterator, dst interface{}) error {
var err error
rows := iter.cursor
defer rows.Close()
// Destination.
dstv := reflect.ValueOf(dst)
if dstv.IsNil() || dstv.Kind() != reflect.Ptr {
return ErrExpectingPointer
}
if dstv.Elem().Kind() != reflect.Slice {
return ErrExpectingSlicePointer
}
if dstv.Kind() != reflect.Ptr || dstv.Elem().Kind() != reflect.Slice || dstv.IsNil() {
return ErrExpectingSliceMapStruct
}
var columns []string
if columns, err = rows.Columns(); err != nil {
return err
}
slicev := dstv.Elem()
itemT := slicev.Type().Elem()
reset(dst)
for rows.Next() {
item, err := fetchResult(iter, itemT, columns)
if err != nil {
return err
}
if itemT.Kind() == reflect.Ptr {
slicev = reflect.Append(slicev, item)
} else {
slicev = reflect.Append(slicev, reflect.Indirect(item))
}
}
dstv.Elem().Set(slicev)
return rows.Err()
}
func fetchResult(iter *iterator, itemT reflect.Type, columns []string) (reflect.Value, error) {
var item reflect.Value
var err error
rows := iter.cursor
objT := itemT
switch objT.Kind() {
case reflect.Map:
item = reflect.MakeMap(objT)
case reflect.Struct:
item = reflect.New(objT)
case reflect.Ptr:
objT = itemT.Elem()
if objT.Kind() != reflect.Struct {
return item, ErrExpectingMapOrStruct
}
item = reflect.New(objT)
default:
return item, ErrExpectingMapOrStruct
}
switch objT.Kind() {
case reflect.Struct:
values := make([]interface{}, len(columns))
typeMap := Mapper.TypeMap(itemT)
fieldMap := typeMap.Names
for i, k := range columns {
fi, ok := fieldMap[k]
if !ok {
values[i] = new(interface{})
continue
}
// Check for deprecated jsonb tag.
if _, hasJSONBTag := fi.Options["jsonb"]; hasJSONBTag {
return item, errDeprecatedJSONBTag
}
f := reflectx.FieldByIndexes(item, fi.Index)
values[i] = f.Addr().Interface()
if u, ok := values[i].(db.Unmarshaler); ok {
values[i] = scanner{u}
}
}
if converter, ok := iter.sess.(hasConvertValues); ok {
values = converter.ConvertValues(values)
}
if err = rows.Scan(values...); err != nil {
return item, err
}
case reflect.Map:
columns, err := rows.Columns()
if err != nil {
return item, err
}
values := make([]interface{}, len(columns))
for i := range values {
if itemT.Elem().Kind() == reflect.Interface {
values[i] = new(interface{})
} else {
values[i] = reflect.New(itemT.Elem()).Interface()
}
}
if err = rows.Scan(values...); err != nil {
return item, err
}
for i, column := range columns {
item.SetMapIndex(reflect.ValueOf(column), reflect.Indirect(reflect.ValueOf(values[i])))
}
}
return item, nil
}
func reset(data interface{}) {
// Resetting element.
v := reflect.ValueOf(data).Elem()
t := v.Type()
var z reflect.Value
switch v.Kind() {
case reflect.Slice:
z = reflect.MakeSlice(t, 0, v.Cap())
default:
z = reflect.Zero(t)
}
v.Set(z)
}