mirror of https://github.com/go-gorm/gorm.git
Support use variable to keep query chain
This commit is contained in:
parent
c9e8c8d16c
commit
8fd8604a70
|
@ -710,6 +710,12 @@ db.Where("created_at > ?", "2013-10-10").Find(&cancelled_orders, "state = ?", "c
|
|||
//// SELECT * FROM orders WHERE created_at > '2013/10/10' AND state = 'shipped'; (shipped_orders)
|
||||
|
||||
|
||||
// Use variable to keep query chain
|
||||
todays_orders := db.Where("created_at > ?", "2013-10-29")
|
||||
cancelled_orders := todays_orders.Where("state = ?", "cancelled")
|
||||
shipped_orders := todays_orders.Where("state = ?", "shipped")
|
||||
|
||||
|
||||
// Search with shared conditions from different tables
|
||||
db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_carts)
|
||||
//// SELECT * FROM orders WHERE product_name = 'fancy_product'; (orders)
|
||||
|
|
11
gorm_test.go
11
gorm_test.go
|
@ -1354,11 +1354,14 @@ func TestTransaction(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *CreditCard) BeforeSave() (err error) {
|
||||
if s.Number == "0000" {
|
||||
err = errors.New("invalid credit card")
|
||||
func TestQueryChain(t *testing.T) {
|
||||
var user_count1, user_count2 int64
|
||||
d := db.Model(User{}).Where("age > ?", 20)
|
||||
d.Where("name = ?", "3").Count(&user_count1)
|
||||
d.Count(&user_count2)
|
||||
if user_count2 == user_count1 {
|
||||
t.Error("DB object should be cloned when search")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BenchmarkGorm(b *testing.B) {
|
||||
|
|
Loading…
Reference in New Issue