Better implementatin with switch and support for 0

This commit is contained in:
Florent AIDE 2015-11-23 18:45:31 +01:00
parent c4b5af094a
commit 9e9cba8a85

View file

@ -471,20 +471,29 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
func (d *Decimal) Scan(value interface{}) error {
var err error
// first try to see if the data is stored in database as a Numeric datatype
if val, ok := value.(float64); ok {
*d = NewFromFloat(val)
return err
}
switch value.(type) {
// then try to see if it is stored as a string
case float64:
// numeric in sqlite3 sends us float64
*d = NewFromFloat(value.(float64))
return err
case int64:
// at least in sqlite3 when the value is 0 in db, the data is sent
// to us as an int64 instead of a float64 ...
*d = NewFromFloat(float64(value.(int64)))
return err
default:
// default is trying to interpret value stored as string
str, err := unquoteIfQuoted(value)
if err != nil {
return err
}
*d, err = NewFromString(str)
return err
}
}
// Value implements the driver.Valuer interface for database serialization.
func (d Decimal) Value() (driver.Value, error) {