mirror of
https://github.com/golang/go
synced 2024-11-21 21:14:47 -07:00
ignore, not coalesce
This commit is contained in:
parent
8d5819b808
commit
b316dbd0ff
@ -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.
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user