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 {
s.explain(value, "Save")
s.Exec()
s.explain(value, "Save").Exec()
return s
}
func (s *Orm) Delete(value interface{}) *Orm {
s.explain(value, "Delete")
s.Exec()
s.explain(value, "Delete").Exec()
return s
}

View File

@ -26,6 +26,14 @@ func TestSaveAndFirst(t *testing.T) {
if user.Name != "jinzhu" {
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) {

24
sql.go
View File

@ -1,10 +1,9 @@
package gorm
import (
"fmt"
"reflect"
"strings"
"fmt"
)
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{}) {
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)
s.Error = err
for rows.Next() {
dest := reflect.ValueOf(out).Elem()
fmt.Printf("%+v", dest)
var dest reflect.Value
if is_slice {
dest = reflect.New(dest_type).Elem()
} else {
dest = reflect.ValueOf(out).Elem()
}
columns, _ := rows.Columns()
var values []interface{}
for _, value := range columns {