This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
forked from jackc/pgtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_type.go
353 lines (289 loc) · 7.34 KB
/
array_type.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package pgtype
import (
"database/sql/driver"
"encoding/binary"
"fmt"
"reflect"
"github.com/jackc/pgio"
)
// ArrayType represents an array type. While it implements Value, this is only in service of its type conversion duties
// when registered as a data type in a ConnType. It should not be used directly as a Value. ArrayType is a convenience
// type for types that do not have a concrete array type.
type ArrayType struct {
elements []ValueTranscoder
dimensions []ArrayDimension
typeName string
newElement func() ValueTranscoder
elementOID uint32
status Status
}
func NewArrayType(typeName string, elementOID uint32, newElement func() ValueTranscoder) *ArrayType {
return &ArrayType{typeName: typeName, elementOID: elementOID, newElement: newElement}
}
func (at *ArrayType) NewTypeValue() Value {
return &ArrayType{
elements: at.elements,
dimensions: at.dimensions,
status: at.status,
typeName: at.typeName,
elementOID: at.elementOID,
newElement: at.newElement,
}
}
func (at *ArrayType) TypeName() string {
return at.typeName
}
func (dst *ArrayType) setNil() {
dst.elements = nil
dst.dimensions = nil
dst.status = Null
}
func (dst *ArrayType) Set(src interface{}) error {
// untyped nil and typed nil interfaces are different
if src == nil {
dst.setNil()
return nil
}
sliceVal := reflect.ValueOf(src)
if sliceVal.Kind() != reflect.Slice {
return fmt.Errorf("cannot set non-slice")
}
if sliceVal.IsNil() {
dst.setNil()
return nil
}
dst.elements = make([]ValueTranscoder, sliceVal.Len())
for i := range dst.elements {
v := dst.newElement()
err := v.Set(sliceVal.Index(i).Interface())
if err != nil {
return err
}
dst.elements[i] = v
}
dst.dimensions = []ArrayDimension{{Length: int32(len(dst.elements)), LowerBound: 1}}
dst.status = Present
return nil
}
func (dst ArrayType) Get() interface{} {
switch dst.status {
case Present:
elementValues := make([]interface{}, len(dst.elements))
for i := range dst.elements {
elementValues[i] = dst.elements[i].Get()
}
return elementValues
case Null:
return nil
default:
return dst.status
}
}
func (src *ArrayType) AssignTo(dst interface{}) error {
ptrSlice := reflect.ValueOf(dst)
if ptrSlice.Kind() != reflect.Ptr {
return fmt.Errorf("cannot assign to non-pointer")
}
sliceVal := ptrSlice.Elem()
sliceType := sliceVal.Type()
if sliceType.Kind() != reflect.Slice {
return fmt.Errorf("cannot assign to pointer to non-slice")
}
switch src.status {
case Present:
slice := reflect.MakeSlice(sliceType, len(src.elements), len(src.elements))
elemType := sliceType.Elem()
for i := range src.elements {
ptrElem := reflect.New(elemType)
err := src.elements[i].AssignTo(ptrElem.Interface())
if err != nil {
return err
}
slice.Index(i).Set(ptrElem.Elem())
}
sliceVal.Set(slice)
return nil
case Null:
sliceVal.Set(reflect.Zero(sliceType))
return nil
}
return fmt.Errorf("cannot decode %#v into %T", src, dst)
}
func (dst *ArrayType) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
dst.setNil()
return nil
}
uta, err := ParseUntypedTextArray(string(src))
if err != nil {
return err
}
var elements []ValueTranscoder
if len(uta.Elements) > 0 {
elements = make([]ValueTranscoder, len(uta.Elements))
for i, s := range uta.Elements {
elem := dst.newElement()
var elemSrc []byte
if s != "NULL" {
elemSrc = []byte(s)
}
err = elem.DecodeText(ci, elemSrc)
if err != nil {
return err
}
elements[i] = elem
}
}
dst.elements = elements
dst.dimensions = uta.Dimensions
dst.status = Present
return nil
}
func (dst *ArrayType) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
dst.setNil()
return nil
}
var arrayHeader ArrayHeader
rp, err := arrayHeader.DecodeBinary(ci, src)
if err != nil {
return err
}
var elements []ValueTranscoder
if len(arrayHeader.Dimensions) == 0 {
dst.elements = elements
dst.dimensions = arrayHeader.Dimensions
dst.status = Present
return nil
}
elementCount := arrayHeader.Dimensions[0].Length
for _, d := range arrayHeader.Dimensions[1:] {
elementCount *= d.Length
}
elements = make([]ValueTranscoder, elementCount)
for i := range elements {
elem := dst.newElement()
elemLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4
var elemSrc []byte
if elemLen >= 0 {
elemSrc = src[rp : rp+elemLen]
rp += elemLen
}
err = elem.DecodeBinary(ci, elemSrc)
if err != nil {
return err
}
elements[i] = elem
}
dst.elements = elements
dst.dimensions = arrayHeader.Dimensions
dst.status = Present
return nil
}
func (src ArrayType) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
if len(src.dimensions) == 0 {
return append(buf, '{', '}'), nil
}
buf = EncodeTextArrayDimensions(buf, src.dimensions)
// dimElemCounts is the multiples of elements that each array lies on. For
// example, a single dimension array of length 4 would have a dimElemCounts of
// [4]. A multi-dimensional array of lengths [3,5,2] would have a
// dimElemCounts of [30,10,2]. This is used to simplify when to render a '{'
// or '}'.
dimElemCounts := make([]int, len(src.dimensions))
dimElemCounts[len(src.dimensions)-1] = int(src.dimensions[len(src.dimensions)-1].Length)
for i := len(src.dimensions) - 2; i > -1; i-- {
dimElemCounts[i] = int(src.dimensions[i].Length) * dimElemCounts[i+1]
}
inElemBuf := make([]byte, 0, 32)
for i, elem := range src.elements {
if i > 0 {
buf = append(buf, ',')
}
for _, dec := range dimElemCounts {
if i%dec == 0 {
buf = append(buf, '{')
}
}
elemBuf, err := elem.EncodeText(ci, inElemBuf)
if err != nil {
return nil, err
}
if elemBuf == nil {
buf = append(buf, `NULL`...)
} else {
buf = append(buf, QuoteArrayElementIfNeeded(string(elemBuf))...)
}
for _, dec := range dimElemCounts {
if (i+1)%dec == 0 {
buf = append(buf, '}')
}
}
}
return buf, nil
}
func (src ArrayType) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
arrayHeader := ArrayHeader{
Dimensions: src.dimensions,
ElementOID: int32(src.elementOID),
}
for i := range src.elements {
if src.elements[i].Get() == nil {
arrayHeader.ContainsNull = true
break
}
}
buf = arrayHeader.EncodeBinary(ci, buf)
for i := range src.elements {
sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
elemBuf, err := src.elements[i].EncodeBinary(ci, buf)
if err != nil {
return nil, err
}
if elemBuf != nil {
buf = elemBuf
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
}
}
return buf, nil
}
// Scan implements the database/sql Scanner interface.
func (dst *ArrayType) Scan(src interface{}) error {
if src == nil {
return dst.DecodeText(nil, nil)
}
switch src := src.(type) {
case string:
return dst.DecodeText(nil, []byte(src))
case []byte:
srcCopy := make([]byte, len(src))
copy(srcCopy, src)
return dst.DecodeText(nil, srcCopy)
}
return fmt.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.
func (src ArrayType) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return string(buf), nil
}