forked from twpayne/pgx-geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgxgeom.go
207 lines (185 loc) · 5.21 KB
/
pgxgeom.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
package pgxgeom
import (
"context"
"database/sql/driver"
"encoding/binary"
"encoding/hex"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
)
// A codec implements [github.com/jackc/pgx/v5/pgtype.Codec] for
// [github.com/twpayne/go-geom.T] types.
type codec struct{}
// A binaryEncodePlan implements [github.com/jackc/pgx/v5/pgtype.EncodePlan] for
// [github.com/twpayne/go-geom.T] types in binary format.
type binaryEncodePlan struct{}
// A textEncodePlan implements [github.com/jackc/pgx/v5/pgtype.EncodePlan] for
// [github.com/twpayne/go-geom.T] types in text format.
type textEncodePlan struct{}
// A binaryScanPlan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan] for
// [github.com/twpayne/go-geom.T] types in binary format.
type binaryScanPlan struct{}
// A textScanPlan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan] for
// [github.com/twpayne/go-geom.T] types in text format.
type textScanPlan struct{}
// nativeEndian is the host's native byte order. We have to determine this with
// a runtime test in init() because binary.NativeEndian is a separate value to
// binary.LittleEndian and binary.BigEndian.
var nativeEndian binary.ByteOrder
func init() {
data := []byte{0, 1, 2, 3, 4, 5, 6, 7}
switch binary.NativeEndian.Uint64(data) {
case binary.LittleEndian.Uint64(data):
nativeEndian = binary.LittleEndian
case binary.BigEndian.Uint64(data):
nativeEndian = binary.BigEndian
default:
panic("unsupported byte order")
}
}
// FormatSupported implements
// [github.com/jackc/pgx/v5/pgtype.Codec.FormatSupported].
func (c codec) FormatSupported(format int16) bool {
switch format {
case pgtype.BinaryFormatCode:
return true
case pgtype.TextFormatCode:
return true
default:
return false
}
}
// PreferredFormat implements
// [github.com/jackc/pgx/v5/pgtype.Codec.PreferredFormat].
func (c codec) PreferredFormat() int16 {
return pgtype.BinaryFormatCode
}
// PlanEncode implements [github.com/jackc/pgx/v5/pgtype.Codec.PlanEncode].
func (c codec) PlanEncode(m *pgtype.Map, old uint32, format int16, value any) pgtype.EncodePlan {
if _, ok := value.(geom.T); !ok {
return nil
}
switch format {
case pgtype.BinaryFormatCode:
return binaryEncodePlan{}
case pgtype.TextFormatCode:
return textEncodePlan{}
default:
return nil
}
}
// PlanScan implements [github.com/jackc/pgx/v5/pgtype.Codec.PlanScan].
func (c codec) PlanScan(m *pgtype.Map, old uint32, format int16, target any) pgtype.ScanPlan {
if _, ok := target.(*geom.T); !ok {
return nil
}
switch format {
case pgx.BinaryFormatCode:
return &binaryScanPlan{}
case pgx.TextFormatCode:
return &textScanPlan{}
default:
return nil
}
}
// DecodeDatabaseSQLValue implements
// [github.com/jackc/pgx/v5/pgtype.Codec.DecodeDatabaseSQLValue].
func (c codec) DecodeDatabaseSQLValue(m *pgtype.Map, oid uint32, format int16, src []byte) (driver.Value, error) {
return nil, errors.ErrUnsupported
}
// DecodeValue implements [github.com/jackc/pgx/v5/pgtype.Codec.DecodeValue].
func (c codec) DecodeValue(m *pgtype.Map, oid uint32, format int16, src []byte) (any, error) {
switch format {
case pgtype.TextFormatCode:
var err error
src, err = hex.DecodeString(string(src))
if err != nil {
return nil, err
}
fallthrough
case pgtype.BinaryFormatCode:
return ewkb.Unmarshal(src)
default:
return nil, errors.ErrUnsupported
}
}
// Encode implements [github.com/jackc/pgx/v5/pgtype.EncodePlan.Encode].
func (p binaryEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
g, ok := value.(geom.T)
if !ok {
return buf, errors.ErrUnsupported
}
data, err := ewkb.Marshal(g, nativeEndian)
if err != nil {
return buf, err
}
return append(buf, data...), nil
}
// Encode implements [github.com/jackc/pgx/v5/pgtype.EncodePlan.Encode].
func (p textEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
g, ok := value.(geom.T)
if !ok {
return buf, errors.ErrUnsupported
}
data, err := ewkb.Marshal(g, nativeEndian)
if err != nil {
return buf, err
}
return append(buf, []byte(hex.EncodeToString(data))...), nil
}
// Scan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan.Scan].
func (p binaryScanPlan) Scan(src []byte, target any) error {
pg, ok := target.(*geom.T)
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pg = nil
return nil
}
g, err := ewkb.Unmarshal(src)
if err != nil {
return err
}
*pg = g
return nil
}
// Scan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan.Scan].
func (p textScanPlan) Scan(src []byte, target any) error {
pg, ok := target.(*geom.T)
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pg = nil
return nil
}
var err error
src, err = hex.DecodeString(string(src))
if err != nil {
return err
}
g, err := ewkb.Unmarshal(src)
if err != nil {
return err
}
*pg = g
return nil
}
// Register registers a codec for [github.com/twpayne/go-geom.T] types on conn.
func Register(ctx context.Context, conn *pgx.Conn) error {
var oid uint32
err := conn.QueryRow(ctx, "select 'geometry'::text::regtype::oid").Scan(&oid)
if err != nil {
return err
}
conn.TypeMap().RegisterType(&pgtype.Type{
Codec: codec{},
Name: "geometry",
OID: oid,
})
return nil
}