mirror of https://github.com/go-gorm/gorm.git
Refactor scan into struct
This commit is contained in:
parent
f2edda50e1
commit
68bb5379d9
18
scan.go
18
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))
|
||||
|
|
Loading…
Reference in New Issue