Skip to content

Commit

Permalink
Merge pull request #4 from wchargin/geography
Browse files Browse the repository at this point in the history
Add initial geography support
  • Loading branch information
twpayne authored Jul 1, 2024
2 parents 8f38929 + 5f2507b commit ae49173
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 @@ -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
Expand Down
19 changes: 18 additions & 1 deletion pgxgeom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
Expand Down

0 comments on commit ae49173

Please sign in to comment.