Skip to content

Commit

Permalink
ignore, not coalesce
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterSquishy committed Dec 6, 2022
1 parent 8d5819b commit b316dbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
16 changes: 4 additions & 12 deletions src/database/sql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ func convertAssign(dest, src any) error {
return convertAssignRows(dest, src, nil)
}

// coalesceNullValuesToZero determines whether to interpret null values from SQL as the
// equivalent zero value in go
var coalesceNullValuesToZero = false
// ignoreNullValues determines whether to ignore null values in SQL
var ignoreNullValues = false

// convertAssignRows copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
Expand Down Expand Up @@ -319,15 +318,8 @@ func convertAssignRows(dest, src any, rows *Rows) error {
*d = nil
return nil
default:
if coalesceNullValuesToZero {
dpv := reflect.ValueOf(d)
if dpv.Kind() == reflect.Pointer {
dv := reflect.Indirect(dpv)
dv.Set(reflect.Zero(dv.Type()))
return nil
} else {
return errors.New("destination not a pointer")
}
if ignoreNullValues {
return nil
}
}
// The driver is returning a cursor the client may iterate over.
Expand Down
10 changes: 5 additions & 5 deletions src/database/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,6 @@ func TestCursorFake(t *testing.T) {
}

func TestInvalidNilValues(t *testing.T) {
coalesceNullValuesToZero = false

var date1 time.Time
var date2 int
Expand Down Expand Up @@ -1549,7 +1548,8 @@ func TestInvalidNilValues(t *testing.T) {
}

func TestValidNilValues(t *testing.T) {
coalesceNullValuesToZero = true
ignoreNullValues = true
defer func() { ignoreNullValues = false }()

var date1 time.Time
var int1 int
Expand Down Expand Up @@ -1616,12 +1616,12 @@ func TestValidNilValues(t *testing.T) {
conn.dc.ci.(*fakeConn).skipDirtySession = true
defer conn.Close()

zeroVal := reflect.Zero(reflect.Indirect(reflect.ValueOf(tt.input)).Type())
originalValue := tt.input
err = conn.QueryRowContext(ctx, "SELECT|people|bdate|age=?", 1).Scan(tt.input)
if err != nil {
t.Fatalf("expected no error when querying nil column, but get %s", err.Error())
} else if !reflect.Indirect(reflect.ValueOf(tt.input)).Equal(zeroVal) {
t.Fatalf("expected scan to coalesce to zero value %v, but got %v", zeroVal, reflect.Indirect(reflect.ValueOf(tt.input)))
} else if tt.input != originalValue {
t.Fatalf("expected null scan to preserve original value %v, but got %v", originalValue, tt.input)
}

err = conn.PingContext(ctx)
Expand Down

0 comments on commit b316dbd

Please sign in to comment.