diff --git a/decimal.go b/decimal.go index 049fad3..1a1ad37 100644 --- a/decimal.go +++ b/decimal.go @@ -631,6 +631,10 @@ func (d *Decimal) Scan(value interface{}) error { // first try to see if the data is stored in database as a Numeric datatype switch v := value.(type) { + case float32: + *d = NewFromFloat(float64(v)) + return nil + case float64: // numeric in sqlite3 sends us float64 *d = NewFromFloat(v) @@ -802,8 +806,8 @@ func unquoteIfQuoted(value interface{}) (string, error) { case []byte: bytes = v default: - return "", fmt.Errorf("Could not convert value '%+v' to byte array", - value) + return "", fmt.Errorf("Could not convert value '%+v' to byte array of type '%T'", + value, value) } // If the amount is quoted, strip the quotes diff --git a/decimal_test.go b/decimal_test.go index 35641e4..3d41e8f 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -1175,6 +1175,23 @@ func TestDecimal_Scan(t *testing.T) { } } + // apparently MySQL 5.7.16 and returns these as float32 so we need + // to handle these as well + dbvalueFloat32 := float32(54.33) + expected = NewFromFloat(float64(dbvalueFloat32)) + + err = a.Scan(dbvalueFloat32) + if err != nil { + // Scan failed... no need to test result value + t.Errorf("a.Scan(54.33) failed with message: %s", err) + + } else { + // Scan succeeded... test resulting values + if !a.Equal(expected) { + t.Errorf("%s does not equal to %s", a, expected) + } + } + // at least SQLite returns an int64 when 0 is stored in the db // and you specified a numeric format on the schema dbvalueInt := int64(0)