forked from jackc/pgtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite_fields.go
107 lines (86 loc) · 2.95 KB
/
composite_fields.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
package pgtype
import "fmt"
// CompositeFields scans the fields of a composite type into the elements of the CompositeFields value. To scan a
// nullable value use a *CompositeFields. It will be set to nil in case of null.
//
// CompositeFields implements EncodeBinary and EncodeText. However, functionality is limited due to CompositeFields not
// knowing the PostgreSQL schema of the composite type. Prefer using a registered CompositeType.
type CompositeFields []interface{}
func (cf CompositeFields) DecodeBinary(ci *ConnInfo, src []byte) error {
if len(cf) == 0 {
return fmt.Errorf("cannot decode into empty CompositeFields")
}
if src == nil {
return fmt.Errorf("cannot decode unexpected null into CompositeFields")
}
scanner := NewCompositeBinaryScanner(ci, src)
for _, f := range cf {
scanner.ScanValue(f)
}
if scanner.Err() != nil {
return scanner.Err()
}
return nil
}
func (cf CompositeFields) DecodeText(ci *ConnInfo, src []byte) error {
if len(cf) == 0 {
return fmt.Errorf("cannot decode into empty CompositeFields")
}
if src == nil {
return fmt.Errorf("cannot decode unexpected null into CompositeFields")
}
scanner := NewCompositeTextScanner(ci, src)
for _, f := range cf {
scanner.ScanValue(f)
}
if scanner.Err() != nil {
return scanner.Err()
}
return nil
}
// EncodeText encodes composite fields into the text format. Prefer registering a CompositeType to using
// CompositeFields to encode directly.
func (cf CompositeFields) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
b := NewCompositeTextBuilder(ci, buf)
for _, f := range cf {
if textEncoder, ok := f.(TextEncoder); ok {
b.AppendEncoder(textEncoder)
} else {
b.AppendValue(f)
}
}
return b.Finish()
}
// EncodeBinary encodes composite fields into the binary format. Unlike CompositeType the schema of the destination is
// unknown. Prefer registering a CompositeType to using CompositeFields to encode directly. Because the binary
// composite format requires the OID of each field to be specified the only types that will work are those known to
// ConnInfo.
//
// In particular:
//
// * Nil cannot be used because there is no way to determine what type it.
// * Integer types must be exact matches. e.g. A Go int32 into a PostgreSQL bigint will fail.
// * No dereferencing will be done. e.g. *Text must be used instead of Text.
func (cf CompositeFields) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
b := NewCompositeBinaryBuilder(ci, buf)
for _, f := range cf {
dt, ok := ci.DataTypeForValue(f)
if !ok {
return nil, fmt.Errorf("Unknown OID for %#v", f)
}
if binaryEncoder, ok := f.(BinaryEncoder); ok {
b.AppendEncoder(dt.OID, binaryEncoder)
} else {
err := dt.Value.Set(f)
if err != nil {
return nil, err
}
if binaryEncoder, ok := dt.Value.(BinaryEncoder); ok {
b.AppendEncoder(dt.OID, binaryEncoder)
} else {
return nil, fmt.Errorf("Cannot encode binary format for %v", f)
}
}
}
return b.Finish()
}