Possible to search with interface map

This commit is contained in:
Jinzhu 2013-10-29 17:52:37 +08:00
parent c74af2210d
commit 8a81711e41
3 changed files with 40 additions and 6 deletions

View File

@ -66,6 +66,8 @@ db.Where("name LIKE ?", "%jin%").Find(&users)
//// users -> select * from users name LIKE "%jinzhu%"; //// users -> select * from users name LIKE "%jinzhu%";
db.Where(&User{Name: "jinzhu", Age: 20}).First(&user) db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
//// user -> select * from users name = "jinzhu" and age = 20 limit 1; //// user -> select * from users name = "jinzhu" and age = 20 limit 1;
db.Where(map[string]interface{}{"name": "jinzhu", "age": 20}).First(&user)
//// user -> select * from users name = "jinzhu" and age = 20 limit 1;
db.Where("birthday < ?", time.Now()).Find(&users) db.Where("birthday < ?", time.Now()).Find(&users)
// Inline search condition // Inline search condition
@ -79,6 +81,8 @@ db.Find(&users, "name <> ? and age > ?", "jinzhu", 20)
//// users -> select * from users where name <> "jinzhu" and age > 20; //// users -> select * from users where name <> "jinzhu" and age > 20;
db.Find(&users, &User{Age: 20}) db.Find(&users, &User{Age: 20})
//// users -> select * from users where age = 20; //// users -> select * from users where age = 20;
db.Find(&users, map[string]interface{}{"age": 20})
//// users -> select * from users where age = 20;
// Select // Select
db.Select("name").Find(&users) db.Select("name").Find(&users)
@ -247,10 +251,10 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us
``` ```
## TODO ## TODO
* FindOrInitialize / FindOrCreate
* SubStruct * SubStruct
* Index, Unique, Valiations * Index, Unique, Valiations
* Auto Migration * Auto Migration
* FindOrInitialize / FindOrCreate
* SQL Log * SQL Log
* SQL Query with goroutines * SQL Query with goroutines
* Only tested with postgres, confirm works with other database adaptors * Only tested with postgres, confirm works with other database adaptors

6
do.go
View File

@ -345,6 +345,12 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) {
case []int64, []int, []int32, []string: case []int64, []int, []int32, []string:
str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb()) str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb())
clause["args"] = []interface{}{query} clause["args"] = []interface{}{query}
case map[string]interface{}:
var sqls []string
for key, value := range query.(map[string]interface{}) {
sqls = append(sqls, fmt.Sprintf(" ( %v = %v ) ", key, s.addToVars(value)))
}
return strings.Join(sqls, ",")
case interface{}: case interface{}:
m := &Model{data: query, driver: s.driver} m := &Model{data: query, driver: s.driver}
var sqls []string var sqls []string

View File

@ -293,28 +293,52 @@ func TestWhereWithStruct(t *testing.T) {
var user User var user User
db.First(&user, &User{Name: "2"}) db.First(&user, &User{Name: "2"})
if user.Id == 0 || user.Name != "2" { if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with struct") t.Errorf("Should be able to search first record with inline struct")
} }
db.First(&user, User{Name: "2"}) db.First(&user, User{Name: "2"})
if user.Id == 0 || user.Name != "2" { if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with struct") t.Errorf("Should be able to search first record with inline struct")
} }
db.Where(&User{Name: "2"}).First(&user) db.Where(&User{Name: "2"}).First(&user)
if user.Id == 0 || user.Name != "2" { if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with struct") t.Errorf("Should be able to search first record with where struct")
} }
var users []User var users []User
db.Find(&users, &User{Name: "3"}) db.Find(&users, &User{Name: "3"})
if len(users) != 2 { if len(users) != 2 {
t.Errorf("Should be able to search all record with struct") t.Errorf("Should be able to search all record with inline struct")
} }
db.Where(User{Name: "3"}).Find(&users) db.Where(User{Name: "3"}).Find(&users)
if user.Id == 0 || user.Name != "2" { if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with struct") t.Errorf("Should be able to search first record with where struct")
}
}
func TestWhereWithInterfaceMap(t *testing.T) {
var user User
db.First(&user, map[string]interface{}{"name": "2"})
if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with inline interface map")
}
db.Where(map[string]interface{}{"name": "2"}).First(&user)
if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with where interface map")
}
var users []User
db.Find(&users, map[string]interface{}{"name": "3"})
if len(users) != 2 {
t.Errorf("Should be able to search all record with inline interface map")
}
db.Where(map[string]interface{}{"name": "3"}).Find(&users)
if user.Id == 0 || user.Name != "2" {
t.Errorf("Should be able to search first record with where interface map")
} }
} }