From 68bb5379d91a7f7fae4dc65205db66004f515d0c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Feb 2022 09:09:29 +0800 Subject: [PATCH] Refactor scan into struct --- scan.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/scan.go b/scan.go index a1cb582e..e83390ca 100644 --- a/scan.go +++ b/scan.go @@ -68,7 +68,11 @@ func (db *DB) scanIntoStruct(sch *schema.Schema, rows *sql.Rows, reflectValue re values[idx] = &sql.RawBytes{} } else if len(columns) == 1 { sch = nil - values[idx] = reflectValue.Interface() + if reflectValue.CanAddr() { + values[idx] = reflectValue.Addr().Interface() + } else { + values[idx] = reflectValue.Interface() + } } else { values[idx] = &sql.RawBytes{} } @@ -272,17 +276,7 @@ func Scan(rows *sql.Rows, db *DB, mode ScanMode) { } case reflect.Struct, reflect.Ptr: if initialized || rows.Next() { - if update { - db.scanIntoStruct(sch, rows, reflectValue, values, columns, fields, joinFields) - } else { - elem := reflect.New(reflectValueType) - db.scanIntoStruct(sch, rows, elem, values, columns, fields, joinFields) - if isPtr { - db.Statement.ReflectValue.Set(elem) - } else { - db.Statement.ReflectValue.Set(elem.Elem()) - } - } + db.scanIntoStruct(sch, rows, reflectValue, values, columns, fields, joinFields) } default: db.AddError(rows.Scan(dest))