From 726b3b8c9f0c28b4621f746ea521c6ca2dbc55ae Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 19 Jan 2015 15:50:21 +0800 Subject: [PATCH] Don't raise scan error on column index --- callback_query.go | 31 ++++++++++++++++++++++++------- main_private.go | 8 +------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/callback_query.go b/callback_query.go index 81b191e7..4cdebedb 100644 --- a/callback_query.go +++ b/callback_query.go @@ -47,6 +47,7 @@ func Query(scope *Scope) { } defer rows.Close() + columns, _ := rows.Columns() for rows.Next() { anyRecordFound = true elem := dest @@ -54,19 +55,35 @@ func Query(scope *Scope) { elem = reflect.New(destType).Elem() } - columns, _ := rows.Columns() - var values []interface{} + var values = make([]interface{}, len(columns)) + fields := scope.New(elem.Addr().Interface()).Fields() - for _, value := range columns { - if field, ok := fields[value]; ok { - values = append(values, field.Field.Addr().Interface()) + for index, column := range columns { + if field, ok := fields[column]; ok { + if field.Field.Kind() == reflect.Ptr { + values[index] = field.Field.Addr().Interface() + } else { + values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface() + } } else { - var ignore interface{} - values = append(values, &ignore) + var value interface{} + values[index] = &value } } + scope.Err(rows.Scan(values...)) + for index, column := range columns { + value := values[index] + if field, ok := fields[column]; ok { + if field.Field.Kind() == reflect.Ptr { + field.Field.Set(reflect.ValueOf(value).Elem()) + } else if v := reflect.ValueOf(value).Elem().Elem(); v.IsValid() { + field.Field.Set(v) + } + } + } + if isSlice { if isPtr { dest.Set(reflect.Append(dest, elem.Addr())) diff --git a/main_private.go b/main_private.go index 6dd5c00b..7e5c0ef7 100644 --- a/main_private.go +++ b/main_private.go @@ -1,9 +1,6 @@ package gorm -import ( - "regexp" - "time" -) +import "time" func (s *DB) clone() *DB { db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error, values: map[string]interface{}{}} @@ -30,9 +27,6 @@ func (s *DB) err(err error) error { } else { s.log(err) } - if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) { - return nil - } } s.Error = err }