-
Notifications
You must be signed in to change notification settings - Fork 4
/
pgxgeom.go
326 lines (288 loc) · 8.81 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
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
package pgxgeom
import (
"context"
"database/sql/driver"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"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
}
// GeomScanner enables PostGIS geometry/geography values to be scanned into
// arbitrary Go types. For more context, see section "Extending Existing
// PostgreSQL Type Support" of the README for jackc/pgx/v5/pgtype.
type GeomScanner interface {
ScanGeom(v geom.T) error
}
// GeomValuer enables PostGIS geometry/geography values to be marshaled from
// arbitrary Go types. For more context, see section "Extending Existing
// PostgreSQL Type Support" of the README for jackc/pgx/v5/pgtype.
type GeomValuer interface {
GeomValue() (geom.T, error)
}
// unexpectedTypeError indicates that a PostGIS value did not meet the type
// constraints to be scanned into a particular Go value. For example, this
// occurs when attempting to scan a `geometry(point)` into a `*geom.Polygon`.
type unexpectedTypeError struct {
Got any
Want any
}
func (e unexpectedTypeError) Error() string {
return fmt.Sprintf("pgxgeom: got %T, want %T", e.Got, e.Want)
}
// unsupportedTypeError indicates that a given Go value could not be converted to
// a GeomScanner/GeomValuer. For example, this occurs if you attempt to scan
// into a `*bool`.
type unsupportedTypeError struct {
Got any
}
func (e unsupportedTypeError) Error() string {
return fmt.Sprintf("pgxgeom: unsupported type %T", e.Got)
}
// genericGeomValuer can be used to marshal generic geom.T values as well as
// any concrete value like a *geom.Point.
type genericGeomValuer struct {
value geom.T
}
func (gv genericGeomValuer) GeomValue() (geom.T, error) {
return gv.value, nil
}
// genericGeomScanner can only be used to scan into generic geom.T values. To
// scan into concrete values like a *geom.Point, a more specific scanner type
// is needed to perform the appropriate error checking.
type genericGeomScanner struct {
target *geom.T
}
func (sc genericGeomScanner) ScanGeom(v geom.T) error {
*sc.target = v
return nil
}
// concreteScanner is used to scan into a specific, concrete geom.T type.
// The type parameter T should be in *non-pointer* form, like `geom.Point`,
// such that `*T` implements `geom.T`.
type concreteScanner[T any] struct {
target *T
}
func (sc concreteScanner[T]) ScanGeom(v geom.T) error {
var vv any = v // work around "impossible type assertion" compiler error
concrete, ok := vv.(*T)
if !ok {
return unexpectedTypeError{Got: v, Want: sc.target}
}
*sc.target = *concrete
return nil
}
func getGeomScanner(v any) (GeomScanner, error) {
switch v := v.(type) {
case GeomScanner:
return v, nil
case *geom.T:
return genericGeomScanner{v}, nil
case *geom.Point:
return concreteScanner[geom.Point]{v}, nil
case *geom.LineString:
return concreteScanner[geom.LineString]{v}, nil
case *geom.Polygon:
return concreteScanner[geom.Polygon]{v}, nil
case *geom.MultiPoint:
return concreteScanner[geom.MultiPoint]{v}, nil
case *geom.MultiLineString:
return concreteScanner[geom.MultiLineString]{v}, nil
case *geom.MultiPolygon:
return concreteScanner[geom.MultiPolygon]{v}, nil
case *geom.GeometryCollection:
return concreteScanner[geom.GeometryCollection]{v}, nil
default:
return nil, unsupportedTypeError{v}
}
}
//nolint:ireturn
func getGeomValuer(v any) (GeomValuer, error) {
switch v := v.(type) {
case GeomValuer:
return v, nil
case geom.T:
return genericGeomValuer{v}, nil
default:
return nil, unsupportedTypeError{v}
}
}
// 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 _, err := getGeomValuer(value); err != nil {
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 _, err := getGeomScanner(target); err != nil {
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
}
}
func encodeGeomValue(value any) (ewkbBuf []byte, err error) {
valuer, err := getGeomValuer(value)
if err != nil {
return nil, err
}
g, err := valuer.GeomValue()
if err != nil {
return nil, err
}
return ewkb.Marshal(g, nativeEndian)
}
// Encode implements [github.com/jackc/pgx/v5/pgtype.EncodePlan.Encode].
func (p binaryEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
data, err := encodeGeomValue(value)
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) {
data, err := encodeGeomValue(value)
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 {
scanner, err := getGeomScanner(target)
if err != nil {
return err
}
if len(src) == 0 {
return scanner.ScanGeom(nil)
}
g, err := ewkb.Unmarshal(src)
if err != nil {
return err
}
return scanner.ScanGeom(g)
}
// Scan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan.Scan].
func (p textScanPlan) Scan(src []byte, target any) error {
scanner, err := getGeomScanner(target)
if err != nil {
return err
}
if len(src) == 0 {
return scanner.ScanGeom(nil)
}
src, err = hex.DecodeString(string(src))
if err != nil {
return err
}
g, err := ewkb.Unmarshal(src)
if err != nil {
return err
}
return scanner.ScanGeom(g)
}
// Register registers a codec for [github.com/twpayne/go-geom.T] types on conn.
func Register(ctx context.Context, conn *pgx.Conn) error {
var geographyOID, geometryOID uint32
err := conn.QueryRow(ctx, "select 'geography'::text::regtype::oid, 'geometry'::text::regtype::oid").Scan(&geographyOID, &geometryOID)
if err != nil {
return err
}
conn.TypeMap().RegisterType(&pgtype.Type{
Codec: codec{},
Name: "geography",
OID: geographyOID,
})
conn.TypeMap().RegisterType(&pgtype.Type{
Codec: codec{},
Name: "geometry",
OID: geometryOID,
})
return nil
}