diff --git a/pgxgeom.go b/pgxgeom.go index c610771..0769def 100644 --- a/pgxgeom.go +++ b/pgxgeom.go @@ -191,16 +191,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 15749de..b7994b1 100644 --- a/pgxgeom_test.go +++ b/pgxgeom_test.go @@ -98,7 +98,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{ @@ -115,6 +115,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).SetSRID(4326).ToEWKBWithSRID(), geom.ToEWKBWithSRID()) + }) + } + }) +} + func mustEWKB(tb testing.TB, g geom.T) []byte { tb.Helper() data, err := ewkb.Marshal(g, binary.LittleEndian)