Confusion about PostGIS types and pgtype #2221
peacefixation
started this conversation in
General
Replies: 1 comment
-
You can. I'm not sure why your seeing errors. I don't have PostGIS installed, but I created a simple test that shows it working with hstore, another extension type that isn't registered by default in pgx. package main
import (
"context"
"database/sql/driver"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v5"
)
type Hstore string
func (h *Hstore) Scan(src interface{}) error {
fmt.Println("Hstore.Scan() called")
*h = Hstore(src.(string))
return nil
}
func (h Hstore) Value() (driver.Value, error) {
fmt.Println("Hstore.Value() called")
return string(h), nil
}
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(ctx)
var result Hstore
err = conn.QueryRow(ctx, "select $1::hstore;", Hstore("foo => bar")).Scan(&result)
if err != nil {
log.Fatal(err)
}
log.Println(result)
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm new to
pgx
and learning how to deal with PostGIS geometry types.When I use
lib/pq
I can create a customGeometry
type in Go and implementsql.Scanner
anddriver.Valuer
interfaces to read and write PostGIS geometry types.I tried something similar with
pgx
and I get scan errors -pgx
doesn't recognize the PostGIS geometry types.After much reading I've discovered
pgtype
and also thego-geom
andpgx-geom
library. I have to callpgxgeom.Register()
to let thepgx.Conn
know about the PostGIS geometry types and I can now scan and value ageom.T
.I'm not sure why this is necessary, why can't I just implement
sql.Scanner
anddriver.Valuer
like I did withlib/pq
?Separately, if I do use
go-geom
andpgx-geom
I'd like to wrap thegeom.T
type in a struct so I don't have to exportgo-geom
types throughout the codebase - it's nice for the future if we change geometry libraries - but I don't think this is going to work unless I write my own code likepgx-geom
to register my custom Go type withpgx
.Am I misunderstanding how and why to use
pgtype
?Beta Was this translation helpful? Give feedback.
All reactions