From fe3353bbb16016d0b8e0dae5eaac07845cd2f49a Mon Sep 17 00:00:00 2001 From: tgrziminiar Date: Fri, 20 Dec 2024 12:18:17 +0700 Subject: [PATCH] feat: method Scan, Value --- fixedpoint/fixedpoint.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/fixedpoint/fixedpoint.go b/fixedpoint/fixedpoint.go index c2268ff..de54e09 100644 --- a/fixedpoint/fixedpoint.go +++ b/fixedpoint/fixedpoint.go @@ -2,6 +2,7 @@ package fixedpoint import ( + "database/sql/driver" "math" "math/big" "strings" @@ -414,3 +415,30 @@ func (f FixedPoint) IsOverPrecision(precision int) bool { precisionCutoff := NewFromBigIntExp(big.NewInt(10), int32(-precision)) return f.Abs().Cmp(precisionCutoff) < 0 } + +// Scan implements the sql.Scanner interface for database deserialization. +func (f *FixedPoint) Scan(value interface{}) error { + if value == nil { + f.d.Valid = false + return nil + } + + switch v := value.(type) { + case decimal.Decimal: + // Directly handle decimal.Decimal type + f.d.Decimal = v + f.d.Valid = true + return nil + default: + // Use the normal NullDecimal scan for other types + return f.d.Scan(value) + } +} + +// Value implements the driver.Valuer interface for database serialization. +func (f FixedPoint) Value() (driver.Value, error) { + if f.IsValid() { + return nil, nil + } + return f.d.Decimal.Value() +}