Skip to content

Commit

Permalink
fix null values for non-nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
stepansergeevitch committed Oct 7, 2022
1 parent 6f739b6 commit 747ec99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func parseValue(columnType string, val interface{}) (driver.Value, error) {
suffix = ")"
)

// No need to parse type if the value is nil
if val == nil {
return nil, nil
}

if strings.HasPrefix(columnType, arrayPrefix) && strings.HasSuffix(columnType, suffix) {
s := reflect.ValueOf(val)
res := make([]driver.Value, s.Len())
Expand All @@ -178,10 +183,6 @@ func parseValue(columnType string, val interface{}) (driver.Value, error) {
} else if strings.HasPrefix(columnType, decimalPrefix) && strings.HasSuffix(columnType, suffix) {
return parseSingleValue("Float64", val)
} else if strings.HasPrefix(columnType, nullablePrefix) && strings.HasSuffix(columnType, suffix) {
if val == nil {
return nil, nil
}

return parseSingleValue(columnType[len(nullablePrefix):len(columnType)-len(suffix)], val)
}

Expand Down
4 changes: 2 additions & 2 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func mockRows(isMultiStatement bool) driver.RowsNextResultSet {
"\"data\":[" +
" [null,1,0.312321,123213.321321,\"text\", \"2080-12-31\",\"1989-04-15 01:02:03\",1,[1,2,3],[[]]]," +
" [2,1,0.312321,123213.321321,\"text\",\"1970-01-01\",\"1970-01-01 00:00:00\",1,[1,2,3],[[]]]," +
" [3,5,0.312321,123213.321321,\"text\",\"1970-01-01\",\"1970-01-01 00:00:00\",1,[5,2,3,2],[[\"TEST\",\"TEST1\"],[\"TEST3\"]]]]," +
" [3,null,0.312321,123213.321321,\"text\",\"1970-01-01\",\"1970-01-01 00:00:00\",1,[5,2,3,2],[[\"TEST\",\"TEST1\"],[\"TEST3\"]]]]," +
"\"rows\":3," +
"\"statistics\":{" +
" \"elapsed\":0.001797702," +
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestRowsNext(t *testing.T) {
assert(err == nil, t, "Next shouldn't return an error")

assert(dest[0] == int32(3), t, "results not equal for int32")
assert(dest[1] == int64(5), t, "results not equal for int64")
assert(dest[1] == nil, t, "results not equal for int64")
assert(dest[2] == float32(0.312321), t, "results not equal for float32")
assert(dest[3] == float64(123213.321321), t, "results not equal for float64")
assert(dest[4] == "text", t, "results not equal for string")
Expand Down

0 comments on commit 747ec99

Please sign in to comment.