From 77e6b496f6c093115cb88318c1666a304989793a Mon Sep 17 00:00:00 2001 From: Victor Quinn Date: Tue, 21 Feb 2017 13:34:52 -0500 Subject: [PATCH] Fix same issue as #39 but without changing the Value() interface --- decimal.go | 8 ++++++-- decimal_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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)