From fb3da1f9630388679832655b06915490629ce3a5 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 26 Oct 2013 14:10:47 +0800 Subject: [PATCH] Add whereSql for orm object --- README.md | 4 ++-- orm.go | 2 +- orm_test.go | 7 +++++-- sql.go | 19 ++++++++++++++++--- utils.go | 5 +++++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index be0915f5..535d548c 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ Yet Another ORM library for Go, hope sucks less. (created for internal usage, AP # TODO Where("id =/>/ ?", string or int64).First(&user) (error) -Where("id in ?", map[]interface{}).First(&user) (error) -Where("id in ?", map[]interface{}).Find(&users) (error) +Where("id in (?)", map[]interface{}).First(&user) (error) +Where("id in (?)", map[]interface{}).Find(&users) (error) Where(map[string]string{"id": "12", "name": "jinzhu"}).Find(&users) (error) Order("").Limit(11).Or("").Count().Select("").Not("").Offset(11) diff --git a/orm.go b/orm.go index 83ba9d5c..d3923997 100644 --- a/orm.go +++ b/orm.go @@ -17,7 +17,7 @@ type Orm struct { SqlVars []interface{} db *sql.DB - whereClause []interface{} + whereClause []map[string]interface{} selectStr string orderStr string offsetInt int diff --git a/orm_test.go b/orm_test.go index a1f87d43..2b934f2a 100644 --- a/orm_test.go +++ b/orm_test.go @@ -38,8 +38,11 @@ func TestSaveAndFirst(t *testing.T) { func TestWhere(t *testing.T) { 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{} - orm.First(user) + db.Where("Name = ?", "jinzhu").First(user) + fmt.Println(user) } diff --git a/sql.go b/sql.go index 4e5a92f2..2235bb28 100644 --- a/sql.go +++ b/sql.go @@ -3,6 +3,7 @@ package gorm import ( "fmt" "reflect" + "strconv" "strings" ) @@ -20,7 +21,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm { } 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 } @@ -36,7 +37,7 @@ func (s *Orm) query(out interface{}) { 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 for rows.Next() { @@ -75,6 +76,18 @@ func (s *Orm) deleteSql(value interface{}) { } 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 } diff --git a/utils.go b/utils.go index 9e30b543..96e6dbbb 100644 --- a/utils.go +++ b/utils.go @@ -34,3 +34,8 @@ func quoteMap(values []string) (results []string) { } return } + +func debug(value interface{}) { + fmt.Printf("***************\n") + fmt.Printf("%+v\n\n", value) +}