-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
5 changed files
with
183 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dolmen
Contributor
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@kardianos I don't think that this block works like you expect. You can't check if a value implements an interface just by checking the type in a type switch. Instead you have to explicitly do a type cast outside the switch (or in
default
):