database/sql: check if src is nil before converting to string

A nil src (NULL database value) will result in a "nil" string,
which will never parse correctly in a ParseInt or similar
numeric conversion. The resulting error is confusing. Check
for a nil src prior to converting the value to string
if the resulting string will be parsed after that.

Closes #31274

Change-Id: I90f12cceff00fbbfdd3e343b04fa7e2596390e6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/174177
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Daniel Theophanes 2019-04-26 11:09:51 -07:00
parent 2ae5e7f9bf
commit 1805f425ad
2 changed files with 13 additions and 1 deletions

View file

@ -1401,7 +1401,7 @@ func TestInvalidNilValues(t *testing.T) {
{
name: "int",
input: &date2,
expectedError: `sql: Scan error on column index 0, name "bdate": converting driver.Value type <nil> ("<nil>") to a int: invalid syntax`,
expectedError: `sql: Scan error on column index 0, name "bdate": converting NULL to int is unsupported`,
},
}