/
bytea.go
255 lines (205 loc) · 5.54 KB
/
bytea.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
package pgtype
import (
"database/sql/driver"
"encoding/hex"
"fmt"
)
type BytesScanner interface {
// ScanBytes receives a byte slice of driver memory that is only valid until the next database method call.
ScanBytes(v []byte) error
}
type BytesValuer interface {
// BytesValue returns a byte slice of the byte data. The caller must not change the returned slice.
BytesValue() ([]byte, error)
}
// DriverBytes is a byte slice that holds a reference to memory owned by the driver. It is only valid from the time it
// is scanned until Rows.Next or Rows.Close is called. It is never safe to use DriverBytes with QueryRow as Row.Scan
// internally calls Rows.Close before returning.
type DriverBytes []byte
func (b *DriverBytes) ScanBytes(v []byte) error {
*b = v
return nil
}
// PreallocBytes is a byte slice of preallocated memory that scanned bytes will be copied to. If it is too small a new
// slice will be allocated.
type PreallocBytes []byte
func (b *PreallocBytes) ScanBytes(v []byte) error {
if v == nil {
*b = nil
return nil
}
if len(v) <= len(*b) {
*b = (*b)[:len(v)]
} else {
*b = make(PreallocBytes, len(v))
}
copy(*b, v)
return nil
}
// UndecodedBytes can be used as a scan target to get the raw bytes from PostgreSQL without any decoding.
type UndecodedBytes []byte
type scanPlanAnyToUndecodedBytes struct{}
func (scanPlanAnyToUndecodedBytes) Scan(src []byte, dst any) error {
dstBuf := dst.(*UndecodedBytes)
if src == nil {
*dstBuf = nil
return nil
}
*dstBuf = make([]byte, len(src))
copy(*dstBuf, src)
return nil
}
type ByteaCodec struct{}
func (ByteaCodec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (ByteaCodec) PreferredFormat() int16 {
return BinaryFormatCode
}
func (ByteaCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan {
switch format {
case BinaryFormatCode:
switch value.(type) {
case []byte:
return encodePlanBytesCodecBinaryBytes{}
case BytesValuer:
return encodePlanBytesCodecBinaryBytesValuer{}
}
case TextFormatCode:
switch value.(type) {
case []byte:
return encodePlanBytesCodecTextBytes{}
case BytesValuer:
return encodePlanBytesCodecTextBytesValuer{}
}
}
return nil
}
type encodePlanBytesCodecBinaryBytes struct{}
func (encodePlanBytesCodecBinaryBytes) Encode(value any, buf []byte) (newBuf []byte, err error) {
b := value.([]byte)
if b == nil {
return nil, nil
}
return append(buf, b...), nil
}
type encodePlanBytesCodecBinaryBytesValuer struct{}
func (encodePlanBytesCodecBinaryBytesValuer) Encode(value any, buf []byte) (newBuf []byte, err error) {
b, err := value.(BytesValuer).BytesValue()
if err != nil {
return nil, err
}
if b == nil {
return nil, nil
}
return append(buf, b...), nil
}
type encodePlanBytesCodecTextBytes struct{}
func (encodePlanBytesCodecTextBytes) Encode(value any, buf []byte) (newBuf []byte, err error) {
b := value.([]byte)
if b == nil {
return nil, nil
}
buf = append(buf, `\x`...)
buf = append(buf, hex.EncodeToString(b)...)
return buf, nil
}
type encodePlanBytesCodecTextBytesValuer struct{}
func (encodePlanBytesCodecTextBytesValuer) Encode(value any, buf []byte) (newBuf []byte, err error) {
b, err := value.(BytesValuer).BytesValue()
if err != nil {
return nil, err
}
if b == nil {
return nil, nil
}
buf = append(buf, `\x`...)
buf = append(buf, hex.EncodeToString(b)...)
return buf, nil
}
func (ByteaCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
switch format {
case BinaryFormatCode:
switch target.(type) {
case *[]byte:
return scanPlanBinaryBytesToBytes{}
case BytesScanner:
return scanPlanBinaryBytesToBytesScanner{}
}
case TextFormatCode:
switch target.(type) {
case *[]byte:
return scanPlanTextByteaToBytes{}
case BytesScanner:
return scanPlanTextByteaToBytesScanner{}
}
}
return nil
}
type scanPlanBinaryBytesToBytes struct{}
func (scanPlanBinaryBytesToBytes) Scan(src []byte, dst any) error {
dstBuf := dst.(*[]byte)
if src == nil {
*dstBuf = nil
return nil
}
*dstBuf = make([]byte, len(src))
copy(*dstBuf, src)
return nil
}
type scanPlanBinaryBytesToBytesScanner struct{}
func (scanPlanBinaryBytesToBytesScanner) Scan(src []byte, dst any) error {
scanner := (dst).(BytesScanner)
return scanner.ScanBytes(src)
}
type scanPlanTextByteaToBytes struct{}
func (scanPlanTextByteaToBytes) Scan(src []byte, dst any) error {
dstBuf := dst.(*[]byte)
if src == nil {
*dstBuf = nil
return nil
}
buf, err := decodeHexBytea(src)
if err != nil {
return err
}
*dstBuf = buf
return nil
}
type scanPlanTextByteaToBytesScanner struct{}
func (scanPlanTextByteaToBytesScanner) Scan(src []byte, dst any) error {
scanner := (dst).(BytesScanner)
buf, err := decodeHexBytea(src)
if err != nil {
return err
}
return scanner.ScanBytes(buf)
}
func decodeHexBytea(src []byte) ([]byte, error) {
if src == nil {
return nil, nil
}
if len(src) < 2 || src[0] != '\\' || src[1] != 'x' {
return nil, fmt.Errorf("invalid hex format")
}
buf := make([]byte, (len(src)-2)/2)
_, err := hex.Decode(buf, src[2:])
if err != nil {
return nil, err
}
return buf, nil
}
func (c ByteaCodec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) {
return codecDecodeToTextFormat(c, m, oid, format, src)
}
func (c ByteaCodec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) {
if src == nil {
return nil, nil
}
var buf []byte
err := codecScan(c, m, oid, format, src, &buf)
if err != nil {
return nil, err
}
return buf, nil
}