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 WhereBetween method #402

Merged
merged 3 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 @@ -133,6 +133,8 @@ type Query interface {
WhereIn(column string, values []any) Query
// WhereNotIn adds a "where column not in" clause to the query.
WhereNotIn(column string, values []any) Query
// WhereBetween adds a "where column between x and y" clause to the query.
WhereBetween(column string, x, y any) Query
// WithoutEvents disables event firing for the query.
WithoutEvents() Query
// WithTrashed allows soft deleted models to be included in the results.
Expand Down
4 changes: 4 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ func (r *QueryImpl) OrWhereNotIn(column string, values []any) ormcontract.Query
return r.OrWhere(fmt.Sprintf("%s NOT IN ?", column), values)
}

func (r *QueryImpl) WhereBetween(column string, x, y any) ormcontract.Query {
return r.Where(fmt.Sprintf("%s BETWEEN %v AND %v", column, x, y))
}

func (r *QueryImpl) WithoutEvents() ormcontract.Query {
return NewQueryImplByInstance(r.instance, &QueryImpl{
config: r.config,
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 @@ -2813,6 +2813,28 @@ func (s *QueryTestSuite) TestOrWhereNotIn() {
}
}

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

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

user2 := User{Name: "where_between_user_2", Avatar: "where_between_avatar_2"}
s.Nil(query.Create(&user2))
s.True(user2.ID > 0)

var users []User
s.Nil(query.WhereBetween("id", user.ID, user2.ID).Find(&users))
s.True(len(users) == 3)
})
}
}

func (s *QueryTestSuite) TestWithoutEvents() {
for _, query := range s.queries {
tests := []struct {
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