forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
183 lines (140 loc) · 3.73 KB
/
loader.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
package pg
import (
"reflect"
"strconv"
)
var (
Discard = discardLoader{}
)
//------------------------------------------------------------------------------
type singleRecordCollection struct {
v interface{}
}
var _ Collection = &singleRecordCollection{}
func (f *singleRecordCollection) NewRecord() interface{} {
return f.v
}
//------------------------------------------------------------------------------
type discardLoader struct{}
var _ Collection = &discardLoader{}
var _ ColumnLoader = &discardLoader{}
func (l discardLoader) NewRecord() interface{} {
return l
}
func (discardLoader) LoadColumn(colIdx int, colName string, b []byte) error {
return nil
}
//------------------------------------------------------------------------------
type structLoader struct {
v reflect.Value // reflect.Struct
fields map[string]*pgValue
}
var _ ColumnLoader = &structLoader{}
func newStructLoader(v reflect.Value) *structLoader {
return &structLoader{
v: v,
fields: structs.Fields(v.Type()),
}
}
func (l *structLoader) LoadColumn(colIdx int, colName string, b []byte) error {
field, ok := l.fields[colName]
if !ok {
return errorf("pg: cannot map field %q", colName)
}
return field.DecodeValue(l.v, b)
}
//------------------------------------------------------------------------------
type valuesLoader struct {
values []interface{}
}
var _ ColumnLoader = &valuesLoader{}
func LoadInto(values ...interface{}) ColumnLoader {
return &valuesLoader{values}
}
func (l *valuesLoader) LoadColumn(colIdx int, _ string, b []byte) error {
return Decode(l.values[colIdx], b)
}
//------------------------------------------------------------------------------
type Strings []string
var _ Collection = &Strings{}
var _ ColumnLoader = &Strings{}
func (strings *Strings) NewRecord() interface{} {
return strings
}
func (strings *Strings) LoadColumn(colIdx int, _ string, b []byte) error {
*strings = append(*strings, string(b))
return nil
}
func (strings Strings) AppendQuery(dst []byte) []byte {
if len(strings) <= 0 {
return dst
}
for _, s := range strings {
dst = appendString(dst, s)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
//------------------------------------------------------------------------------
type Ints []int64
var _ Collection = &Ints{}
var _ ColumnLoader = &Ints{}
func (ints *Ints) NewRecord() interface{} {
return ints
}
func (ints *Ints) LoadColumn(colIdx int, colName string, b []byte) error {
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
*ints = append(*ints, n)
return nil
}
func (ints Ints) AppendQuery(dst []byte) []byte {
if len(ints) <= 0 {
return dst
}
for _, v := range ints {
dst = strconv.AppendInt(dst, v, 10)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
//------------------------------------------------------------------------------
type IntSet map[int64]struct{}
var _ Collection = &IntSet{}
var _ ColumnLoader = &IntSet{}
func (set *IntSet) NewRecord() interface{} {
return set
}
func (setptr *IntSet) LoadColumn(colIdx int, colName string, b []byte) error {
set := *setptr
if set == nil {
*setptr = make(IntSet)
set = *setptr
}
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
set[n] = struct{}{}
return nil
}
//------------------------------------------------------------------------------
func NewColumnLoader(dst interface{}) (ColumnLoader, error) {
v := reflect.ValueOf(dst)
if !v.IsValid() {
return nil, errorf("pg: Decode(nil)")
}
if v.Kind() != reflect.Ptr {
return nil, errorf("pg: Decode(nonsettable %T)", dst)
}
vv := v.Elem()
switch vv.Kind() {
case reflect.Struct:
return newStructLoader(vv), nil
}
return nil, errorf("pg: Decode(unsupported %T)", dst)
}