Skip to content

Commit

Permalink
feat: add WhereNotBetween method (#408)
Browse files Browse the repository at this point in the history
Co-authored-by: Wenbo Han <[email protected]>
  • Loading branch information
Kamandlou and hwbrzzl authored Feb 4, 2024
1 parent 6add0f2 commit d397e5c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ type Query interface {
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
// WhereNotBetween adds a "where column not between x and y" clause to the query.
WhereNotBetween(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 @@ -713,6 +713,10 @@ 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) WhereNotBetween(column string, x, y any) ormcontract.Query {
return r.Where(fmt.Sprintf("%s NOT BETWEEN %v AND %v", column, x, y))
}

func (r *QueryImpl) WithoutEvents() ormcontract.Query {
return NewQueryImplByInstance(r.instance, &QueryImpl{
config: r.config,
Expand Down
27 changes: 27 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,33 @@ func (s *QueryTestSuite) TestWhereBetween() {
}
}

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

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

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

user3 := User{Name: "where_not_between_user", Avatar: "where_not_between_avatar_2"}
s.Nil(query.Create(&user3))
s.True(user3.ID > 0)

var users []User
s.Nil(query.Where("name = ?", "where_not_between_user").WhereNotBetween("id", user.ID, user2.ID).Find(&users))
s.True(len(users) == 1)
s.True(users[0].ID == user3.ID)
})
}
}

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.

0 comments on commit d397e5c

Please sign in to comment.