Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch function Scan to support int,int8,int16,int32 (#1) #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,26 @@ func (d *Decimal) Scan(value interface{}) error {
*d = NewFromFloat(v)
return nil

case int:
// also better to implement to support all type of int.
// profitable to create another abstract layer for testing
// data access with no depends on real RDBMS.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this comment, it seems unnecessary.


*d = New(int64(v), 0)
return nil

case int8:
*d = New(int64(v), 0)
return nil

case int16:
*d = New(int64(v), 0)
return nil

case int32:
*d = New(int64(v), 0)
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 ...
Expand Down
60 changes: 60 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,66 @@ func TestDecimal_Scan(t *testing.T) {
if err == nil {
t.Errorf("a.Scan(Foo{}) should have thrown an error but did not")
}

dbvalueInt8 := int8(32)
expected = New(int64(32), 0)

err = a.Scan(dbvalueInt8)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan(32) 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)
}
}

dbvalueInt16 := int16(21)
expected = New(int64(21), 0)

err = a.Scan(dbvalueInt16)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan(21) 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)
}
}

dbvalueInt32 := int32(32)
expected = New(int64(32), 0)

err = a.Scan(dbvalueInt32)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan(32) 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)
}
}

dbvalueIntNatural := 3264
expected = New(int64(3264), 0)

err = a.Scan(dbvalueIntNatural)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan(3264) 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)
}
}
}

func TestDecimal_Value(t *testing.T) {
Expand Down