Skip to content

Commit

Permalink
feat: add support for NULL geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 28, 2024
1 parent 70a09a3 commit 3acc4f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pgxgeos.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ func (p *binaryScanPlan) Scan(src []byte, target any) error {
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pgeom = nil
return nil
}
geom, err := p.geosContext.NewGeomFromWKB(src)
if err != nil {
return err
Expand All @@ -154,6 +158,10 @@ func (p *textScanPlan) Scan(src []byte, target any) error {
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pgeom = nil
return nil
}
var err error
src, err = hex.DecodeString(string(src))
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pgxgeos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ func TestCodecDecodeValue(t *testing.T) {
}

func TestCodecDecodeNullValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()

type s struct {
Geom *geos.Geom `db:"geom"`
}

for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
tb.Helper()

rows, err := conn.Query(ctx, "select NULL::geometry AS geom", pgx.QueryResultFormats{format})
assert.NoError(tb, err)

value, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[s])
assert.NoError(t, err)
assert.Zero(t, value)
})
}
})
}

func TestCodecDecodeNullGeometry(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
rows, err := conn.Query(ctx, "select $1::geometry", nil)
Expand Down

0 comments on commit 3acc4f1

Please sign in to comment.