mirror of https://github.com/go-gorm/gorm.git
Break find code into sql.go file
This commit is contained in:
parent
69f53a4b41
commit
0491675ae8
17
orm.go
17
orm.go
|
@ -2,8 +2,6 @@ package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -116,23 +114,12 @@ func (s *Orm) Exec(sql ...string) *Orm {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) First(out interface{}) *Orm {
|
func (s *Orm) First(out interface{}) *Orm {
|
||||||
s.setModel(out)
|
s.explain(out, "Query").query(out)
|
||||||
rows, err := s.db.Query("SELECT * from users limit 1")
|
|
||||||
s.Error = err
|
|
||||||
for rows.Next() {
|
|
||||||
dest := reflect.ValueOf(out).Elem()
|
|
||||||
fmt.Printf("%+v", dest)
|
|
||||||
columns, _ := rows.Columns()
|
|
||||||
var values []interface{}
|
|
||||||
for _, value := range columns {
|
|
||||||
values = append(values, dest.FieldByName(value).Addr().Interface())
|
|
||||||
}
|
|
||||||
s.Error = rows.Scan(values...)
|
|
||||||
}
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) Find(out interface{}) *Orm {
|
func (s *Orm) Find(out interface{}) *Orm {
|
||||||
|
s.explain(out, "Query").query(out)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
sql.go
26
sql.go
|
@ -1,18 +1,42 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Orm) explain(value interface{}, operation string) {
|
func (s *Orm) explain(value interface{}, operation string) *Orm {
|
||||||
s.setModel(value)
|
s.setModel(value)
|
||||||
switch operation {
|
switch operation {
|
||||||
case "Save":
|
case "Save":
|
||||||
s.saveSql(value)
|
s.saveSql(value)
|
||||||
case "Delete":
|
case "Delete":
|
||||||
s.deleteSql(value)
|
s.deleteSql(value)
|
||||||
|
case "Query":
|
||||||
|
s.querySql(value)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Orm) querySql(out interface{}) {
|
||||||
|
s.Sql = "SELECT * from users limit 1"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Orm) query(out interface{}) {
|
||||||
|
rows, err := s.db.Query(s.Sql)
|
||||||
|
s.Error = err
|
||||||
|
for rows.Next() {
|
||||||
|
dest := reflect.ValueOf(out).Elem()
|
||||||
|
fmt.Printf("%+v", dest)
|
||||||
|
columns, _ := rows.Columns()
|
||||||
|
var values []interface{}
|
||||||
|
for _, value := range columns {
|
||||||
|
values = append(values, dest.FieldByName(value).Addr().Interface())
|
||||||
|
}
|
||||||
|
s.Error = rows.Scan(values...)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue