-
Notifications
You must be signed in to change notification settings - Fork 1
/
geom.go
210 lines (188 loc) · 5.41 KB
/
geom.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
package pgxgeos
import (
"context"
"database/sql/driver"
"encoding/hex"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/twpayne/go-geos"
)
// A geometryCodec implements [github.com/jackc/pgx/v5/pgtype.Codec] for
// [*github.com/twpayne/go-geos.Geom] types.
type geometryCodec struct {
geosContext *geos.Context
}
// A geometryBinaryEncodePlan implements
// [github.com/jackc/pgx/v5/pgtype.EncodePlan] for
// [*github.com/twpayne/go-geos.Geom] types in binary format.
type geometryBinaryEncodePlan struct{}
// A geometryTextEncodePlan implements
// [github.com/jackc/pgx/v5/pgtype.EncodePlan] for
// [*github.com/twpayne/go-geos.Geom] types in text format.
type geometryTextEncodePlan struct{}
// A geometryBinaryScanPlan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan]
// for [*github.com/twpayne/go-geos.Geom] types in binary format.
type geometryBinaryScanPlan struct {
geosContext *geos.Context
}
// A geometryTextScanPlan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan]
// for [*github.com/twpayne/go-geos.Geom] types in text format.
type geometryTextScanPlan struct {
geosContext *geos.Context
}
// FormatSupported implements
// [github.com/jackc/pgx/v5/pgtype.Codec.FormatSupported].
func (c *geometryCodec) 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 *geometryCodec) PreferredFormat() int16 {
return pgtype.BinaryFormatCode
}
// PlanEncode implements [github.com/jackc/pgx/v5/pgtype.Codec.PlanEncode].
func (c *geometryCodec) PlanEncode(m *pgtype.Map, old uint32, format int16, value any) pgtype.EncodePlan {
if _, ok := value.(*geos.Geom); !ok {
return nil
}
switch format {
case pgtype.BinaryFormatCode:
return geometryBinaryEncodePlan{}
case pgtype.TextFormatCode:
return geometryTextEncodePlan{}
default:
return nil
}
}
// PlanScan implements [github.com/jackc/pgx/v5/pgtype.Codec.PlanScan].
func (c *geometryCodec) PlanScan(m *pgtype.Map, old uint32, format int16, target any) pgtype.ScanPlan {
if _, ok := target.(**geos.Geom); !ok {
return nil
}
switch format {
case pgx.BinaryFormatCode:
return geometryBinaryScanPlan{
geosContext: c.geosContext,
}
case pgx.TextFormatCode:
return geometryTextScanPlan{
geosContext: c.geosContext,
}
default:
return nil
}
}
// DecodeDatabaseSQLValue implements
// [github.com/jackc/pgx/v5/pgtype.Codec.DecodeDatabaseSQLValue].
func (c *geometryCodec) 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 *geometryCodec) 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:
geom, err := c.geosContext.NewGeomFromWKB(src)
return geom, err
default:
return nil, errors.ErrUnsupported
}
}
// Encode implements [github.com/jackc/pgx/v5/pgtype.EncodePlan.Encode].
func (p geometryBinaryEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
geom, ok := value.(*geos.Geom)
if !ok {
return buf, errors.ErrUnsupported
}
return append(buf, geom.ToEWKBWithSRID()...), nil
}
// Encode implements [github.com/jackc/pgx/v5/pgtype.EncodePlan.Encode].
func (p geometryTextEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
geom, ok := value.(*geos.Geom)
if !ok {
return buf, errors.ErrUnsupported
}
wkb := geom.ToEWKBWithSRID()
return append(buf, []byte(hex.EncodeToString(wkb))...), nil
}
// Scan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan.Scan].
func (p geometryBinaryScanPlan) Scan(src []byte, target any) error {
pgeom, ok := target.(**geos.Geom)
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pgeom = nil
return nil
}
geom, err := p.geosContext.NewGeomFromWKB(src)
if err != nil {
return err
}
(*pgeom).Destroy()
*pgeom = geom
return nil
}
// Scan implements [github.com/jackc/pgx/v5/pgtype.ScanPlan.Scan].
func (p geometryTextScanPlan) Scan(src []byte, target any) error {
pgeom, ok := target.(**geos.Geom)
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pgeom = nil
return nil
}
var err error
src, err = hex.DecodeString(string(src))
if err != nil {
return err
}
geom, err := p.geosContext.NewGeomFromWKB(src)
if err != nil {
return err
}
(*pgeom).Destroy()
*pgeom = geom
return nil
}
// registerGeom registers codecs for [*github.com/twpayne/go-geos.Geom] types on conn.
func registerGeom(ctx context.Context, conn *pgx.Conn, geosContext *geos.Context) 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
}
if geosContext == nil {
geosContext = geos.DefaultContext
}
conn.TypeMap().RegisterType(&pgtype.Type{
Codec: &geometryCodec{
geosContext: geosContext,
},
Name: "geography",
OID: geographyOID,
})
conn.TypeMap().RegisterType(&pgtype.Type{
Codec: &geometryCodec{
geosContext: geosContext,
},
Name: "geometry",
OID: geometryOID,
})
return nil
}