From c4b5af094a73f8d8c4b35e3a905bf9df33d2e4ba Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Mon, 23 Nov 2015 16:43:59 +0100 Subject: [PATCH 1/6] Added support for Numeric type in db storage --- decimal.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/decimal.go b/decimal.go index e8d99b4..42acf30 100644 --- a/decimal.go +++ b/decimal.go @@ -469,6 +469,14 @@ func (d Decimal) MarshalJSON() ([]byte, error) { // Scan implements the sql.Scanner interface for database deserialization. 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 + } + + // then try to see if it is stored as a string str, err := unquoteIfQuoted(value) if err != nil { return err From 9e9cba8a855f8289b77e6e4b13443166aa29e8e3 Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Mon, 23 Nov 2015 18:45:31 +0100 Subject: [PATCH 2/6] Better implementatin with switch and support for 0 --- decimal.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/decimal.go b/decimal.go index 42acf30..1a53440 100644 --- a/decimal.go +++ b/decimal.go @@ -471,19 +471,28 @@ 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) + switch value.(type) { + + 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 } - - // then try to see if it is stored as a 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. From 28bb8ff9f579e0cfba5d94161d1d9b6ae7875fce Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Thu, 3 Dec 2015 11:06:49 +0100 Subject: [PATCH 3/6] use directly the int64 instead of casting to float --- decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index 1a53440..8dbc062 100644 --- a/decimal.go +++ b/decimal.go @@ -481,7 +481,7 @@ func (d *Decimal) Scan(value interface{}) error { 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))) + *d = New(value.(int64), 0) return err default: From c30673fb5f7b29a4cfab0f1e27098e0860bcec64 Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Fri, 11 Dec 2015 06:34:52 +0100 Subject: [PATCH 4/6] Use an idiomatic type switch for Scan type comparison --- decimal.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/decimal.go b/decimal.go index 8dbc062..fd2f4c5 100644 --- a/decimal.go +++ b/decimal.go @@ -471,22 +471,22 @@ 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 - switch value.(type) { + switch v := value.(type) { case float64: // numeric in sqlite3 sends us float64 - *d = NewFromFloat(value.(float64)) + *d = NewFromFloat(v) 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 = New(value.(int64), 0) + *d = New(v, 0) return err default: // default is trying to interpret value stored as string - str, err := unquoteIfQuoted(value) + str, err := unquoteIfQuoted(v) if err != nil { return err } From b873aa2b431b3ff78acd919b777b0cfe49664ce6 Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Fri, 11 Dec 2015 06:37:27 +0100 Subject: [PATCH 5/6] Return nil instead of empty err in Scan --- decimal.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/decimal.go b/decimal.go index fd2f4c5..cbbce7b 100644 --- a/decimal.go +++ b/decimal.go @@ -469,20 +469,19 @@ func (d Decimal) MarshalJSON() ([]byte, error) { // Scan implements the sql.Scanner interface for database deserialization. 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 switch v := value.(type) { case float64: // numeric in sqlite3 sends us float64 *d = NewFromFloat(v) - return err + return nil 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 = New(v, 0) - return err + return nil default: // default is trying to interpret value stored as string From faca378c8a8c3b9e4b1668db106c4cc69099d6aa Mon Sep 17 00:00:00 2001 From: Florent AIDE Date: Fri, 11 Dec 2015 19:46:00 +0100 Subject: [PATCH 6/6] Adding tests from the Scan method --- decimal_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/decimal_test.go b/decimal_test.go index 5202234..e560ffb 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -813,6 +813,67 @@ func TestDecimal_Max(t *testing.T) { } } +func TestDecimal_Scan(t *testing.T) { + // test the Scan method the implements the + // sql.Scanner interface + // check for the for different type of values + // that are possible to be received from the database + // drivers + + // in normal operations the db driver (sqlite at least) + // will return an int64 if you specified a numeric format + a := Decimal{} + dbvalue := float64(54.33) + expected := NewFromFloat(dbvalue) + + err := a.Scan(dbvalue) + if err != nil { + // Scan failed... no need to test result value + t.Errorf("a.Scan(54.33) failed with message: %s", err) + + } else { + // Scan suceeded... test resulting values + if !a.Equals(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 + dbvalue_int := int64(0) + expected = New(dbvalue_int, 0) + + err = a.Scan(dbvalue_int) + if err != nil { + // Scan failed... no need to test result value + t.Errorf("a.Scan(0) failed with message: %s", err) + + } else { + // Scan suceeded... test resulting values + if !a.Equals(expected) { + t.Errorf("%s does not equal to %s", a, expected) + } + } + + // in case you specified a varchar in your SQL schema, + // the database driver will return byte slice []byte + value_str := "535.666" + dbvalue_str := []byte(value_str) + expected, err = NewFromString(value_str) + + err = a.Scan(dbvalue_str) + if err != nil { + // Scan failed... no need to test result value + t.Errorf("a.Scan('535.666') failed with message: %s", err) + + } else { + // Scan suceeded... test resulting values + if !a.Equals(expected) { + t.Errorf("%s does not equal to %s", a, expected) + } + } +} + // old tests after this line func TestDecimal_Scale(t *testing.T) {