From faa9ae8be8c7bd5b146ffe9e0a73cae729fed5f3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 6 Jul 2021 14:11:52 +0800 Subject: [PATCH] Improve code --- convert.go | 25 +++++++++++++------------ scan.go | 15 ++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/convert.go b/convert.go index ba834a8d..f7d733ad 100644 --- a/convert.go +++ b/convert.go @@ -698,10 +698,8 @@ var ( // NullUint64 implements the Scanner interface so // it can be used as a scan destination, similar to NullString. type NullUint64 struct { - Uint64 uint64 - Valid bool // Valid is true if Uint64 is not NULL - OriginalLocation *time.Location - ConvertedLocation *time.Location + Uint64 uint64 + Valid bool } // Scan implements the Scanner interface. @@ -711,8 +709,9 @@ func (n *NullUint64) Scan(value interface{}) error { return nil } n.Valid = true - fmt.Println("======44444") - return convertAssign(&n.Uint64, value, n.OriginalLocation, n.ConvertedLocation) + var err error + n.Uint64, err = asUint64(value) + return err } // Value implements the driver Valuer interface. @@ -731,10 +730,8 @@ var ( // NullUint32 implements the Scanner interface so // it can be used as a scan destination, similar to NullString. type NullUint32 struct { - Uint32 uint32 - Valid bool // Valid is true if Uint32 is not NULL - OriginalLocation *time.Location - ConvertedLocation *time.Location + Uint32 uint32 + Valid bool // Valid is true if Uint32 is not NULL } // Scan implements the Scanner interface. @@ -744,8 +741,12 @@ func (n *NullUint32) Scan(value interface{}) error { return nil } n.Valid = true - fmt.Println("555555") - return convertAssign(&n.Uint32, value, n.OriginalLocation, n.ConvertedLocation) + i64, err := asUint64(value) + if err != nil { + return err + } + n.Uint32 = uint32(i64) + return nil } // Value implements the driver Valuer interface. diff --git a/scan.go b/scan.go index 7b242ec9..c5cb77ff 100644 --- a/scan.go +++ b/scan.go @@ -16,7 +16,7 @@ import ( ) // 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) { case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString, *sql.RawBytes: return t, false, nil @@ -29,15 +29,9 @@ func genScanResultsByBeanNullable(bean interface{}, originalLocation, convertedL case *int64: return &sql.NullInt64{}, true, nil case *uint, *uint8, *uint16, *uint32: - return &NullUint32{ - OriginalLocation: originalLocation, - ConvertedLocation: convertedLocation, - }, true, nil + return &NullUint32{}, true, nil case *uint64: - return &NullUint64{ - OriginalLocation: originalLocation, - ConvertedLocation: convertedLocation, - }, true, nil + return &NullUint64{}, true, nil case *float32, *float64: return &sql.NullFloat64{}, true, nil case *bool: @@ -57,7 +51,6 @@ func genScanResultsByBeanNullable(bean interface{}, originalLocation, convertedL tp := reflect.TypeOf(bean).Elem() switch tp.Kind() { case reflect.String: - fmt.Println("=====", tp) return &sql.NullString{}, true, nil case reflect.Int64: return &sql.NullInt64{}, true, nil @@ -197,7 +190,7 @@ func (engine *Engine) scan(rows *core.Rows, fields []string, types []*sql.Column } if useNullable { - scanResult, replaced, err = genScanResultsByBeanNullable(v, engine.DatabaseTZ, engine.TZLocation) + scanResult, replaced, err = genScanResultsByBeanNullable(v) } else { scanResult, replaced, err = genScanResultsByBean(v) }