Fix Box, Circle, and Point NULL

query-exec-mode
Jack Christensen 2022-01-08 13:29:47 -06:00
parent 58d2d8e453
commit 6a6878bafd
7 changed files with 43 additions and 0 deletions

View File

@ -50,6 +50,10 @@ func (dst *Box) Scan(src interface{}) error {
// Value implements the database/sql/driver Valuer interface.
func (src Box) Value() (driver.Value, error) {
if !src.Valid {
return nil, nil
}
buf, err := BoxCodec{}.PlanEncode(nil, 0, TextFormatCode, src).Encode(src, nil)
if err != nil {
return nil, err
@ -90,6 +94,10 @@ func (p *encodePlanBoxCodecBinary) Encode(value interface{}, buf []byte) (newBuf
return nil, err
}
if !box.Valid {
return nil, nil
}
buf = pgio.AppendUint64(buf, math.Float64bits(box.P[0].X))
buf = pgio.AppendUint64(buf, math.Float64bits(box.P[0].Y))
buf = pgio.AppendUint64(buf, math.Float64bits(box.P[1].X))
@ -105,6 +113,10 @@ func (p *encodePlanBoxCodecText) Encode(value interface{}, buf []byte) (newBuf [
return nil, err
}
if !box.Valid {
return nil, nil
}
buf = append(buf, fmt.Sprintf(`(%s,%s),(%s,%s)`,
strconv.FormatFloat(box.P[0].X, 'f', -1, 64),
strconv.FormatFloat(box.P[0].Y, 'f', -1, 64),

View File

@ -31,6 +31,7 @@ func TestBoxCodec(t *testing.T) {
Valid: true,
}),
},
{pgtype.Box{}, new(pgtype.Box), isExpectedEq(pgtype.Box{})},
{nil, new(pgtype.Box), isExpectedEq(pgtype.Box{})},
})
}

View File

@ -51,6 +51,10 @@ func (dst *Circle) Scan(src interface{}) error {
// Value implements the database/sql/driver Valuer interface.
func (src Circle) Value() (driver.Value, error) {
if !src.Valid {
return nil, nil
}
buf, err := CircleCodec{}.PlanEncode(nil, 0, TextFormatCode, src).Encode(src, nil)
if err != nil {
return nil, err
@ -91,6 +95,10 @@ func (p *encodePlanCircleCodecBinary) Encode(value interface{}, buf []byte) (new
return nil, err
}
if !circle.Valid {
return nil, nil
}
buf = pgio.AppendUint64(buf, math.Float64bits(circle.P.X))
buf = pgio.AppendUint64(buf, math.Float64bits(circle.P.Y))
buf = pgio.AppendUint64(buf, math.Float64bits(circle.R))
@ -105,6 +113,10 @@ func (p *encodePlanCircleCodecText) Encode(value interface{}, buf []byte) (newBu
return nil, err
}
if !circle.Valid {
return nil, nil
}
buf = append(buf, fmt.Sprintf(`<(%s,%s),%s>`,
strconv.FormatFloat(circle.P.X, 'f', -1, 64),
strconv.FormatFloat(circle.P.Y, 'f', -1, 64),

View File

@ -18,6 +18,7 @@ func TestCircleTranscode(t *testing.T) {
new(pgtype.Circle),
isExpectedEq(pgtype.Circle{P: pgtype.Vec2{1.234, 5.67890123}, R: 3.5, Valid: true}),
},
{pgtype.Circle{}, new(pgtype.Circle), isExpectedEq(pgtype.Circle{})},
{nil, new(pgtype.Circle), isExpectedEq(pgtype.Circle{})},
})
}

View File

@ -949,6 +949,10 @@ func codecScan(codec Codec, ci *ConnInfo, oid uint32, format int16, src []byte,
}
func codecDecodeToTextFormat(codec Codec, ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
if src == nil {
return nil, nil
}
if format == TextFormatCode {
return string(src), nil
} else {

View File

@ -85,6 +85,10 @@ func (dst *Point) Scan(src interface{}) error {
// Value implements the database/sql/driver Valuer interface.
func (src Point) Value() (driver.Value, error) {
if !src.Valid {
return nil, nil
}
buf, err := PointCodec{}.PlanEncode(nil, 0, TextFormatCode, src).Encode(src, nil)
if err != nil {
return nil, err
@ -146,6 +150,10 @@ func (p *encodePlanPointCodecBinary) Encode(value interface{}, buf []byte) (newB
return nil, err
}
if !point.Valid {
return nil, nil
}
buf = pgio.AppendUint64(buf, math.Float64bits(point.P.X))
buf = pgio.AppendUint64(buf, math.Float64bits(point.P.Y))
return buf, nil
@ -159,6 +167,10 @@ func (p *encodePlanPointCodecText) Encode(value interface{}, buf []byte) (newBuf
return nil, err
}
if !point.Valid {
return nil, nil
}
return append(buf, fmt.Sprintf(`(%s,%s)`,
strconv.FormatFloat(point.P.X, 'f', -1, 64),
strconv.FormatFloat(point.P.Y, 'f', -1, 64),

View File

@ -20,6 +20,7 @@ func TestPointCodec(t *testing.T) {
new(pgtype.Point),
isExpectedEq(pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true}),
},
{pgtype.Point{}, new(pgtype.Point), isExpectedEq(pgtype.Point{})},
{nil, new(pgtype.Point), isExpectedEq(pgtype.Point{})},
})
}