Improve code

This commit is contained in:
Lunny Xiao 2021-07-06 14:11:52 +08:00
parent a56f7cf5eb
commit faa9ae8be8
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 17 additions and 23 deletions

View File

@ -699,9 +699,7 @@ var (
// it can be used as a scan destination, similar to NullString. // it can be used as a scan destination, similar to NullString.
type NullUint64 struct { type NullUint64 struct {
Uint64 uint64 Uint64 uint64
Valid bool // Valid is true if Uint64 is not NULL Valid bool
OriginalLocation *time.Location
ConvertedLocation *time.Location
} }
// Scan implements the Scanner interface. // Scan implements the Scanner interface.
@ -711,8 +709,9 @@ func (n *NullUint64) Scan(value interface{}) error {
return nil return nil
} }
n.Valid = true n.Valid = true
fmt.Println("======44444") var err error
return convertAssign(&n.Uint64, value, n.OriginalLocation, n.ConvertedLocation) n.Uint64, err = asUint64(value)
return err
} }
// Value implements the driver Valuer interface. // Value implements the driver Valuer interface.
@ -733,8 +732,6 @@ var (
type NullUint32 struct { type NullUint32 struct {
Uint32 uint32 Uint32 uint32
Valid bool // Valid is true if Uint32 is not NULL Valid bool // Valid is true if Uint32 is not NULL
OriginalLocation *time.Location
ConvertedLocation *time.Location
} }
// Scan implements the Scanner interface. // Scan implements the Scanner interface.
@ -744,8 +741,12 @@ func (n *NullUint32) Scan(value interface{}) error {
return nil return nil
} }
n.Valid = true n.Valid = true
fmt.Println("555555") i64, err := asUint64(value)
return convertAssign(&n.Uint32, value, n.OriginalLocation, n.ConvertedLocation) if err != nil {
return err
}
n.Uint32 = uint32(i64)
return nil
} }
// Value implements the driver Valuer interface. // Value implements the driver Valuer interface.

15
scan.go
View File

@ -16,7 +16,7 @@ import (
) )
// genScanResultsByBeanNullabale generates scan result // genScanResultsByBeanNullabale generates scan result
func genScanResultsByBeanNullable(bean interface{}, originalLocation, convertedLocation *time.Location) (interface{}, bool, error) { func genScanResultsByBeanNullable(bean interface{}) (interface{}, bool, error) {
switch t := bean.(type) { switch t := bean.(type) {
case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString, *sql.RawBytes: case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString, *sql.RawBytes:
return t, false, nil return t, false, nil
@ -29,15 +29,9 @@ func genScanResultsByBeanNullable(bean interface{}, originalLocation, convertedL
case *int64: case *int64:
return &sql.NullInt64{}, true, nil return &sql.NullInt64{}, true, nil
case *uint, *uint8, *uint16, *uint32: case *uint, *uint8, *uint16, *uint32:
return &NullUint32{ return &NullUint32{}, true, nil
OriginalLocation: originalLocation,
ConvertedLocation: convertedLocation,
}, true, nil
case *uint64: case *uint64:
return &NullUint64{ return &NullUint64{}, true, nil
OriginalLocation: originalLocation,
ConvertedLocation: convertedLocation,
}, true, nil
case *float32, *float64: case *float32, *float64:
return &sql.NullFloat64{}, true, nil return &sql.NullFloat64{}, true, nil
case *bool: case *bool:
@ -57,7 +51,6 @@ func genScanResultsByBeanNullable(bean interface{}, originalLocation, convertedL
tp := reflect.TypeOf(bean).Elem() tp := reflect.TypeOf(bean).Elem()
switch tp.Kind() { switch tp.Kind() {
case reflect.String: case reflect.String:
fmt.Println("=====", tp)
return &sql.NullString{}, true, nil return &sql.NullString{}, true, nil
case reflect.Int64: case reflect.Int64:
return &sql.NullInt64{}, true, nil return &sql.NullInt64{}, true, nil
@ -197,7 +190,7 @@ func (engine *Engine) scan(rows *core.Rows, fields []string, types []*sql.Column
} }
if useNullable { if useNullable {
scanResult, replaced, err = genScanResultsByBeanNullable(v, engine.DatabaseTZ, engine.TZLocation) scanResult, replaced, err = genScanResultsByBeanNullable(v)
} else { } else {
scanResult, replaced, err = genScanResultsByBean(v) scanResult, replaced, err = genScanResultsByBean(v)
} }