1
0
mirror of https://github.com/golang/go synced 2024-11-25 08:47:56 -07:00

ignore, not coalesce

This commit is contained in:
MisterSquishy 2022-12-05 22:24:31 -05:00
parent 8d5819b808
commit b316dbd0ff
2 changed files with 9 additions and 17 deletions

View File

@ -212,9 +212,8 @@ func convertAssign(dest, src any) error {
return convertAssignRows(dest, src, nil) return convertAssignRows(dest, src, nil)
} }
// coalesceNullValuesToZero determines whether to interpret null values from SQL as the // ignoreNullValues determines whether to ignore null values in SQL
// equivalent zero value in go var ignoreNullValues = false
var coalesceNullValuesToZero = false
// convertAssignRows copies to dest the value in src, converting it if possible. // 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. // 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 *d = nil
return nil return nil
default: default:
if coalesceNullValuesToZero { if ignoreNullValues {
dpv := reflect.ValueOf(d) return nil
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")
}
} }
} }
// The driver is returning a cursor the client may iterate over. // The driver is returning a cursor the client may iterate over.

View File

@ -1496,7 +1496,6 @@ func TestCursorFake(t *testing.T) {
} }
func TestInvalidNilValues(t *testing.T) { func TestInvalidNilValues(t *testing.T) {
coalesceNullValuesToZero = false
var date1 time.Time var date1 time.Time
var date2 int var date2 int
@ -1549,7 +1548,8 @@ func TestInvalidNilValues(t *testing.T) {
} }
func TestValidNilValues(t *testing.T) { func TestValidNilValues(t *testing.T) {
coalesceNullValuesToZero = true ignoreNullValues = true
defer func() { ignoreNullValues = false }()
var date1 time.Time var date1 time.Time
var int1 int var int1 int
@ -1616,12 +1616,12 @@ func TestValidNilValues(t *testing.T) {
conn.dc.ci.(*fakeConn).skipDirtySession = true conn.dc.ci.(*fakeConn).skipDirtySession = true
defer conn.Close() 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) err = conn.QueryRowContext(ctx, "SELECT|people|bdate|age=?", 1).Scan(tt.input)
if err != nil { if err != nil {
t.Fatalf("expected no error when querying nil column, but get %s", err.Error()) t.Fatalf("expected no error when querying nil column, but get %s", err.Error())
} else if !reflect.Indirect(reflect.ValueOf(tt.input)).Equal(zeroVal) { } else if tt.input != originalValue {
t.Fatalf("expected scan to coalesce to zero value %v, but got %v", zeroVal, reflect.Indirect(reflect.ValueOf(tt.input))) t.Fatalf("expected null scan to preserve original value %v, but got %v", originalValue, tt.input)
} }
err = conn.PingContext(ctx) err = conn.PingContext(ctx)