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>
This commit is contained in:
Daniel Theophanes 2019-04-26 12:49:23 -07:00
parent dc63b59630
commit 683ffe09f6
5 changed files with 183 additions and 7 deletions

View file

@ -3606,7 +3606,7 @@ type nvcConn struct {
skipNamedValueCheck bool
}
type decimal struct {
type decimalInt struct {
value int
}
@ -3630,7 +3630,7 @@ func (c *nvcConn) CheckNamedValue(nv *driver.NamedValue) error {
nv.Value = "OUT:*string"
}
return nil
case decimal, []int64:
case decimalInt, []int64:
return nil
case doNotInclude:
return driver.ErrRemoveArgument
@ -3659,13 +3659,13 @@ func TestNamedValueChecker(t *testing.T) {
}
o1 := ""
_, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A,str1=?,out1=?O1,array1=?", Named("A", decimal{123}), "hello", Named("O1", Out{Dest: &o1}), []int64{42, 128, 707}, doNotInclude{})
_, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A,str1=?,out1=?O1,array1=?", Named("A", decimalInt{123}), "hello", Named("O1", Out{Dest: &o1}), []int64{42, 128, 707}, doNotInclude{})
if err != nil {
t.Fatal("exec insert", err)
}
var (
str1 string
dec1 decimal
dec1 decimalInt
arr1 []int64
)
err = db.QueryRowContext(ctx, "SELECT|keys|dec1,str1,array1|").Scan(&dec1, &str1, &arr1)
@ -3675,7 +3675,7 @@ func TestNamedValueChecker(t *testing.T) {
list := []struct{ got, want interface{} }{
{o1, "from-server"},
{dec1, decimal{123}},
{dec1, decimalInt{123}},
{str1, "hello"},
{arr1, []int64{42, 128, 707}},
}
@ -3708,7 +3708,7 @@ func TestNamedValueCheckerSkip(t *testing.T) {
t.Fatal("exec create", err)
}
_, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A", Named("A", decimal{123}))
_, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A", Named("A", decimalInt{123}))
if err == nil {
t.Fatalf("expected error with bad argument, got %v", err)
}