From 97fc5878e91c276cf260d7314bb037304b752285 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 00:36:56 +0800 Subject: [PATCH] Fix batch find --- orm_test.go | 24 +++++++++++++++++++++--- sql.go | 5 +++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/orm_test.go b/orm_test.go index a662f8b2..e32abf35 100644 --- a/orm_test.go +++ b/orm_test.go @@ -1,10 +1,15 @@ package gorm -import "testing" +import ( + "testing" + "time" +) type User struct { - Id int64 - Name string + Id int64 + Age int64 + Birthday time.Time + Name string } var db DB @@ -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)) + } +} diff --git a/sql.go b/sql.go index 6198c42c..e06b3070 100644 --- a/sql.go +++ b/sql.go @@ -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!") }