Can't believe this, Find works!

This commit is contained in:
Jinzhu 2013-10-26 12:33:05 +08:00
parent 0491675ae8
commit 3e7e110590
3 changed files with 30 additions and 8 deletions

6
orm.go
View File

@ -85,14 +85,12 @@ func (s *Orm) Select(value interface{}) *Orm {
} }
func (s *Orm) Save(value interface{}) *Orm { func (s *Orm) Save(value interface{}) *Orm {
s.explain(value, "Save") s.explain(value, "Save").Exec()
s.Exec()
return s return s
} }
func (s *Orm) Delete(value interface{}) *Orm { func (s *Orm) Delete(value interface{}) *Orm {
s.explain(value, "Delete") s.explain(value, "Delete").Exec()
s.Exec()
return s return s
} }

View File

@ -26,6 +26,14 @@ func TestSaveAndFirst(t *testing.T) {
if user.Name != "jinzhu" { if user.Name != "jinzhu" {
t.Errorf("User should be saved and fetched correctly") t.Errorf("User should be saved and fetched correctly")
} }
users := []User{}
db.Find(&users)
for _, user := range users {
if user.Name != "jinzhu" {
t.Errorf("User should be saved and fetched correctly")
}
}
} }
func TestWhere(t *testing.T) { func TestWhere(t *testing.T) {

24
sql.go
View File

@ -1,10 +1,9 @@
package gorm package gorm
import ( import (
"fmt"
"reflect" "reflect"
"strings" "strings"
"fmt"
) )
func (s *Orm) explain(value interface{}, operation string) *Orm { func (s *Orm) explain(value interface{}, operation string) *Orm {
@ -26,11 +25,28 @@ func (s *Orm) querySql(out interface{}) {
} }
func (s *Orm) query(out interface{}) { func (s *Orm) query(out interface{}) {
var (
is_slice bool
dest_type reflect.Type
)
dest_out := reflect.Indirect(reflect.ValueOf(out))
if x := dest_out.Kind(); x == reflect.Slice {
is_slice = true
dest_type = dest_out.Type().Elem()
}
rows, err := s.db.Query(s.Sql) rows, err := s.db.Query(s.Sql)
s.Error = err s.Error = err
for rows.Next() { for rows.Next() {
dest := reflect.ValueOf(out).Elem() var dest reflect.Value
fmt.Printf("%+v", dest) if is_slice {
dest = reflect.New(dest_type).Elem()
} else {
dest = reflect.ValueOf(out).Elem()
}
columns, _ := rows.Columns() columns, _ := rows.Columns()
var values []interface{} var values []interface{}
for _, value := range columns { for _, value := range columns {