mirror of https://github.com/go-gorm/gorm.git
Added offset when scanning the result back to struct
This commit is contained in:
parent
b566ed7913
commit
aeaca493cf
28
scan.go
28
scan.go
|
@ -156,10 +156,12 @@ func Scan(rows *sql.Rows, db *DB, mode ScanMode) {
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
var (
|
var (
|
||||||
fields = make([]*schema.Field, len(columns))
|
fields = make([]*schema.Field, len(columns))
|
||||||
joinFields [][2]*schema.Field
|
selectFields []*schema.Field
|
||||||
sch = db.Statement.Schema
|
selectedColumnsMap = map[string]int{}
|
||||||
reflectValue = db.Statement.ReflectValue
|
joinFields [][2]*schema.Field
|
||||||
|
sch = db.Statement.Schema
|
||||||
|
reflectValue = db.Statement.ReflectValue
|
||||||
)
|
)
|
||||||
|
|
||||||
if reflectValue.Kind() == reflect.Interface {
|
if reflectValue.Kind() == reflect.Interface {
|
||||||
|
@ -194,7 +196,23 @@ func Scan(rows *sql.Rows, db *DB, mode ScanMode) {
|
||||||
if sch != nil {
|
if sch != nil {
|
||||||
for idx, column := range columns {
|
for idx, column := range columns {
|
||||||
if field := sch.LookUpField(column); field != nil && field.Readable {
|
if field := sch.LookUpField(column); field != nil && field.Readable {
|
||||||
fields[idx] = field
|
selectFields = sch.Fields
|
||||||
|
offset := 0
|
||||||
|
if index, ok := selectedColumnsMap[column]; ok {
|
||||||
|
offset = index + 1
|
||||||
|
selectFields = selectFields[offset:]
|
||||||
|
}
|
||||||
|
|
||||||
|
for fieldIndex, selectField := range selectFields {
|
||||||
|
if selectField.DBName == column {
|
||||||
|
fields[idx] = selectField
|
||||||
|
selectedColumnsMap[column] = offset + fieldIndex
|
||||||
|
|
||||||
|
if selectField.Readable {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if names := strings.Split(column, "__"); len(names) > 1 {
|
} else if names := strings.Split(column, "__"); len(names) > 1 {
|
||||||
if rel, ok := sch.Relationships.Relations[names[0]]; ok {
|
if rel, ok := sch.Relationships.Relations[names[0]]; ok {
|
||||||
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
|
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
|
||||||
|
|
Loading…
Reference in New Issue