diff options
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 +} |
