dialect/sql: support scanning extra nullable time columns (#4232)

This commit is contained in:
Ariel Mashraki
2024-10-05 11:03:53 +03:00
committed by GitHub
parent 0c74e8ee0d
commit 9627017062
18 changed files with 581 additions and 27 deletions

View File

@@ -161,6 +161,7 @@ func scanType(typ reflect.Type, columns []string) (*rowScan, error) {
}
var (
timeType = reflect.TypeOf(time.Time{})
scannerType = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
nullJSONType = reflect.TypeOf((*nullJSON)(nil)).Elem()
)
@@ -352,7 +353,7 @@ func ScanTypeOf(rows *Rows, i int) any {
rt = rt.Elem()
}
// Handle NULL values.
switch rt.Kind() {
switch k := rt.Kind(); k {
case reflect.Bool:
rt = reflect.TypeOf(sql.NullBool{})
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
@@ -362,6 +363,10 @@ func ScanTypeOf(rows *Rows, i int) any {
rt = reflect.TypeOf(sql.NullFloat64{})
case reflect.String:
rt = reflect.TypeOf(sql.NullString{})
default:
if k == reflect.Struct && rt == timeType {
rt = reflect.TypeOf(sql.NullTime{})
}
}
return reflect.New(rt).Interface()
}

View File

@@ -2450,7 +2450,6 @@ func TestQueryNodesSchema(t *testing.T) {
AddRow(1, 10, nil, nil, nil).
AddRow(2, 20, "", 0, 0).
AddRow(3, 30, "a8m", 1, 1))
var (
users []*user
spec = &QuerySpec{