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()
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()))

View File

@ -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
}