Skip to content

Commit

Permalink
feat: add OrWhereBetween and OrWhereNotBetween methods (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamandlou authored Feb 14, 2024
1 parent c12a383 commit a4e9b9a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ type Query interface {
OrWhereIn(column string, values []any) Query
// OrWhereNotIn adds an "or where column not in" clause to the query.
OrWhereNotIn(column string, values []any) Query
// OrWhereBetween adds an "or where column between x and y" clause to the query.
OrWhereBetween(column string, x, y any) Query
// OrWhereNotBetween adds an "or where column not between x and y" clause to the query.
OrWhereNotBetween(column string, x, y any) Query
// Paginate the given query into a simple paginator.
Paginate(page, limit int, dest any, total *int64) error
// Pluck retrieves a single column from the database.
Expand Down
8 changes: 8 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ 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) OrWhereBetween(column string, x, y any) ormcontract.Query {
return r.OrWhere(fmt.Sprintf("%s BETWEEN %v AND %v", column, x, y))
}

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

func (r *QueryImpl) WhereNull(column string) ormcontract.Query {
return r.Where(fmt.Sprintf("%s IS NULL", column))
}
Expand Down
52 changes: 52 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,58 @@ func (s *QueryTestSuite) TestWhereNotBetween() {
}
}

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

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

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

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

var users []User
s.Nil(query.Where("name = ?", "or_where_between_user_3").OrWhereBetween("id", user.ID, user2.ID).Find(&users))
s.True(len(users) == 4)
})
}
}

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

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

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

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

var users []User
s.Nil(query.Where("name = ?", "or_where_between_user_3").OrWhereNotBetween("id", user.ID, user2.ID).Find(&users))
s.True(len(users) >= 1)
})
}
}

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

32 changes: 32 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 a4e9b9a

Please sign in to comment.