-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgis.go
706 lines (602 loc) · 20.4 KB
/
postgis.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package postgis
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
"github.com/go-spatial/geom"
"github.com/go-spatial/geom/encoding/wkb"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/dict"
"github.com/go-spatial/tegola/provider"
)
const Name = "postgis"
// Provider provides the postgis data provider.
type Provider struct {
config pgx.ConnPoolConfig
pool *pgx.ConnPool
// map of layer name and corresponding sql
layers map[string]Layer
srid uint64
firstlayer string
}
const (
// We quote the field and table names to prevent colliding with postgres keywords.
stdSQL = `SELECT %[1]v FROM %[2]v WHERE "%[3]v" && ` + bboxToken
mvtSQL = `SELECT %[1]v FROM %[2]v`
// SQL to get the column names, without hitting the information_schema. Though it might be better to hit the information_schema.
fldsSQL = `SELECT * FROM %[1]v LIMIT 0;`
)
const (
DefaultPort = 5432
DefaultSRID = tegola.WebMercator
DefaultMaxConn = 100
DefaultSSLMode = "disable"
DefaultSSLKey = ""
DefaultSSLCert = ""
)
const (
ConfigKeyHost = "host"
ConfigKeyPort = "port"
ConfigKeyDB = "database"
ConfigKeyUser = "user"
ConfigKeyPassword = "password"
ConfigKeySSLMode = "ssl_mode"
ConfigKeySSLKey = "ssl_key"
ConfigKeySSLCert = "ssl_cert"
ConfigKeySSLRootCert = "ssl_root_cert"
ConfigKeyMaxConn = "max_connections"
ConfigKeySRID = "srid"
ConfigKeyLayers = "layers"
ConfigKeyLayerName = "name"
ConfigKeyTablename = "tablename"
ConfigKeySQL = "sql"
ConfigKeyFields = "fields"
ConfigKeyGeomField = "geometry_fieldname"
ConfigKeyGeomIDField = "id_fieldname"
ConfigKeyGeomType = "geometry_type"
)
// isSelectQuery is a regexp to check if a query starts with `SELECT`,
// case-insensitive and ignoring any preceeding whitespace and SQL comments.
var isSelectQuery = regexp.MustCompile(`(?i)^((\s*)(--.*\n)?)*select`)
// CreateProvider instantiates and returns a new postgis provider or an error.
// The function will validate that the config object looks good before
// trying to create a driver. This Provider supports the following fields
// in the provided map[string]interface{} map:
//
// host (string): [Required] postgis database host
// port (int): [Required] postgis database port (required)
// database (string): [Required] postgis database name
// user (string): [Required] postgis database user
// password (string): [Required] postgis database password
// srid (int): [Optional] The default SRID for the provider. Defaults to WebMercator (3857) but also supports WGS84 (4326)
// max_connections : [Optional] The max connections to maintain in the connection pool. Default is 100. 0 means no max.
// layers (map[string]struct{}) — This is map of layers keyed by the layer name. supports the following properties
//
// name (string): [Required] the name of the layer. This is used to reference this layer from map layers.
// tablename (string): [*Required] the name of the database table to query against. Required if sql is not defined.
// geometry_fieldname (string): [Optional] the name of the filed which contains the geometry for the feature. defaults to geom
// id_fieldname (string): [Optional] the name of the feature id field. defaults to gid
// fields ([]string): [Optional] a list of fields to include alongside the feature. Can be used if sql is not defined.
// srid (int): [Optional] the SRID of the layer. Supports 3857 (WebMercator) or 4326 (WGS84).
// sql (string): [*Required] custom SQL to use use. Required if tablename is not defined. Supports the following tokens:
//
// !BBOX! - [Required] will be replaced with the bounding box of the tile before the query is sent to the database.
// !ZOOM! - [Optional] will be replaced with the "Z" (zoom) value of the requested tile.
//
func CreateProvider(config dict.Dicter, providerType string) (*Provider, error) {
host, err := config.String(ConfigKeyHost, nil)
if err != nil {
return nil, err
}
db, err := config.String(ConfigKeyDB, nil)
if err != nil {
return nil, err
}
user, err := config.String(ConfigKeyUser, nil)
if err != nil {
return nil, err
}
password, err := config.String(ConfigKeyPassword, nil)
if err != nil {
return nil, err
}
sslmode := DefaultSSLMode
sslmode, err = config.String(ConfigKeySSLMode, &sslmode)
if err != nil {
return nil, err
}
sslkey := DefaultSSLKey
sslkey, err = config.String(ConfigKeySSLKey, &sslkey)
if err != nil {
return nil, err
}
sslcert := DefaultSSLCert
sslcert, err = config.String(ConfigKeySSLCert, &sslcert)
if err != nil {
return nil, err
}
sslrootcert := DefaultSSLCert
sslrootcert, err = config.String(ConfigKeySSLRootCert, &sslrootcert)
if err != nil {
return nil, err
}
port := DefaultPort
if port, err = config.Int(ConfigKeyPort, &port); err != nil {
return nil, err
}
maxcon := DefaultMaxConn
if maxcon, err = config.Int(ConfigKeyMaxConn, &maxcon); err != nil {
return nil, err
}
srid := DefaultSRID
if srid, err = config.Int(ConfigKeySRID, &srid); err != nil {
return nil, err
}
connConfig := pgx.ConnConfig{
Host: host,
Port: uint16(port),
Database: db,
User: user,
Password: password,
LogLevel: pgx.LogLevelWarn,
RuntimeParams: map[string]string{
"default_transaction_read_only": "TRUE",
"application_name": "tegola",
},
}
err = ConfigTLS(sslmode, sslkey, sslcert, sslrootcert, &connConfig)
if err != nil {
return nil, err
}
p := Provider{
srid: uint64(srid),
config: pgx.ConnPoolConfig{
ConnConfig: connConfig,
MaxConnections: int(maxcon),
},
}
if p.pool, err = pgx.NewConnPool(p.config); err != nil {
return nil, fmt.Errorf("Failed while creating connection pool: %v", err)
}
layers, err := config.MapSlice(ConfigKeyLayers)
if err != nil {
return nil, err
}
lyrs := make(map[string]Layer)
lyrsSeen := make(map[string]int)
for i, layer := range layers {
lname, err := layer.String(ConfigKeyLayerName, nil)
if err != nil {
return nil, fmt.Errorf("For layer (%v) we got the following error trying to get the layer's name field: %v", i, err)
}
if j, ok := lyrsSeen[lname]; ok {
return nil, fmt.Errorf("%v layer name is duplicated in both layer %v and layer %v", lname, i, j)
}
lyrsSeen[lname] = i
if i == 0 {
p.firstlayer = lname
}
fields, err := layer.StringSlice(ConfigKeyFields)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v %v field had the following error: %v", i, lname, ConfigKeyFields, err)
}
geomfld := "geom"
geomfld, err = layer.String(ConfigKeyGeomField, &geomfld)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v : %v", i, lname, err)
}
idfld := ""
idfld, err = layer.String(ConfigKeyGeomIDField, &idfld)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v : %v", i, lname, err)
}
if idfld == geomfld {
return nil, fmt.Errorf("for layer (%v) %v: %v (%v) and %v field (%v) is the same", i, lname, ConfigKeyGeomField, geomfld, ConfigKeyGeomIDField, idfld)
}
geomType := ""
geomType, err = layer.String(ConfigKeyGeomType, &geomType)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v : %v", i, lname, err)
}
var tblName string
tblName, err = layer.String(ConfigKeyTablename, &lname)
if err != nil {
return nil, fmt.Errorf("for %v layer (%v) %v has an error: %v", i, lname, ConfigKeyTablename, err)
}
var sql string
sql, err = layer.String(ConfigKeySQL, &sql)
if err != nil {
return nil, fmt.Errorf("for %v layer (%v) %v has an error: %v", i, lname, ConfigKeySQL, err)
}
if tblName != lname && sql != "" {
log.Printf("both %v and %v field are specified for layer (%v) %v, using only %[2]v field.", ConfigKeyTablename, ConfigKeySQL, i, lname)
}
var lsrid = srid
if lsrid, err = layer.Int(ConfigKeySRID, &lsrid); err != nil {
return nil, err
}
l := Layer{
name: lname,
idField: idfld,
geomField: geomfld,
srid: uint64(lsrid),
}
if sql != "" && !isSelectQuery.MatchString(sql) {
// if it is not a SELECT query, then we assume we have a sub-query
// (`(select ...) as foo`) which we can handle like a tablename
tblName = sql
sql = ""
}
if sql != "" {
// convert !BOX! (MapServer) and !bbox! (Mapnik) to !BBOX! for compatibility
sql := strings.Replace(strings.Replace(sql, "!BOX!", "!BBOX!", -1), "!bbox!", "!BBOX!", -1)
// make sure that the sql has a !BBOX! token
if !strings.Contains(sql, bboxToken) {
return nil, fmt.Errorf("SQL for layer (%v) %v is missing required token: %v", i, lname, bboxToken)
}
if !strings.Contains(sql, "*") {
if !strings.Contains(sql, geomfld) {
return nil, fmt.Errorf("SQL for layer (%v) %v does not contain the geometry field: %v", i, lname, geomfld)
}
if !strings.Contains(sql, idfld) {
return nil, fmt.Errorf("SQL for layer (%v) %v does not contain the id field for the geometry: %v", i, lname, sql)
}
}
l.sql = sql
} else {
// Tablename and Fields will be used to build the query.
// We need to do some work. We need to check to see Fields contains the geom and gid fields
// and if not add them to the list. If Fields list is empty/nil we will use '*' for the field list.
l.sql, err = genSQL(&l, p.pool, tblName, fields, true, providerType)
if err != nil {
return nil, fmt.Errorf("could not generate sql, for layer(%v): %v", lname, err)
}
}
if debugLayerSQL {
log.Printf("SQL for Layer(%v):\n%v\n", lname, l.sql)
}
// set the layer geom type
if geomType != "" {
if err = p.setLayerGeomType(&l, geomType); err != nil {
return nil, fmt.Errorf("error fetching geometry type for layer (%v): %v", l.name, err)
}
} else {
if err = p.inspectLayerGeomType(&l); err != nil {
return nil, fmt.Errorf("error fetching geometry type for layer (%v): %v", l.name, err)
}
}
lyrs[lname] = l
}
p.layers = lyrs
// track the provider so we can clean it up later
providers = append(providers, p)
return &p, nil
}
// derived from github.com/jackc/pgx configTLS (https://github.com/jackc/pgx/blob/master/conn.go)
func ConfigTLS(sslMode string, sslKey string, sslCert string, sslRootCert string, cc *pgx.ConnConfig) error {
switch sslMode {
case "disable":
cc.UseFallbackTLS = false
cc.TLSConfig = nil
cc.FallbackTLSConfig = nil
return nil
case "allow":
cc.UseFallbackTLS = true
cc.FallbackTLSConfig = &tls.Config{InsecureSkipVerify: true}
case "prefer":
cc.TLSConfig = &tls.Config{InsecureSkipVerify: true}
cc.UseFallbackTLS = true
cc.FallbackTLSConfig = nil
case "require":
cc.TLSConfig = &tls.Config{InsecureSkipVerify: true}
case "verify-ca", "verify-full":
cc.TLSConfig = &tls.Config{
ServerName: cc.Host,
}
default:
return ErrInvalidSSLMode(sslMode)
}
if sslRootCert != "" {
caCertPool := x509.NewCertPool()
caCert, err := ioutil.ReadFile(sslRootCert)
if err != nil {
return fmt.Errorf("unable to read CA file (%q): %v", sslRootCert, err)
}
if !caCertPool.AppendCertsFromPEM(caCert) {
return fmt.Errorf("unable to add CA to cert pool")
}
cc.TLSConfig.RootCAs = caCertPool
cc.TLSConfig.ClientCAs = caCertPool
}
if (sslCert == "") != (sslKey == "") {
return fmt.Errorf("both 'sslcert' and 'sslkey' are required")
} else if sslCert != "" { // we must have both now
cert, err := tls.LoadX509KeyPair(sslCert, sslKey)
if err != nil {
return fmt.Errorf("unable to read cert: %v", err)
}
cc.TLSConfig.Certificates = []tls.Certificate{cert}
}
return nil
}
// setLayerGeomType sets the geomType field on the layer to one of point,
// linestring, polygon, multipoint, multilinestring, multipolygon or
// geometrycollection
func (p Provider) setLayerGeomType(l *Layer, geomType string) error {
switch strings.ToLower(geomType) {
case "point":
l.geomType = geom.Point{}
case "linestring":
l.geomType = geom.LineString{}
case "polygon":
l.geomType = geom.Polygon{}
case "multipoint":
l.geomType = geom.MultiPoint{}
case "multilinestring":
l.geomType = geom.MultiLineString{}
case "multipolygon":
l.geomType = geom.MultiPolygon{}
case "geometrycollection":
l.geomType = geom.Collection{}
default:
return fmt.Errorf("unsupported geometry_type (%v) for layer (%v)", geomType, l.name)
}
return nil
}
// inspectLayerGeomType sets the geomType field on the layer by running the SQL
// and reading the geom type in the result set
func (p Provider) inspectLayerGeomType(l *Layer) error {
var err error
// we want to know the geom type instead of returning the geom data so we modify the SQL
// TODO (arolek): this strategy wont work if remove the requirement of wrapping ST_AsBinary(geom) in the SQL statements.
//
// https://github.com/go-spatial/tegola/issues/180
//
// case insensitive search
re := regexp.MustCompile(`(?i)ST_AsBinary`)
sql := re.ReplaceAllString(l.sql, "ST_GeometryType")
re = regexp.MustCompile(`(?i)(ST_AsMVTGeom\(.*\))`)
if re.MatchString(sql) {
sql = fmt.Sprintf("SELECT ST_GeometryType(%v) FROM (%v) as q", l.geomField, sql)
}
// we only need a single result set to sniff out the geometry type
sql = fmt.Sprintf("%v LIMIT 1", sql)
// if a !ZOOM! token exists, all features could be filtered out so we don't have a geometry to inspect it's type.
// address this by replacing the !ZOOM! token with an ANY statement which includes all zooms
sql = strings.Replace(sql, "!ZOOM!", "ANY('{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}')", 1)
// we need a tile to run our sql through the replacer
tile := provider.NewTile(0, 0, 0, 64, tegola.WebMercator)
// normal replacer
sql, err = replaceTokens(sql, l, tile, true)
if err != nil {
return err
}
rows, err := p.pool.Query(sql)
if err != nil {
return err
}
defer rows.Close()
// fetch rows FieldDescriptions. this gives us the OID for the data types returned to aid in decoding
fdescs := rows.FieldDescriptions()
for rows.Next() {
vals, err := rows.Values()
if err != nil {
return fmt.Errorf("error running SQL: %v ; %v", sql, err)
}
// iterate the values returned from our row, sniffing for the geomField or st_geometrytype field name
for i, v := range vals {
switch fdescs[i].Name {
case l.geomField, "st_geometrytype":
switch v {
case "ST_Point":
l.geomType = geom.Point{}
case "ST_LineString":
l.geomType = geom.LineString{}
case "ST_Polygon":
l.geomType = geom.Polygon{}
case "ST_MultiPoint":
l.geomType = geom.MultiPoint{}
case "ST_MultiLineString":
l.geomType = geom.MultiLineString{}
case "ST_MultiPolygon":
l.geomType = geom.MultiPolygon{}
case "ST_GeometryCollection":
l.geomType = geom.Collection{}
default:
return fmt.Errorf("layer (%v) returned unsupported geometry type (%v)", l.name, v)
}
}
}
}
return rows.Err()
}
// Layer fetches an individual layer from the provider, if it's configured
// if no name is provider, the first layer is returned
func (p *Provider) Layer(name string) (Layer, bool) {
if name == "" {
return p.layers[p.firstlayer], true
}
layer, ok := p.layers[name]
return layer, ok
}
// Layers returns meta data about the various layers which are configured with the provider
func (p Provider) Layers() ([]provider.LayerInfo, error) {
var ls []provider.LayerInfo
for i := range p.layers {
ls = append(ls, p.layers[i])
}
return ls, nil
}
// TileFeatures adheres to the provider.Tiler interface
func (p Provider) TileFeatures(ctx context.Context, layer string, tile provider.Tile, fn func(f *provider.Feature) error) error {
// fetch the provider layer
plyr, ok := p.Layer(layer)
if !ok {
return ErrLayerNotFound{layer}
}
sql, err := replaceTokens(plyr.sql, &plyr, tile, true)
if err != nil {
return fmt.Errorf("error replacing layer tokens for layer (%v) SQL (%v): %v", layer, sql, err)
}
if debugExecuteSQL {
log.Printf("TEGOLA_SQL_DEBUG:EXECUTE_SQL for layer (%v): %v", layer, sql)
}
// context check
if err := ctx.Err(); err != nil {
return err
}
rows, err := p.pool.Query(sql)
if err != nil {
return fmt.Errorf("error running layer (%v) SQL (%v): %v", layer, sql, err)
}
defer rows.Close()
// fetch rows FieldDescriptions. this gives us the OID for the data types returned to aid in decoding
fdescs := rows.FieldDescriptions()
// loop our field descriptions looking for the geometry field
var geomFieldFound bool
for i := range fdescs {
if fdescs[i].Name == plyr.GeomFieldName() {
geomFieldFound = true
break
}
}
if !geomFieldFound {
return ErrGeomFieldNotFound{
GeomFieldName: plyr.GeomFieldName(),
LayerName: plyr.Name(),
}
}
reportedLayerFieldName := ""
for rows.Next() {
// context check
if err := ctx.Err(); err != nil {
return err
}
// fetch row values
vals, err := rows.Values()
if err != nil {
return fmt.Errorf("error running layer (%v) SQL (%v): %v", layer, sql, err)
}
gid, geobytes, tags, err := decipherFields(ctx, plyr.GeomFieldName(), plyr.IDFieldName(), fdescs, vals)
if err != nil {
switch err {
case context.Canceled:
return err
default:
return fmt.Errorf("for layer (%v) %v", plyr.Name(), err)
}
}
// check that we have geometry data. if not, skip the feature
if len(geobytes) == 0 {
continue
}
// decode our WKB
geometry, err := wkb.DecodeBytes(geobytes)
if err != nil {
switch err.(type) {
case wkb.ErrUnknownGeometryType:
rplfn := layer + ":" + plyr.GeomFieldName()
// Only report to the log once. This is to prevent the logs from filling up if there are many geometries in the layer
if reportedLayerFieldName == "" || reportedLayerFieldName == rplfn {
reportedLayerFieldName = rplfn
log.Printf("[WARNING] Ignoring unsupported geometry in layer (%v). Only basic 2D geometry type are supported. Try using `ST_Force2D(%v)`.", layer, plyr.GeomFieldName())
}
continue
default:
return fmt.Errorf("unable to decode layer (%v) geometry field (%v) into wkb where (%v = %v): %v", layer, plyr.GeomFieldName(), plyr.IDFieldName(), gid, err)
}
}
feature := provider.Feature{
ID: gid,
Geometry: geometry,
SRID: plyr.SRID(),
Tags: tags,
}
// pass the feature to the provided callback
if err = fn(&feature); err != nil {
return err
}
}
return rows.Err()
}
func (p Provider) MVTForLayers(ctx context.Context, tile provider.Tile, layers []provider.Layer) ([]byte, error) {
var (
err error
sqls = make([]string, 0, len(layers))
)
for i := range layers {
if debug {
log.Printf("looking for layer: %v", layers[i])
}
l, ok := p.Layer(layers[i].Name)
if !ok {
// Should we be erroring here, or have a flag so that we don't
// spam the user?
log.Printf("provider layer not found %v", layers[i].Name)
}
if debugLayerSQL {
log.Printf("SQL for Layer(%v):\n%v\n", l.Name(), l.sql)
}
sql, err := replaceTokens(l.sql, &l, tile, false)
if err != nil {
return nil, err
}
// ref: https://postgis.net/docs/ST_AsMVT.html
// bytea ST_AsMVT(anyelement row, text name, integer extent, text geom_name, text feature_id_name)
var featureIDName string
if l.IDFieldName() == "" {
featureIDName = "NULL"
} else {
featureIDName = fmt.Sprintf(`'%s'`, l.IDFieldName())
}
sqls = append(sqls, fmt.Sprintf(
`(SELECT ST_AsMVT(q,'%s',%d,'%s',%s) AS data FROM (%s) AS q)`,
layers[i].MVTName,
tegola.DefaultExtent,
l.GeomFieldName(),
featureIDName,
sql,
))
}
subsqls := strings.Join(sqls, "||")
fsql := fmt.Sprintf(`SELECT (%s) AS data`, subsqls)
var data pgtype.Bytea
if debugExecuteSQL {
log.Printf("%s:%s: %v", EnvSQLDebugName, EnvSQLDebugExecute, fsql)
}
err = p.pool.QueryRow(fsql).Scan(&data)
if debugExecuteSQL {
log.Printf("%s:%s: %v", EnvSQLDebugName, EnvSQLDebugExecute, fsql)
if err != nil {
log.Printf("%s:%s: returned error %v", EnvSQLDebugName, EnvSQLDebugExecute, err)
} else {
log.Printf("%s:%s: returned %v bytes", EnvSQLDebugName, EnvSQLDebugExecute, len(data.Bytes))
}
}
// data may have garbage in it.
if err != nil {
return []byte{}, err
}
return data.Bytes, nil
}
// Close will close the Provider's database connectio
func (p *Provider) Close() { p.pool.Close() }
// reference to all instantiated providers
var providers []Provider
// Cleanup will close all database connections and destroy all previously instantiated Provider instances
func Cleanup() {
if len(providers) > 0 {
log.Printf("cleaning up postgis providers")
}
for i := range providers {
providers[i].Close()
}
providers = make([]Provider, 0)
}