mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Added code to scan null values from the database
This commit is contained in:
parent
5471f2c322
commit
25f639b9c8
2 changed files with 23 additions and 1 deletions
|
@ -579,7 +579,14 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
|
|||
|
||||
// Scan implements the sql.Scanner interface for database deserialization.
|
||||
func (d *Decimal) Scan(value interface{}) error {
|
||||
// first try to see if the data is stored in database as a Numeric datatype
|
||||
// check to see if we have a nil value, return a zero value and exp
|
||||
if value == nil {
|
||||
d.value = zeroInt
|
||||
d.exp = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
// try to see if the data is stored in database as a Numeric datatype
|
||||
switch v := value.(type) {
|
||||
|
||||
case float64:
|
||||
|
|
|
@ -1117,6 +1117,21 @@ func TestDecimal_Max(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDecimal_ScanNil(t *testing.T) {
|
||||
a := Decimal{}
|
||||
expected := Decimal{
|
||||
value: zeroInt,
|
||||
exp: 0,
|
||||
}
|
||||
err := a.Scan(nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !a.Equal(expected) {
|
||||
t.Errorf("%s does not equal to %s", a, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimal_Scan(t *testing.T) {
|
||||
// test the Scan method that implements the
|
||||
// sql.Scanner interface
|
||||
|
|
Loading…
Reference in a new issue