From 8a81711e41e7f91e02f20fe66ab3b0f7555104fc Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Oct 2013 17:52:37 +0800 Subject: [PATCH] Possible to search with interface map --- README.md | 6 +++++- do.go | 6 ++++++ gorm_test.go | 34 +++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f25e8e81..82bd5b4d 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ db.Where("name LIKE ?", "%jin%").Find(&users) //// users -> select * from users name LIKE "%jinzhu%"; db.Where(&User{Name: "jinzhu", Age: 20}).First(&user) //// 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) // 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; db.Find(&users, &User{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 db.Select("name").Find(&users) @@ -247,10 +251,10 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us ``` ## TODO +* FindOrInitialize / FindOrCreate * SubStruct * Index, Unique, Valiations * Auto Migration -* FindOrInitialize / FindOrCreate * SQL Log * SQL Query with goroutines * Only tested with postgres, confirm works with other database adaptors diff --git a/do.go b/do.go index cd163ae8..511bc725 100644 --- a/do.go +++ b/do.go @@ -345,6 +345,12 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) { case []int64, []int, []int32, []string: str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb()) 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{}: m := &Model{data: query, driver: s.driver} var sqls []string diff --git a/gorm_test.go b/gorm_test.go index e5b0cbf3..1f525dee 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -293,28 +293,52 @@ func TestWhereWithStruct(t *testing.T) { var user User db.First(&user, &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"}) 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) 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 db.Find(&users, &User{Name: "3"}) 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) 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") } }