Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions fixedpoint/fixedpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package fixedpoint

import (
"database/sql/driver"
"math"
"math/big"
"strings"
Expand Down Expand Up @@ -414,3 +415,34 @@ 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add case decimal.NullDecimal, because why not

// Directly handle decimal.Decimal type
f.d.Decimal = v
f.d.Valid = true
return nil
case decimal.NullDecimal:
// Directly handle decimal.NullDecimal type
f.d = v
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()
}
Loading