forked from mirror/gorm
Possible to search with interface map
This commit is contained in:
parent
c74af2210d
commit
8a81711e41
|
@ -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
|
||||
|
|
6
do.go
6
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
|
||||
|
|
34
gorm_test.go
34
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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue