Skip to content

Commit

Permalink
Merge pull request #2 from twpayne/fix-1
Browse files Browse the repository at this point in the history
feat: handle NULL geometries
  • Loading branch information
twpayne authored May 28, 2024
2 parents 85b6e06 + 743fcd2 commit 4c8fbda
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pgxgeom.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (p binaryScanPlan) Scan(src []byte, target any) error {
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pg = nil
return nil
}
g, err := ewkb.Unmarshal(src)
if err != nil {
return err
Expand All @@ -168,6 +172,10 @@ func (p textScanPlan) Scan(src []byte, target any) error {
if !ok {
return errors.ErrUnsupported
}
if len(src) == 0 {
*pg = nil
return nil
}
var err error
src, err = hex.DecodeString(string(src))
if err != nil {
Expand Down
28 changes: 27 additions & 1 deletion pgxgeom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,33 @@ 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()
rows, err := conn.Query(ctx, "select $1::geometry", nil)

type s struct {
Geom geom.T `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 NULL::geometry", pgx.QueryResultFormats{pgx.BinaryFormatCode})
assert.NoError(tb, err)

for rows.Next() {
Expand Down

0 comments on commit 4c8fbda

Please sign in to comment.