Convert float4 and float8 to Codec
This commit is contained in:
+249
-196
@@ -10,178 +10,259 @@ import (
|
||||
"github.com/jackc/pgio"
|
||||
)
|
||||
|
||||
type Float64Scanner interface {
|
||||
ScanFloat64(Float8) error
|
||||
}
|
||||
|
||||
type Float64Valuer interface {
|
||||
Float64Value() (Float8, error)
|
||||
}
|
||||
|
||||
type Float8 struct {
|
||||
Float float64
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func (dst *Float8) Set(src interface{}) error {
|
||||
// ScanFloat64 implements the Float64Scanner interface.
|
||||
func (f *Float8) ScanFloat64(n Float8) error {
|
||||
*f = n
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Float8) Float64Value() (Float8, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (f *Float8) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = Float8{}
|
||||
*f = Float8{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if value, ok := src.(interface{ Get() interface{} }); ok {
|
||||
value2 := value.Get()
|
||||
if value2 != value {
|
||||
return dst.Set(value2)
|
||||
}
|
||||
}
|
||||
|
||||
switch value := src.(type) {
|
||||
case float32:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
switch src := src.(type) {
|
||||
case float64:
|
||||
*dst = Float8{Float: value, Valid: true}
|
||||
case int8:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case uint8:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case int16:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case uint16:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case int32:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case uint32:
|
||||
*dst = Float8{Float: float64(value), Valid: true}
|
||||
case int64:
|
||||
f64 := float64(value)
|
||||
if int64(f64) == value {
|
||||
*dst = Float8{Float: f64, Valid: true}
|
||||
} else {
|
||||
return fmt.Errorf("%v cannot be exactly represented as float64", value)
|
||||
}
|
||||
case uint64:
|
||||
f64 := float64(value)
|
||||
if uint64(f64) == value {
|
||||
*dst = Float8{Float: f64, Valid: true}
|
||||
} else {
|
||||
return fmt.Errorf("%v cannot be exactly represented as float64", value)
|
||||
}
|
||||
case int:
|
||||
f64 := float64(value)
|
||||
if int(f64) == value {
|
||||
*dst = Float8{Float: f64, Valid: true}
|
||||
} else {
|
||||
return fmt.Errorf("%v cannot be exactly represented as float64", value)
|
||||
}
|
||||
case uint:
|
||||
f64 := float64(value)
|
||||
if uint(f64) == value {
|
||||
*dst = Float8{Float: f64, Valid: true}
|
||||
} else {
|
||||
return fmt.Errorf("%v cannot be exactly represented as float64", value)
|
||||
}
|
||||
*f = Float8{Float: src, Valid: true}
|
||||
return nil
|
||||
case string:
|
||||
num, err := strconv.ParseFloat(value, 64)
|
||||
n, err := strconv.ParseFloat(string(src), 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = Float8{Float: float64(num), Valid: true}
|
||||
case *float64:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
*f = Float8{Float: n, Valid: true}
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot scan %T", src)
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (f Float8) Value() (driver.Value, error) {
|
||||
if !f.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return f.Float, nil
|
||||
}
|
||||
|
||||
type Float8Codec struct{}
|
||||
|
||||
func (Float8Codec) FormatSupported(format int16) bool {
|
||||
return format == TextFormatCode || format == BinaryFormatCode
|
||||
}
|
||||
|
||||
func (Float8Codec) PreferredFormat() int16 {
|
||||
return BinaryFormatCode
|
||||
}
|
||||
|
||||
func (Float8Codec) PlanEncode(ci *ConnInfo, oid uint32, format int16, value interface{}) EncodePlan {
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
switch value.(type) {
|
||||
case float64:
|
||||
return encodePlanFloat8CodecBinaryFloat64{}
|
||||
case Float64Valuer:
|
||||
return encodePlanFloat8CodecBinaryFloat64Valuer{}
|
||||
case Int64Valuer:
|
||||
return encodePlanFloat8CodecBinaryInt64Valuer{}
|
||||
}
|
||||
case *float32:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
case TextFormatCode:
|
||||
switch value.(type) {
|
||||
case float64:
|
||||
return encodePlanTextFloat64{}
|
||||
case Float64Valuer:
|
||||
return encodePlanTextFloat64Valuer{}
|
||||
case Int64Valuer:
|
||||
return encodePlanTextInt64Valuer{}
|
||||
}
|
||||
case *int8:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *uint8:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *int16:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *uint16:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *int32:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *uint32:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *int64:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *uint64:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *int:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *uint:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *string:
|
||||
if value == nil {
|
||||
*dst = Float8{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
default:
|
||||
if originalSrc, ok := underlyingNumberType(src); ok {
|
||||
return dst.Set(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Float8", value)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst Float8) Get() interface{} {
|
||||
if !dst.Valid {
|
||||
return nil
|
||||
type encodePlanFloat8CodecBinaryFloat64 struct{}
|
||||
|
||||
func (encodePlanFloat8CodecBinaryFloat64) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n := value.(float64)
|
||||
return pgio.AppendUint64(buf, math.Float64bits(n)), nil
|
||||
}
|
||||
|
||||
type encodePlanTextFloat64 struct{}
|
||||
|
||||
func (encodePlanTextFloat64) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n := value.(float64)
|
||||
return append(buf, strconv.FormatFloat(n, 'f', -1, 64)...), nil
|
||||
}
|
||||
|
||||
type encodePlanFloat8CodecBinaryFloat64Valuer struct{}
|
||||
|
||||
func (encodePlanFloat8CodecBinaryFloat64Valuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n, err := value.(Float64Valuer).Float64Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dst.Float
|
||||
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return pgio.AppendUint64(buf, math.Float64bits(n.Float)), nil
|
||||
}
|
||||
|
||||
func (src *Float8) AssignTo(dst interface{}) error {
|
||||
return float64AssignTo(src.Float, src.Valid, dst)
|
||||
type encodePlanTextFloat64Valuer struct{}
|
||||
|
||||
func (encodePlanTextFloat64Valuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n, err := value.(Float64Valuer).Float64Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return append(buf, strconv.FormatFloat(n.Float, 'f', -1, 64)...), nil
|
||||
}
|
||||
|
||||
func (dst *Float8) DecodeText(ci *ConnInfo, src []byte) error {
|
||||
type encodePlanFloat8CodecBinaryInt64Valuer struct{}
|
||||
|
||||
func (encodePlanFloat8CodecBinaryInt64Valuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n, err := value.(Int64Valuer).Int64Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
f := float64(n.Int)
|
||||
return pgio.AppendUint64(buf, math.Float64bits(f)), nil
|
||||
}
|
||||
|
||||
type encodePlanTextInt64Valuer struct{}
|
||||
|
||||
func (encodePlanTextInt64Valuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
n, err := value.(Int64Valuer).Int64Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return append(buf, strconv.FormatInt(n.Int, 10)...), nil
|
||||
}
|
||||
|
||||
func (Float8Codec) PlanScan(ci *ConnInfo, oid uint32, format int16, target interface{}, actualTarget bool) ScanPlan {
|
||||
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
switch target.(type) {
|
||||
case *float64:
|
||||
return scanPlanBinaryFloat8ToFloat64{}
|
||||
case Float64Scanner:
|
||||
return scanPlanBinaryFloat8ToFloat64Scanner{}
|
||||
case Int64Scanner:
|
||||
return scanPlanBinaryFloat8ToInt64Scanner{}
|
||||
}
|
||||
case TextFormatCode:
|
||||
switch target.(type) {
|
||||
case *float64:
|
||||
return scanPlanTextAnyToFloat64{}
|
||||
case Float64Scanner:
|
||||
return scanPlanTextAnyToFloat64Scanner{}
|
||||
case Int64Scanner:
|
||||
return scanPlanTextAnyToInt64Scanner{}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type scanPlanBinaryFloat8ToFloat64 struct{}
|
||||
|
||||
func (scanPlanBinaryFloat8ToFloat64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
if src == nil {
|
||||
*dst = Float8{}
|
||||
return nil
|
||||
return fmt.Errorf("cannot scan null into %T", dst)
|
||||
}
|
||||
|
||||
if len(src) != 8 {
|
||||
return fmt.Errorf("invalid length for float8: %v", len(src))
|
||||
}
|
||||
|
||||
n := int64(binary.BigEndian.Uint64(src))
|
||||
f := (dst).(*float64)
|
||||
*f = math.Float64frombits(uint64(n))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type scanPlanBinaryFloat8ToFloat64Scanner struct{}
|
||||
|
||||
func (scanPlanBinaryFloat8ToFloat64Scanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
s := (dst).(Float64Scanner)
|
||||
|
||||
if src == nil {
|
||||
return s.ScanFloat64(Float8{})
|
||||
}
|
||||
|
||||
if len(src) != 8 {
|
||||
return fmt.Errorf("invalid length for float8: %v", len(src))
|
||||
}
|
||||
|
||||
n := int64(binary.BigEndian.Uint64(src))
|
||||
return s.ScanFloat64(Float8{Float: math.Float64frombits(uint64(n)), Valid: true})
|
||||
}
|
||||
|
||||
type scanPlanBinaryFloat8ToInt64Scanner struct{}
|
||||
|
||||
func (scanPlanBinaryFloat8ToInt64Scanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
s := (dst).(Int64Scanner)
|
||||
|
||||
if src == nil {
|
||||
return s.ScanInt64(Int8{})
|
||||
}
|
||||
|
||||
if len(src) != 8 {
|
||||
return fmt.Errorf("invalid length for float8: %v", len(src))
|
||||
}
|
||||
|
||||
ui64 := int64(binary.BigEndian.Uint64(src))
|
||||
f64 := math.Float64frombits(uint64(ui64))
|
||||
i64 := int64(f64)
|
||||
if f64 != float64(i64) {
|
||||
return fmt.Errorf("cannot losslessly convert %v to int64", f64)
|
||||
}
|
||||
|
||||
return s.ScanInt64(Int8{Int: i64, Valid: true})
|
||||
}
|
||||
|
||||
type scanPlanTextAnyToFloat64 struct{}
|
||||
|
||||
func (scanPlanTextAnyToFloat64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
if src == nil {
|
||||
return fmt.Errorf("cannot scan null into %T", dst)
|
||||
}
|
||||
|
||||
n, err := strconv.ParseFloat(string(src), 64)
|
||||
@@ -189,70 +270,42 @@ func (dst *Float8) DecodeText(ci *ConnInfo, src []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Float8{Float: n, Valid: true}
|
||||
f := (dst).(*float64)
|
||||
*f = n
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Float8) DecodeBinary(ci *ConnInfo, src []byte) error {
|
||||
type scanPlanTextAnyToFloat64Scanner struct{}
|
||||
|
||||
func (scanPlanTextAnyToFloat64Scanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
s := (dst).(Float64Scanner)
|
||||
|
||||
if src == nil {
|
||||
*dst = Float8{}
|
||||
return nil
|
||||
return s.ScanFloat64(Float8{})
|
||||
}
|
||||
|
||||
if len(src) != 8 {
|
||||
return fmt.Errorf("invalid length for float4: %v", len(src))
|
||||
n, err := strconv.ParseFloat(string(src), 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n := int64(binary.BigEndian.Uint64(src))
|
||||
|
||||
*dst = Float8{Float: math.Float64frombits(uint64(n)), Valid: true}
|
||||
return nil
|
||||
return s.ScanFloat64(Float8{Float: n, Valid: true})
|
||||
}
|
||||
|
||||
func (src Float8) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
buf = append(buf, strconv.FormatFloat(float64(src.Float), 'f', -1, 64)...)
|
||||
return buf, nil
|
||||
func (c Float8Codec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
|
||||
return c.DecodeValue(ci, oid, format, src)
|
||||
}
|
||||
|
||||
func (src Float8) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
buf = pgio.AppendUint64(buf, math.Float64bits(src.Float))
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Float8) Scan(src interface{}) error {
|
||||
func (c Float8Codec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
|
||||
if src == nil {
|
||||
*dst = Float8{}
|
||||
return nil
|
||||
}
|
||||
|
||||
switch src := src.(type) {
|
||||
case float64:
|
||||
*dst = Float8{Float: src, Valid: true}
|
||||
return nil
|
||||
case string:
|
||||
return dst.DecodeText(nil, []byte(src))
|
||||
case []byte:
|
||||
srcCopy := make([]byte, len(src))
|
||||
copy(srcCopy, src)
|
||||
return dst.DecodeText(nil, srcCopy)
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot scan %T", src)
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Float8) Value() (driver.Value, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return src.Float, nil
|
||||
|
||||
var n float64
|
||||
err := codecScan(c, ci, oid, format, src, &n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user