Make pluck works

This commit is contained in:
Jinzhu 2013-10-27 10:28:47 +08:00
parent 80a7e6296d
commit 6511cad317
2 changed files with 9 additions and 12 deletions

View File

@ -41,7 +41,7 @@ func init() {
func TestSaveAndFind(t *testing.T) { func TestSaveAndFind(t *testing.T) {
name := "save_and_find" name := "save_and_find"
u := &User{Name: name} u := &User{Name: name, Age: 1}
db.Save(u) db.Save(u)
if u.Id == 0 { if u.Id == 0 {
t.Errorf("Should have ID after create record") t.Errorf("Should have ID after create record")
@ -59,9 +59,9 @@ func TestSaveAndFind(t *testing.T) {
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
name, name2, new_name := "update", "update2", "new_update" name, name2, new_name := "update", "update2", "new_update"
user := User{Name: name} user := User{Name: name, Age: 1}
db.Save(&user) db.Save(&user)
db.Save(&User{Name: name2}) db.Save(&User{Name: name2, Age: 1})
if user.Id == 0 { if user.Id == 0 {
t.Errorf("User Id should exist after create") t.Errorf("User Id should exist after create")
@ -87,9 +87,9 @@ func TestUpdate(t *testing.T) {
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
name, name2 := "delete", "delete2" name, name2 := "delete", "delete2"
user := User{Name: name} user := User{Name: name, Age: 1}
db.Save(&user) db.Save(&user)
db.Save(&User{Name: name2}) db.Save(&User{Name: name2, Age: 1})
orm := db.Delete(&user) orm := db.Delete(&user)
orm = db.Where("name = ?", name).First(&User{}) orm = db.Where("name = ?", name).First(&User{})
@ -105,7 +105,7 @@ func TestDelete(t *testing.T) {
func TestWhere(t *testing.T) { func TestWhere(t *testing.T) {
name := "where" name := "where"
db.Save(&User{Name: name}) db.Save(&User{Name: name, Age: 1})
user := &User{} user := &User{}
db.Where("Name = ?", name).First(user) db.Where("Name = ?", name).First(user)
@ -239,11 +239,9 @@ func TestSelect(t *testing.T) {
if query.Error == nil { if query.Error == nil {
t.Errorf("Should got error with invalid select string") t.Errorf("Should got error with invalid select string")
} }
debug(query.Error)
} }
func TestPluck(t *testing.T) { func TestPluck(t *testing.T) {
var ages []int64 var ages []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages) db.Model(&[]User{}).Order("age desc").Pluck("age", &ages)
debug(ages)
} }

7
sql.go
View File

@ -92,12 +92,11 @@ func (s *Orm) pluck(value interface{}) {
rows, err := s.db.Query(s.Sql, s.SqlVars...) rows, err := s.db.Query(s.Sql, s.SqlVars...)
s.Error = err s.Error = err
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
dest := reflect.New(dest_type).Elem() dest := reflect.New(dest_type).Elem().Interface()
s.Error = rows.Scan(dest) s.Error = rows.Scan(&dest)
dest_out.Set(reflect.Append(dest_out, dest)) dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
} }
return return
} }