From b316dbd0ff68be8ec2330bbd34c4488a9910e94d Mon Sep 17 00:00:00 2001 From: MisterSquishy Date: Mon, 5 Dec 2022 22:24:31 -0500 Subject: [PATCH] ignore, not coalesce --- src/database/sql/convert.go | 16 ++++------------ src/database/sql/sql_test.go | 10 +++++----- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/database/sql/convert.go b/src/database/sql/convert.go index 04432492cf9..eb0fe9dbb44 100644 --- a/src/database/sql/convert.go +++ b/src/database/sql/convert.go @@ -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. @@ -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. diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go index 036731ae8d0..d97546b60ed 100644 --- a/src/database/sql/sql_test.go +++ b/src/database/sql/sql_test.go @@ -1496,7 +1496,6 @@ func TestCursorFake(t *testing.T) { } func TestInvalidNilValues(t *testing.T) { - coalesceNullValuesToZero = false var date1 time.Time var date2 int @@ -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 @@ -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)