Don't raise scan error on column index

This commit is contained in:
Jinzhu 2015-01-19 15:50:21 +08:00
parent b7554a2cb0
commit 726b3b8c9f
2 changed files with 25 additions and 14 deletions

View File

@ -47,6 +47,7 @@ func Query(scope *Scope) {
} }
defer rows.Close() defer rows.Close()
columns, _ := rows.Columns()
for rows.Next() { for rows.Next() {
anyRecordFound = true anyRecordFound = true
elem := dest elem := dest
@ -54,19 +55,35 @@ func Query(scope *Scope) {
elem = reflect.New(destType).Elem() elem = reflect.New(destType).Elem()
} }
columns, _ := rows.Columns() var values = make([]interface{}, len(columns))
var values []interface{}
fields := scope.New(elem.Addr().Interface()).Fields() fields := scope.New(elem.Addr().Interface()).Fields()
for _, value := range columns { for index, column := range columns {
if field, ok := fields[value]; ok { if field, ok := fields[column]; ok {
values = append(values, field.Field.Addr().Interface()) if field.Field.Kind() == reflect.Ptr {
values[index] = field.Field.Addr().Interface()
} else { } else {
var ignore interface{} values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface()
values = append(values, &ignore) }
} else {
var value interface{}
values[index] = &value
} }
} }
scope.Err(rows.Scan(values...)) 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 isSlice {
if isPtr { if isPtr {
dest.Set(reflect.Append(dest, elem.Addr())) dest.Set(reflect.Append(dest, elem.Addr()))

View File

@ -1,9 +1,6 @@
package gorm package gorm
import ( import "time"
"regexp"
"time"
)
func (s *DB) clone() *DB { 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{}{}} 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 { } else {
s.log(err) s.log(err)
} }
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
return nil
}
} }
s.Error = err s.Error = err
} }