Skip to content

Commit

Permalink
Add initial geography support
Browse files Browse the repository at this point in the history
This follows the form of a recent commit to `pgx-geos` for the same
functionality.
  • Loading branch information
wchargin committed Jul 1, 2024
1 parent 4c8fbda commit 591b4f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 9 additions & 3 deletions pgxgeom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion pgxgeom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down

0 comments on commit 591b4f8

Please sign in to comment.