Refactor scan into struct

This commit is contained in:
Jinzhu 2022-02-27 09:09:29 +08:00
parent f2edda50e1
commit 68bb5379d9
1 changed files with 6 additions and 12 deletions

18
scan.go
View File

@ -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))