Fix batch find

This commit is contained in:
Jinzhu 2013-10-27 00:36:56 +08:00
parent 09b6fc3ab0
commit 97fc5878e9
2 changed files with 26 additions and 3 deletions

View File

@ -1,9 +1,14 @@
package gorm
import "testing"
import (
"testing"
"time"
)
type User struct {
Id int64
Age int64
Birthday time.Time
Name string
}
@ -110,3 +115,16 @@ func TestWhere(t *testing.T) {
t.Errorf("Shouldn't find anything when looking for none existing records, %+v", users)
}
}
func TestComplexWhere(t *testing.T) {
db.Save(&User{Name: "1", Age: 18})
db.Save(&User{Name: "2", Age: 20})
db.Save(&User{Name: "3", Age: 22})
db.Save(&User{Name: "3", Age: 24})
var users []User
db.Where("age > ?", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should only have 2 users age great than 20, but have %v", len(users))
}
}

5
sql.go
View File

@ -65,7 +65,12 @@ func (s *Orm) query(out interface{}) {
values = append(values, dest.FieldByName(snakeToUpperCamel(value)).Addr().Interface())
}
s.Error = rows.Scan(values...)
if is_slice {
dest_out.Set(reflect.Append(dest_out, dest))
}
}
if (counts == 0) && !is_slice {
s.Error = errors.New("Record not found!")
}