From a11c9f9b1b3d00d0ed87eab0dca9ac17072dc53c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Oct 2013 15:37:58 +0800 Subject: [PATCH] Query with primary key map --- README.md | 6 ++++- do.go | 3 +++ gorm_test.go | 76 ++++++++++++++++++++++++++++++---------------------- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 7ba841e4..c7536421 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,10 @@ db.Where("name = ?", "jinzhu").Find(&users) // Advanced Where Usage db.Where("name <> ?", "jinzhu").Find(&users) //// users -> select * from users name <> 'jinzhu'; +db.Where(20).First(&user) +//// users -> select * from users where id = 20; +db.Where([]int64{20, 21, 22}).Find(&user) +//// users -> select * from users where id in (20, 21, 22); db.Where("name = ? and age >= ?", "jinzhu", "22").Find(&users) //// users -> select * from users name = 'jinzhu' and age >= 22; db.Where("name in (?)", []string["jinzhu", "jinzhu 2"]).Find(&users) @@ -239,7 +243,7 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us ``` ## TODO -* Query with map or struct +* Query with struct * SubStruct * Index, Unique, Valiations * Auto Migration diff --git a/do.go b/do.go index b9fbdea8..77d0ea88 100644 --- a/do.go +++ b/do.go @@ -338,6 +338,9 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) { } case int, int64, int32: return s.primaryCondiation(s.addToVars(clause["query"])) + case []int64, []int, []int32, []string: + str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb()) + clause["args"] = []interface{}{clause["query"]} } args := clause["args"].([]interface{}) diff --git a/gorm_test.go b/gorm_test.go index da89e734..5926e576 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -75,37 +75,6 @@ func init() { db.Save(&User{Name: "5", Age: 26, Birthday: t4}) } -func TestInitlineCondition(t *testing.T) { - var u1, u2, u3, u4, u5, u6, u7 User - db.Where("name = ?", "3").Order("age desc").First(&u1).First(&u2) - - db.Where("name = ?", "3").First(&u3, "age = 22").First(&u4, "age = ?", 24).First(&u5, "age = ?", 26) - if !((u5.Id == 0) && (u3.Age == 22 && u3.Name == "3") && (u4.Age == 24 && u4.Name == "3")) { - t.Errorf("Inline where condition for first when search") - } - - var us1, us2, us3, us4 []User - db.Find(&us1, "age = 22").Find(&us2, "name = ?", "3").Find(&us3, "age > ?", 20) - if !(len(us1) == 1 && len(us2) == 2 && len(us3) == 3) { - t.Errorf("Inline where condition for find when search") - } - - db.Find(&us4, "name = ? and age > ?", "3", "22") - if len(us4) != 1 { - t.Errorf("More complex inline where condition for find, %v", us4) - } - - db.First(&u6, u1.Id) - if !(u6.Id == u1.Id && u6.Id != 0) { - t.Errorf("Should find out user with int id") - } - - db.First(&u7, strconv.Itoa(int(u1.Id))) - if !(u6.Id == u1.Id && u6.Id != 0) { - t.Errorf("Should find out user with string id") - } -} - func TestSaveAndFind(t *testing.T) { name := "save_and_find" u := &User{Name: name, Age: 1} @@ -219,6 +188,18 @@ func TestComplexWhere(t *testing.T) { t.Errorf("Should only found 3 users that age > 20, but have %v", len(users)) } + var user_ids []int64 + db.Table("users").Where("age > ?", 20).Pluck("id", &user_ids) + if len(user_ids) != 3 { + t.Errorf("Should only found 3 users that age > 20, but have %v", len(users)) + } + users = []User{} + db.Where(user_ids).Find(&users) + + if len(users) != 3 { + t.Errorf("Should only found 3 users that age > 20 when search with id map, but have %v", len(users)) + } + users = []User{} db.Where("age >= ?", 20).Find(&users) if len(users) != 4 { @@ -274,7 +255,7 @@ func TestComplexWhere(t *testing.T) { t.Errorf("Should only found 3 users's name in (1, 3), but have %v", len(users)) } - var user_ids []int64 + user_ids = []int64{} for _, user := range users { user_ids = append(user_ids, user.Id) } @@ -308,6 +289,37 @@ func TestComplexWhere(t *testing.T) { } } +func TestInitlineCondition(t *testing.T) { + var u1, u2, u3, u4, u5, u6, u7 User + db.Where("name = ?", "3").Order("age desc").First(&u1).First(&u2) + + db.Where("name = ?", "3").First(&u3, "age = 22").First(&u4, "age = ?", 24).First(&u5, "age = ?", 26) + if !((u5.Id == 0) && (u3.Age == 22 && u3.Name == "3") && (u4.Age == 24 && u4.Name == "3")) { + t.Errorf("Inline where condition for first when search") + } + + var us1, us2, us3, us4 []User + db.Find(&us1, "age = 22").Find(&us2, "name = ?", "3").Find(&us3, "age > ?", 20) + if !(len(us1) == 1 && len(us2) == 2 && len(us3) == 3) { + t.Errorf("Inline where condition for find when search") + } + + db.Find(&us4, "name = ? and age > ?", "3", "22") + if len(us4) != 1 { + t.Errorf("More complex inline where condition for find, %v", us4) + } + + db.First(&u6, u1.Id) + if !(u6.Id == u1.Id && u6.Id != 0) { + t.Errorf("Should find out user with int id") + } + + db.First(&u7, strconv.Itoa(int(u1.Id))) + if !(u6.Id == u1.Id && u6.Id != 0) { + t.Errorf("Should find out user with string id") + } +} + func TestSelect(t *testing.T) { var user User db.Where("name = ?", "3").Select("name").Find(&user)