Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add InRandomOrder method #405

Merged
merged 8 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/database/orm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package orm
const (
DriverMysql Driver = "mysql"
DriverPostgresql Driver = "postgresql"
DriverPostgres Driver = "postgres"
DriverSqlite Driver = "sqlite"
DriverSqlserver Driver = "sqlserver"
)
Expand Down
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Query interface {
Group(name string) Query
// Having specifying HAVING conditions for the query.
Having(query any, args ...any) Query
// InRandomOrder specifies the order randomly.
InRandomOrder() Query
// Join specifying JOIN conditions for the query.
Join(query string, args ...any) Query
// Limit the number of records returned.
Expand Down
15 changes: 15 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,21 @@ func (r *QueryImpl) OrderByDesc(column string) ormcontract.Query {
return r.Order(fmt.Sprintf("%s DESC", column))
}

func (r *QueryImpl) InRandomOrder() ormcontract.Query {
order := ""
switch r.Driver() {
case ormcontract.DriverMysql:
order = "RAND()"
case ormcontract.DriverSqlserver:
order = "NEWID()"
case ormcontract.DriverPostgres:
order = "RANDOM()"
case ormcontract.DriverSqlite:
order = "RANDOM()"
}
return r.Order(order)
}

func (r *QueryImpl) OrWhere(query any, args ...any) ormcontract.Query {
tx := r.instance.Or(query, args...)

Expand Down
22 changes: 22 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,28 @@ func (s *QueryTestSuite) TestOrderByDesc() {
}
}

func (s *QueryTestSuite) TestInRandomOrder() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
for i := 0; i < 30; i++ {
user := User{Name: "random_order_user", Avatar: "random_order_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)
}

var users1 []User
s.Nil(query.Where("name = ?", "random_order_user").InRandomOrder().Find(&users1))
s.True(len(users1) == 30)

var users2 []User
s.Nil(query.Where("name = ?", "random_order_user").InRandomOrder().Find(&users2))
s.True(len(users2) == 30)

s.True(users1[0].ID != users2[0].ID || users1[14].ID != users2[14].ID || users1[29].ID != users2[29].ID)
})
}
}

func (s *QueryTestSuite) TestPaginate() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
Expand Down
16 changes: 16 additions & 0 deletions mocks/database/orm/Query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions mocks/database/orm/Transaction.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading