forked from mirror/gorm
Add whereSql for orm object
This commit is contained in:
parent
3e7e110590
commit
fb3da1f963
|
@ -4,8 +4,8 @@ Yet Another ORM library for Go, hope sucks less. (created for internal usage, AP
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
Where("id =/>/</<> ?", string or int64).First(&user) (error)
|
Where("id =/>/</<> ?", string or int64).First(&user) (error)
|
||||||
Where("id in ?", map[]interface{}).First(&user) (error)
|
Where("id in (?)", map[]interface{}).First(&user) (error)
|
||||||
Where("id in ?", map[]interface{}).Find(&users) (error)
|
Where("id in (?)", map[]interface{}).Find(&users) (error)
|
||||||
Where(map[string]string{"id": "12", "name": "jinzhu"}).Find(&users) (error)
|
Where(map[string]string{"id": "12", "name": "jinzhu"}).Find(&users) (error)
|
||||||
Order("").Limit(11).Or("").Count().Select("").Not("").Offset(11)
|
Order("").Limit(11).Or("").Count().Select("").Not("").Offset(11)
|
||||||
|
|
||||||
|
|
2
orm.go
2
orm.go
|
@ -17,7 +17,7 @@ type Orm struct {
|
||||||
SqlVars []interface{}
|
SqlVars []interface{}
|
||||||
|
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
whereClause []interface{}
|
whereClause []map[string]interface{}
|
||||||
selectStr string
|
selectStr string
|
||||||
orderStr string
|
orderStr string
|
||||||
offsetInt int
|
offsetInt int
|
||||||
|
|
|
@ -38,8 +38,11 @@ func TestSaveAndFirst(t *testing.T) {
|
||||||
|
|
||||||
func TestWhere(t *testing.T) {
|
func TestWhere(t *testing.T) {
|
||||||
db := getDB()
|
db := getDB()
|
||||||
orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu")
|
u := &User{Name: "jinzhu"}
|
||||||
|
db.Save(u)
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
orm.First(user)
|
db.Where("Name = ?", "jinzhu").First(user)
|
||||||
|
|
||||||
fmt.Println(user)
|
fmt.Println(user)
|
||||||
}
|
}
|
||||||
|
|
19
sql.go
19
sql.go
|
@ -3,6 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) querySql(out interface{}) {
|
func (s *Orm) querySql(out interface{}) {
|
||||||
s.Sql = "SELECT * from users limit 1"
|
s.Sql = fmt.Sprintf("SELECT * FROM %v %v", s.TableName, s.whereSql())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ func (s *Orm) query(out interface{}) {
|
||||||
dest_type = dest_out.Type().Elem()
|
dest_type = dest_out.Type().Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := s.db.Query(s.Sql)
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
||||||
s.Error = err
|
s.Error = err
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
@ -75,6 +76,18 @@ func (s *Orm) deleteSql(value interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) whereSql() (sql string) {
|
func (s *Orm) whereSql() (sql string) {
|
||||||
sql = "1=1"
|
if len(s.whereClause) == 0 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
sql = "WHERE "
|
||||||
|
for _, clause := range s.whereClause {
|
||||||
|
sql += clause["query"].(string)
|
||||||
|
args := clause["args"].([]interface{})
|
||||||
|
for _, arg := range args {
|
||||||
|
s.SqlVars = append(s.SqlVars, arg.([]interface{})...)
|
||||||
|
sql = strings.Replace(sql, "?", "$"+strconv.Itoa(len(s.SqlVars)), 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue