Convert float4 and float8 to Codec
This commit is contained in:
+18
-54
@@ -8,68 +8,29 @@ import (
|
||||
|
||||
type Float8 float64
|
||||
|
||||
func (dst *Float8) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Float8
|
||||
err := nullable.DecodeText(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
// ScanFloat64 implements the Float64Scanner interface.
|
||||
func (f *Float8) ScanFloat64(n pgtype.Float8) error {
|
||||
if !n.Valid {
|
||||
*f = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Float8(nullable.Float)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
*f = Float8(n.Float)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Float8) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Float8
|
||||
err := nullable.DecodeBinary(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
func (f Float8) Float64Value() (pgtype.Float8, error) {
|
||||
if f == 0 {
|
||||
return pgtype.Float8{}, nil
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Float8(nullable.Float)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Float8) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Float8{
|
||||
Float: float64(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeText(ci, buf)
|
||||
}
|
||||
|
||||
func (src Float8) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Float8{
|
||||
Float: float64(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeBinary(ci, buf)
|
||||
return pgtype.Float8{Float: float64(f), Valid: true}, nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Float8) Scan(src interface{}) error {
|
||||
func (f *Float8) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
*f = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -79,12 +40,15 @@ func (dst *Float8) Scan(src interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Float8(nullable.Float)
|
||||
*f = Float8(nullable.Float)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Float8) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
func (f Float8) Value() (driver.Value, error) {
|
||||
if f == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return float64(f), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user