aboutsummaryrefslogtreecommitdiffstats
path: root/common/big.go
diff options
context:
space:
mode:
authorMeng-Ying Yang <garfield@dexon.org>2018-12-25 16:51:08 +0800
committerWei-Ning Huang <w@dexon.org>2018-12-28 14:15:46 +0800
commit61c39fb69fcd3ff4bef350cbde150805c6e85bac (patch)
tree67063fb9b1ce31e1826818a70dc86f054e140bfe /common/big.go
parent245e0179da9d74cb71a0d9d67b0a9b41a3a6b6bd (diff)
downloaddexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar.gz
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar.bz2
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar.lz
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar.xz
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.tar.zst
dexon-61c39fb69fcd3ff4bef350cbde150805c6e85bac.zip
core: add database/sql support for more types (#102)
* core: types: add database/sql support for BlockNonce * common: add database/sql support with Big New Big type is declared to let big.Int support database/sql by implementing Scan() and Value() on new type.
Diffstat (limited to 'common/big.go')
-rw-r--r--common/big.go37
1 files changed, 36 insertions, 1 deletions
diff --git a/common/big.go b/common/big.go
index 65d4377bf..dcc5269a7 100644
--- a/common/big.go
+++ b/common/big.go
@@ -16,7 +16,11 @@
package common
-import "math/big"
+import (
+ "database/sql/driver"
+ "fmt"
+ "math/big"
+)
// Common big integers often used
var (
@@ -28,3 +32,34 @@ var (
Big256 = big.NewInt(256)
Big257 = big.NewInt(257)
)
+
+// Big support database/sql Scan and Value.
+type Big big.Int
+
+// Scan implements Scanner for database/sql.
+func (b *Big) Scan(src interface{}) error {
+ newB := new(big.Int)
+ switch t := src.(type) {
+ case int64:
+ *b = Big(*newB.SetInt64(t))
+ case uint64:
+ *b = Big(*newB.SetUint64(t))
+ case []byte:
+ *b = Big(*newB.SetBytes(t))
+ case string:
+ v, ok := newB.SetString(t, 10)
+ if !ok {
+ return fmt.Errorf("invalid string format %v", src)
+ }
+ *b = Big(*v)
+ default:
+ return fmt.Errorf("can't scan %T into Big", src)
+ }
+ return nil
+}
+
+// Value implements valuer for database/sql.
+func (b Big) Value() (driver.Value, error) {
+ b2 := big.Int(b)
+ return (&b2).String(), nil
+}