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 OrderBy method #404

Merged
merged 5 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type Query interface {
Omit(columns ...string) Query
// Order specifies the order in which the results should be returned.
Order(value any) Query
// OrderBy specifies the order should be ascending.
OrderBy(column string, direction ...string) Query
// OrderByDesc specifies the order should be descending.
OrderByDesc(column string) Query
// OrWhere add an "or where" clause to the query.
Expand Down
10 changes: 10 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ func (r *QueryImpl) Order(value any) ormcontract.Query {
return NewQueryImplByInstance(tx, r)
}

func (r *QueryImpl) OrderBy(column string, direction ...string) ormcontract.Query {
var orderDirection string
if len(direction) > 0 {
orderDirection = direction[0]
} else {
orderDirection = "ASC"
}
return r.Order(fmt.Sprintf("%s %s", column, orderDirection))
}

func (r *QueryImpl) OrderByDesc(column string) ormcontract.Query {
return r.Order(fmt.Sprintf("%s DESC", column))
}
Expand Down
24 changes: 24 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,30 @@ func (s *QueryTestSuite) TestOrder() {
}
}

func (s *QueryTestSuite) TestOrderBy() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "order_asc_user", Avatar: "order_asc_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "order_asc_user", Avatar: "order_asc_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

var users1 []User
s.Nil(query.Where("name = ?", "order_asc_user").OrderBy("id").Get(&users1))
s.True(len(users1) == 2)
s.True(users1[0].ID == user.ID)

var users2 []User
s.Nil(query.Where("name = ?", "order_asc_user").OrderBy("id", "DESC").Get(&users2))
s.True(len(users2) == 2)
s.True(users2[0].ID == user1.ID)
})
}
}

func (s *QueryTestSuite) TestOrderByDesc() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
Expand Down
23 changes: 23 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.

23 changes: 23 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