Skip to content

Cannot replace EndBracketsReg #659

@hexadecy

Description

@hexadecy

Current situation

We use postgist query like this: INSERT INTO data (map_id, coordinates) VALUES (:map_id, GeomFromEWKB(:coordinates))

But it returns an error because of the GeomFromEWKB function.

Console error

pq: syntax error at end of input

To reproduce

main.go

package main

import (
	"fmt"
	"log"

	"github.com/cridenour/go-postgis"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

type data struct {
	DataID int64          `db:"data_id"`
	MapID  int64          `db:"map_id"`
	Coord  postgis.PointS `db:"coordinates"`
}

func main() {
	db := sqlx.MustOpen("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
		"localhost", 5432, "user", "pass123", "db"))

	dd := []data{
		{MapID: 1, Coord: postgis.PointS{SRID: 4326, X: -117.16686667, Y: 36.42191667}},
		{MapID: 2, Coord: postgis.PointS{SRID: 4326, X: -120.16686667, Y: 37.42191667}}}

	query := `
	INSERT INTO data (map_id, coordinates)
	VALUES (:map_id, GeomFromEWKB(:coordinates))`

	_, err := db.NamedExec(query, dd)
	log.Println(err)
}

docker-compose.yaml

version: "3.8"

services:
  db:
    image: postgis/postgis:10-3.0
    environment:
      POSTGRES_DB: db
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass123
      POSTGRES_MULTIPLE_EXTENSIONS: postgis,hstore,postgis_topology,postgis_raster,pgrouting
    ports:
      - "5432:5432"

data.sql

CREATE TABLE data" (
	data_id bigserial NOT NULL,
	map_id int8 NOT NULL,
	coordinates geography(POINT,4326) NULL
)

go.mod

module sqlx-659

go 1.15

require (
    github.com/cridenour/go-postgis v1.0.0
    github.com/jmoiron/sqlx v1.2.1-0.20200615141059-0794cb1f47ee
    github.com/lib/pq v1.9.0
)

Workaround with the previous version only

originalRegex := sqlx.EndBracketsReg
sqlx.EndBracketsReg = regexp.MustCompile(`\([^()]*\)\s*$|\([^)]*(GeomFromEWKB\(\?\))+.*\)\s*$`)
defer func() {
   sqlx.EndBracketsReg = originalRegex
}()

Since PR #514, EndBacketsReg was replaced by valueBracketReg, but this variable is not public.

Possible fix

Find a universal regex or let the user inject a regexp for this particular case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions