diff options
| author | Daniel Theophanes <kardianos@gmail.com> | 2019-04-26 12:49:23 -0700 |
|---|---|---|
| committer | Daniel Theophanes <kardianos@gmail.com> | 2019-06-13 16:59:01 +0000 |
| commit | 683ffe09f66f0834baf039deeabe16eec6b09daa (patch) | |
| tree | 46ca283a2cc566d294ff672b5391279087d7f6e3 /src/database/sql/convert.go | |
| parent | dc63b59630cbc7fe1b039757bac0d6f713dfc26d (diff) | |
| download | go-683ffe09f66f0834baf039deeabe16eec6b09daa.tar.xz | |
database/sql: add support for decimal interface
Add support for scanning decimal types into values. If the dest
supports the decimal composer interface and the src supports
the decimal decomposer, set the value of the decimal when Scanning.
Add support for sending decimal decomposer interface values
as parameters.
For #30870
Change-Id: Ic5dbf9069df8d56405852b17542a9188d55c2947
Reviewed-on: https://go-review.googlesource.com/c/go/+/174181
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/database/sql/convert.go')
| -rw-r--r-- | src/database/sql/convert.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/database/sql/convert.go b/src/database/sql/convert.go index 7a3b5fa7da..4c056a1eda 100644 --- a/src/database/sql/convert.go +++ b/src/database/sql/convert.go @@ -288,6 +288,11 @@ func convertAssignRows(dest, src interface{}, rows *Rows) error { *d = s.AppendFormat((*d)[:0], time.RFC3339Nano) return nil } + case decimalDecompose: + switch d := dest.(type) { + case decimalCompose: + return d.Compose(s.Decompose(nil)) + } case nil: switch d := dest.(type) { case *interface{}: @@ -553,3 +558,42 @@ func callValuerValue(vr driver.Valuer) (v driver.Value, err error) { } return vr.Value() } + +// decimal composes or decomposes a decimal value to and from individual parts. +// There are four parts: a boolean negative flag, a form byte with three possible states +// (finite=0, infinite=1, NaN=2), a base-2 big-endian integer +// coefficient (also known as a significand) as a []byte, and an int32 exponent. +// These are composed into a final value as "decimal = (neg) (form=finite) coefficient * 10 ^ exponent". +// A zero length coefficient is a zero value. +// The big-endian integer coefficent stores the most significant byte first (at coefficent[0]). +// If the form is not finite the coefficient and exponent should be ignored. +// The negative parameter may be set to true for any form, although implementations are not required +// to respect the negative parameter in the non-finite form. +// +// Implementations may choose to set the negative parameter to true on a zero or NaN value, +// but implementations that do not differentiate between negative and positive +// zero or NaN values should ignore the negative parameter without error. +// If an implementation does not support Infinity it may be converted into a NaN without error. +// If a value is set that is larger than what is supported by an implementation, +// an error must be returned. +// Implementations must return an error if a NaN or Infinity is attempted to be set while neither +// are supported. +// +// NOTE(kardianos): This is an experimental interface. See https://golang.org/issue/30870 +type decimal interface { + decimalDecompose + decimalCompose +} + +type decimalDecompose interface { + // Decompose returns the internal decimal state in parts. + // If the provided buf has sufficient capacity, buf may be returned as the coefficient with + // the value set and length set as appropriate. + Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32) +} + +type decimalCompose interface { + // Compose sets the internal decimal value from parts. If the value cannot be + // represented then an error should be returned. + Compose(form byte, negative bool, coefficient []byte, exponent int32) error +} |
