-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructscan.go
228 lines (186 loc) · 5.84 KB
/
structscan.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
package pgxscan
import (
"context"
"fmt"
"reflect"
"github.com/jackc/pgconn"
pgx "github.com/jackc/pgx/v4"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
"github.com/pkg/errors"
)
var DefaultMapper = reflectx.NewMapperFunc("db", sqlx.NameMapper)
type Querier interface {
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)
}
func Get(ctx context.Context, querier Querier, dest interface{}, query string, args ...interface{}) error {
rows, err := querier.Query(ctx, query, args...)
if err != nil {
return err
}
return ScanStruct(rows, dest)
}
func Select(ctx context.Context, querier Querier, dest interface{}, query string, args ...interface{}) error {
rows, err := querier.Query(ctx, query, args...)
if err != nil {
return err
}
return ScanStructs(rows, dest)
}
func SelectFlat(ctx context.Context, querier Querier, dest interface{}, query string, args ...interface{}) error {
rows, err := querier.Query(ctx, query, args...)
if err != nil {
return err
}
return ScanFlat(rows, dest)
}
// ScanStruct scans a pgx.Rows into destination struct passed by reference based on the "db" fields tags.
// This is workaround function for pgx.Rows with single row as pgx/v4 does not allow to get row metadata
// from pgx.Row - see https://github.com/jackc/pgx/issues/627 for details.
//
// If there are no rows pgx.ErrNoRows is returned.
// If there are more than one row in the result - they are ignored.
// Function call closes rows, so caller may skip it.
func ScanStruct(r pgx.Rows, dest interface{}) error {
defer r.Close()
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr {
return errors.New("dest must be a pointer to a struct, not a value")
}
if v.IsNil() {
return errors.New("dest is nil pointer")
}
if !r.Next() {
if err := r.Err(); err != nil {
return err
}
return pgx.ErrNoRows
}
columns, err := rowMetadata(r, v)
if err != nil {
return err
}
fields := DefaultMapper.TraversalsByName(v.Type(), columns)
values := make([]interface{}, len(columns))
err = fieldsByTraversal(v, fields, values)
if err != nil {
return err
}
return r.Scan(values...)
}
func ScanFlat(r pgx.Rows, dest interface{}) error {
defer r.Close()
valDest := reflect.ValueOf(dest)
if valDest.Kind() != reflect.Ptr || valDest.Elem().Kind() != reflect.Slice {
return errors.New("invalid input, expected a pointer to a slice")
}
typDest := valDest.Type()
typSlice := typDest.Elem()
typElem := typSlice.Elem()
valSlice := reflect.MakeSlice(typSlice, 0, 0)
for r.Next() {
valRow := reflect.New(typElem)
if err := r.Scan(valRow.Interface()); err != nil {
return errors.Wrap(err, "failed to parse a row")
}
valSlice = reflect.Append(valSlice, valRow.Elem())
}
valDest.Elem().Set(valSlice)
return r.Err()
}
// ScanStructs scans a pgx.Rows into destination structs list passed by reference based on the "db" fields tags
func ScanStructs(r pgx.Rows, dest interface{}) error {
defer r.Close()
var (
columns []string
err error
)
destType := reflect.TypeOf(dest) // either *[]test or *[]*test
if destType.Kind() != reflect.Ptr || destType.Elem().Kind() != reflect.Slice {
return fmt.Errorf("expected a pointer to a slice, got %s", destType)
}
sliceType := destType.Elem() // either []test or []*test
elementType := sliceType.Elem()
var structTypeToCreate *reflect.Type
if elementType.Kind() == reflect.Ptr {
x := elementType.Elem()
structTypeToCreate = &x
} else {
structTypeToCreate = &elementType
}
resultSlice := reflect.MakeSlice(sliceType, 0, 0)
for r.Next() {
destVal := reflect.New(*structTypeToCreate)
if destVal.Kind() != reflect.Ptr {
return errors.New("must return a pointer to a new struct, not a value, to ScanStructs destination")
}
if destVal.IsNil() {
return errors.New("nil pointer returned to ScanStructs destination")
}
if len(columns) == 0 {
columns, err = rowMetadata(r, destVal)
if err != nil {
return err
}
}
fields := DefaultMapper.TraversalsByName(destVal.Type(), columns)
values := make([]interface{}, len(columns))
err := fieldsByTraversal(destVal, fields, values)
if err != nil {
return err
}
if err := r.Scan(values...); err != nil {
return err
}
// pointers are only applied directly
if destVal.Kind() == reflect.Ptr && destVal.Elem().Kind() == elementType.Kind() {
resultSlice = reflect.Append(resultSlice, destVal.Elem())
} else {
resultSlice = reflect.Append(resultSlice, destVal)
}
}
reflect.ValueOf(dest).Elem().Set(resultSlice)
return r.Err()
}
func rowMetadata(r pgx.Rows, v reflect.Value) (columns []string, err error) {
fieldDescriptions := r.FieldDescriptions()
columns = make([]string, len(fieldDescriptions))
for i, fieldDescription := range fieldDescriptions {
columns[i] = string(fieldDescription.Name)
}
fields := DefaultMapper.TraversalsByName(v.Type(), columns)
// if we are not unsafe and are missing fields, return an error
if f, err := missingFields(fields); err != nil {
return columns, fmt.Errorf("missing column %q in dest %s", columns[f], v.Type())
}
return
}
func missingFields(traversals [][]int) (field int, err error) {
for i, t := range traversals {
if len(t) == 0 {
return i, errors.New("missing field")
}
}
return 0, nil
}
func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}) error {
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return errors.New("argument is not a struct")
}
for i, traversal := range traversals {
if len(traversal) == 0 {
values[i] = new(interface{})
continue
}
f := reflectx.FieldByIndexes(v, traversal)
if f.Kind() == reflect.Ptr {
values[i] = f.Interface()
} else {
values[i] = f.Addr().Interface()
}
}
return nil
}