From 5f2507bf2794c3bb9ecc48072edb2e3c7708f1ac Mon Sep 17 00:00:00 2001 From: Willow Chargin Date: Mon, 1 Jul 2024 09:57:34 -0700 Subject: [PATCH] feat: add initial geography support --- pgxgeom.go | 12 +++++++++--- pgxgeom_test.go | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/pgxgeom.go b/pgxgeom.go index 43b7f78..ef3b85f 100644 --- a/pgxgeom.go +++ b/pgxgeom.go @@ -304,16 +304,22 @@ func (p textScanPlan) Scan(src []byte, target any) error { // Register registers a codec for [github.com/twpayne/go-geom.T] types on conn. func Register(ctx context.Context, conn *pgx.Conn) error { - var oid uint32 - err := conn.QueryRow(ctx, "select 'geometry'::text::regtype::oid").Scan(&oid) + 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 } + conn.TypeMap().RegisterType(&pgtype.Type{ + Codec: codec{}, + Name: "geography", + OID: geographyOID, + }) + conn.TypeMap().RegisterType(&pgtype.Type{ Codec: codec{}, Name: "geometry", - OID: oid, + OID: geometryOID, }) return nil diff --git a/pgxgeom_test.go b/pgxgeom_test.go index 97058d4..5482b25 100644 --- a/pgxgeom_test.go +++ b/pgxgeom_test.go @@ -125,7 +125,7 @@ func TestCodecDecodeNullGeometry(t *testing.T) { }) } -func TestCodecScanValue(t *testing.T) { +func TestCodecScanValueGeometry(t *testing.T) { defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) { tb.Helper() for _, format := range []int16{ @@ -142,6 +142,23 @@ func TestCodecScanValue(t *testing.T) { }) } +func TestCodecScanValueGeography(t *testing.T) { + defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) { + tb.Helper() + for _, format := range []int16{ + pgx.BinaryFormatCode, + pgx.TextFormatCode, + } { + tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) { + var geom geom.T + err := conn.QueryRow(ctx, "select ST_SetSRID('POINT(1 2)'::geography, 4326)", pgx.QueryResultFormats{format}).Scan(&geom) + assert.NoError(t, err) + assert.Equal(t, mustNewGeomFromWKT(t, "POINT(1 2)", 4326), geom) + }) + } + }) +} + func TestCodecScanValuePolymorphic(t *testing.T) { defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) { tb.Helper()