-
Notifications
You must be signed in to change notification settings - Fork 103
/
sql.go
314 lines (281 loc) · 6.54 KB
/
sql.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
package ewkb
import (
"database/sql/driver"
"fmt"
"strings"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkbcommon"
)
// ErrExpectedByteSlice is returned when a []byte is expected.
type ErrExpectedByteSlice struct {
Value interface{}
}
func (e ErrExpectedByteSlice) Error() string {
return fmt.Sprintf("wkb: want []byte, got %T", e.Value)
}
// A Point is a EWKB-encoded Point that implements the sql.Scanner and
// driver.Value interfaces.
type Point struct {
*geom.Point
}
// A LineString is a EWKB-encoded LineString that implements the
// sql.Scanner and driver.Value interfaces.
type LineString struct {
*geom.LineString
}
// A Polygon is a EWKB-encoded Polygon that implements the sql.Scanner and
// driver.Value interfaces.
type Polygon struct {
*geom.Polygon
}
// A MultiPoint is a EWKB-encoded MultiPoint that implements the
// sql.Scanner and driver.Value interfaces.
type MultiPoint struct {
*geom.MultiPoint
}
// A MultiLineString is a EWKB-encoded MultiLineString that implements the
// sql.Scanner and driver.Value interfaces.
type MultiLineString struct {
*geom.MultiLineString
}
// A MultiPolygon is a EWKB-encoded MultiPolygon that implements the
// sql.Scanner and driver.Value interfaces.
type MultiPolygon struct {
*geom.MultiPolygon
}
// A GeometryCollection is a EWKB-encoded GeometryCollection that implements
// the sql.Scanner and driver.Value interfaces.
type GeometryCollection struct {
*geom.GeometryCollection
}
// Scan scans from a []byte.
func (p *Point) Scan(src interface{}) error {
if src == nil {
p.Point = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
p1, ok := got.(*geom.Point)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: p1, Want: p}
}
p.Point = p1
return nil
}
// Valid returns true if p has a value.
func (p *Point) Valid() bool {
return p != nil && p.Point != nil
}
// Value returns the EWKB encoding of p.
func (p *Point) Value() (driver.Value, error) {
if p.Point == nil {
return nil, nil
}
return value(p.Point)
}
// Scan scans from a []byte.
func (ls *LineString) Scan(src interface{}) error {
if src == nil {
ls.LineString = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
ls1, ok := got.(*geom.LineString)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: ls1, Want: ls}
}
ls.LineString = ls1
return nil
}
// Valid return true if ls has a value.
func (ls *LineString) Valid() bool {
return ls != nil && ls.LineString != nil
}
// Value returns the EWKB encoding of ls.
func (ls *LineString) Value() (driver.Value, error) {
if ls.LineString == nil {
return nil, nil
}
return value(ls.LineString)
}
// Scan scans from a []byte.
func (p *Polygon) Scan(src interface{}) error {
if src == nil {
p.Polygon = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
p1, ok := got.(*geom.Polygon)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: p1, Want: p}
}
p.Polygon = p1
return nil
}
// Valid returns true if p has a value.
func (p *Polygon) Valid() bool {
return p != nil && p.Polygon != nil
}
// Value returns the EWKB encoding of p.
func (p *Polygon) Value() (driver.Value, error) {
if p.Polygon == nil {
return nil, nil
}
return value(p.Polygon)
}
// Scan scans from a []byte.
func (mp *MultiPoint) Scan(src interface{}) error {
if src == nil {
mp.MultiPoint = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
mp1, ok := got.(*geom.MultiPoint)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: mp1, Want: mp}
}
mp.MultiPoint = mp1
return nil
}
// Valid returns true if mp has a value.
func (mp *MultiPoint) Valid() bool {
return mp != nil && mp.MultiPoint != nil
}
// Value returns the EWKB encoding of mp.
func (mp *MultiPoint) Value() (driver.Value, error) {
if mp.MultiPoint == nil {
return nil, nil
}
return value(mp.MultiPoint)
}
// Scan scans from a []byte.
func (mls *MultiLineString) Scan(src interface{}) error {
if src == nil {
mls.MultiLineString = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
mls1, ok := got.(*geom.MultiLineString)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: mls1, Want: mls}
}
mls.MultiLineString = mls1
return nil
}
// Valid returns true if mls has a value.
func (mls *MultiLineString) Valid() bool {
return mls != nil && mls.MultiLineString != nil
}
// Value returns the EWKB encoding of mls.
func (mls *MultiLineString) Value() (driver.Value, error) {
if mls.MultiLineString == nil {
return nil, nil
}
return value(mls.MultiLineString)
}
// Scan scans from a []byte.
func (mp *MultiPolygon) Scan(src interface{}) error {
if src == nil {
mp.MultiPolygon = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
mp1, ok := got.(*geom.MultiPolygon)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: mp1, Want: mp}
}
mp.MultiPolygon = mp1
return nil
}
// Valid returns true if mp has a value.
func (mp *MultiPolygon) Valid() bool {
return mp != nil && mp.MultiPolygon != nil
}
// Value returns the EWKB encoding of mp.
func (mp *MultiPolygon) Value() (driver.Value, error) {
if mp.MultiPolygon == nil {
return nil, nil
}
return value(mp.MultiPolygon)
}
// Scan scans from a []byte.
func (gc *GeometryCollection) Scan(src interface{}) error {
if src == nil {
gc.GeometryCollection = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return ErrExpectedByteSlice{Value: src}
}
got, err := Unmarshal(b)
if err != nil {
return err
}
gc1, ok := got.(*geom.GeometryCollection)
if !ok {
return wkbcommon.ErrUnexpectedType{Got: gc1, Want: gc}
}
gc.GeometryCollection = gc1
return nil
}
// Valid returns true if gc has a value.
func (gc *GeometryCollection) Valid() bool {
return gc != nil && gc.GeometryCollection != nil
}
// Value returns the EWKB encoding of gc.
func (gc *GeometryCollection) Value() (driver.Value, error) {
if gc.GeometryCollection == nil {
return nil, nil
}
return value(gc.GeometryCollection)
}
func value(g geom.T) (driver.Value, error) {
sb := &strings.Builder{}
if err := Write(sb, NDR, g); err != nil {
return nil, err
}
return []byte(sb.String()), nil
}