forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QueryExpr, thanks @ManReinsp for PR go-gorm#1548
- Loading branch information
Showing
5 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,9 +607,54 @@ func TestHaving(t *testing.T) { | |
} | ||
} | ||
|
||
func TestQueryBuilderSubselectInWhere(t *testing.T) { | ||
user := User{Name: "ruser1", Email: "[email protected]", Age: 32} | ||
DB.Save(&user) | ||
user = User{Name: "ruser2", Email: "[email protected]", Age: 16} | ||
DB.Save(&user) | ||
user = User{Name: "ruser3", Email: "[email protected]", Age: 64} | ||
DB.Save(&user) | ||
user = User{Name: "ruser4", Email: "[email protected]", Age: 128} | ||
DB.Save(&user) | ||
|
||
var users []User | ||
DB.Select("*").Where("name IN (?)", DB. | ||
Select("name").Table("users").Where("email LIKE ?", "root@%").SubqueryExpr()).Find(&users) | ||
|
||
if len(users) != 2 { | ||
t.Errorf("Two users should be found, instead found %d", len(users)) | ||
} | ||
|
||
DB.Select("*").Where("email LIKE ?", "root%").Where("age >= (?)", DB. | ||
Select("AVG(age)").Table("users").SubqueryExpr()).Find(&users) | ||
|
||
if len(users) != 2 { | ||
t.Errorf("Two users should be found, instead found %d", len(users)) | ||
} | ||
} | ||
|
||
func TestQueryBuilderSubselectInHaving(t *testing.T) { | ||
user := User{Name: "ruser1", Email: "[email protected]", Age: 64} | ||
DB.Save(&user) | ||
user = User{Name: "ruser2", Email: "[email protected]", Age: 128} | ||
DB.Save(&user) | ||
user = User{Name: "ruser3", Email: "[email protected]", Age: 64} | ||
DB.Save(&user) | ||
user = User{Name: "ruser4", Email: "[email protected]", Age: 128} | ||
DB.Save(&user) | ||
|
||
var users []User | ||
DB.Select("AVG(age) as avgage").Where("email LIKE ?", "root%").Group("email").Having("AVG(age) > (?)", DB. | ||
Select("AVG(age)").Where("email LIKE ?", "root%").Table("users").SubqueryExpr()).Find(&users) | ||
|
||
if len(users) != 1 { | ||
t.Errorf("One user group should be found, instead found %d", len(users)) | ||
} | ||
} | ||
|
||
func DialectHasTzSupport() bool { | ||
// NB: mssql and FoundationDB do not support time zones. | ||
if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" || dialect == "foundation" { | ||
if dialect := os.Getenv("GORM_DIALECT"); dialect == "foundation" { | ||
return false | ||
} | ||
return true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters