1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
package pgtype
import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"strconv"
"github.com/jackc/pgx/v5/internal/pgio"
)
type Float64Scanner interface {
ScanFloat64(Float8) error
}
type Float64Valuer interface {
Float64Value() (Float8, error)
}
type Float8 struct {
Float64 float64
Valid bool
}
// ScanFloat64 implements the [Float64Scanner] interface.
func (f *Float8) ScanFloat64(n Float8) error {
*f = n
return nil
}
// Float64Value implements the [Float64Valuer] interface.
func (f Float8) Float64Value() (Float8, error) {
return f, nil
}
// ScanInt64 implements the [Int64Scanner] interface.
func (f *Float8) ScanInt64(n Int8) error {
*f = Float8{Float64: float64(n.Int64), Valid: n.Valid}
return nil
}
// Int64Value implements the [Int64Valuer] interface.
func (f Float8) Int64Value() (Int8, error) {
return Int8{Int64: int64(f.Float64), Valid: f.Valid}, nil
}
// Scan implements the [database/sql.Scanner] interface.
func (f *Float8) Scan(src any) error {
if src == nil {
*f = Float8{}
return nil
}
switch src := src.(type) {
case float64:
*f = Float8{Float64: src, Valid: true}
return nil
case string:
n, err := strconv.ParseFloat(string(src), 64)
if err != nil {
return err
}
*f = Float8{Float64: 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.Float64, nil
}
// MarshalJSON implements the [encoding/json.Marshaler] interface.
func (f Float8) MarshalJSON() ([]byte, error) {
if !f.Valid {
return []byte("null"), nil
}
return json.Marshal(f.Float64)
}
// UnmarshalJSON implements the [encoding/json.Unmarshaler] interface.
func (f *Float8) UnmarshalJSON(b []byte) error {
var n *float64
err := json.Unmarshal(b, &n)
if err != nil {
return err
}
if n == nil {
*f = Float8{}
} else {
*f = Float8{Float64: *n, Valid: true}
}
return nil
}
type Float8Codec struct{}
func (Float8Codec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (Float8Codec) PreferredFormat() int16 {
return BinaryFormatCode
}
func (Float8Codec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan {
switch format {
case BinaryFormatCode:
switch value.(type) {
case float64:
return encodePlanFloat8CodecBinaryFloat64{}
case Float64Valuer:
return encodePlanFloat8CodecBinaryFloat64Valuer{}
case Int64Valuer:
return encodePlanFloat8CodecBinaryInt64Valuer{}
}
case TextFormatCode:
switch value.(type) {
case float64:
return encodePlanTextFloat64{}
case Float64Valuer:
return encodePlanTextFloat64Valuer{}
case Int64Valuer:
return encodePlanTextInt64Valuer{}
}
}
return nil
}
type encodePlanFloat8CodecBinaryFloat64 struct{}
func (encodePlanFloat8CodecBinaryFloat64) Encode(value any, buf []byte) (newBuf []byte, err error) {
n := value.(float64)
return pgio.AppendUint64(buf, math.Float64bits(n)), nil
}
type encodePlanTextFloat64 struct{}
func (encodePlanTextFloat64) Encode(value any, 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 any, buf []byte) (newBuf []byte, err error) {
n, err := value.(Float64Valuer).Float64Value()
if err != nil {
return nil, err
}
if !n.Valid {
return nil, nil
}
return pgio.AppendUint64(buf, math.Float64bits(n.Float64)), nil
}
type encodePlanTextFloat64Valuer struct{}
func (encodePlanTextFloat64Valuer) Encode(value any, 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.Float64, 'f', -1, 64)...), nil
}
type encodePlanFloat8CodecBinaryInt64Valuer struct{}
func (encodePlanFloat8CodecBinaryInt64Valuer) Encode(value any, 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.Int64)
return pgio.AppendUint64(buf, math.Float64bits(f)), nil
}
type encodePlanTextInt64Valuer struct{}
func (encodePlanTextInt64Valuer) Encode(value any, 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.Int64, 10)...), nil
}
func (Float8Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
switch format {
case BinaryFormatCode:
switch target.(type) {
case *float64:
return scanPlanBinaryFloat8ToFloat64{}
case Float64Scanner:
return scanPlanBinaryFloat8ToFloat64Scanner{}
case Int64Scanner:
return scanPlanBinaryFloat8ToInt64Scanner{}
case TextScanner:
return scanPlanBinaryFloat8ToTextScanner{}
}
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(src []byte, dst any) error {
if src == 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(src []byte, dst any) 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{Float64: math.Float64frombits(uint64(n)), Valid: true})
}
type scanPlanBinaryFloat8ToInt64Scanner struct{}
func (scanPlanBinaryFloat8ToInt64Scanner) Scan(src []byte, dst any) 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{Int64: i64, Valid: true})
}
type scanPlanBinaryFloat8ToTextScanner struct{}
func (scanPlanBinaryFloat8ToTextScanner) Scan(src []byte, dst any) error {
s := (dst).(TextScanner)
if src == nil {
return s.ScanText(Text{})
}
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))
return s.ScanText(Text{String: strconv.FormatFloat(f64, 'f', -1, 64), Valid: true})
}
type scanPlanTextAnyToFloat64 struct{}
func (scanPlanTextAnyToFloat64) Scan(src []byte, dst any) error {
if src == nil {
return fmt.Errorf("cannot scan NULL into %T", dst)
}
n, err := strconv.ParseFloat(string(src), 64)
if err != nil {
return err
}
f := (dst).(*float64)
*f = n
return nil
}
type scanPlanTextAnyToFloat64Scanner struct{}
func (scanPlanTextAnyToFloat64Scanner) Scan(src []byte, dst any) error {
s := (dst).(Float64Scanner)
if src == nil {
return s.ScanFloat64(Float8{})
}
n, err := strconv.ParseFloat(string(src), 64)
if err != nil {
return err
}
return s.ScanFloat64(Float8{Float64: n, Valid: true})
}
func (c Float8Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) {
return c.DecodeValue(m, oid, format, src)
}
func (c Float8Codec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) {
if src == nil {
return nil, nil
}
var n float64
err := codecScan(c, m, oid, format, src, &n)
if err != nil {
return nil, err
}
return n, nil
}
|