From aeaca493cf412def7813d36fd6a68acc832bf79f Mon Sep 17 00:00:00 2001 From: Hasan Date: Tue, 8 Mar 2022 04:08:16 +0600 Subject: [PATCH] Added offset when scanning the result back to struct --- scan.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/scan.go b/scan.go index a4243d12..b749190a 100644 --- a/scan.go +++ b/scan.go @@ -156,10 +156,12 @@ func Scan(rows *sql.Rows, db *DB, mode ScanMode) { } default: var ( - fields = make([]*schema.Field, len(columns)) - joinFields [][2]*schema.Field - sch = db.Statement.Schema - reflectValue = db.Statement.ReflectValue + fields = make([]*schema.Field, len(columns)) + selectFields []*schema.Field + selectedColumnsMap = map[string]int{} + joinFields [][2]*schema.Field + sch = db.Statement.Schema + reflectValue = db.Statement.ReflectValue ) if reflectValue.Kind() == reflect.Interface { @@ -194,7 +196,23 @@ func Scan(rows *sql.Rows, db *DB, mode ScanMode) { if sch != nil { for idx, column := range columns { 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 { if rel, ok := sch.Relationships.Relations[names[0]]; ok { if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {