Skip to content

Commit

Permalink
feat: add OrWhereNotIn and WhereNotIn methods (#399)
Browse files Browse the repository at this point in the history
* feat: add OrWhereNotIn and WhereNotIn methods

* update: update TestWhereNotIn method

* update: update TestOrWhereNotIn method

* check users contain user2
  • Loading branch information
Kamandlou authored Jan 30, 2024
1 parent 2e2c84a commit e503ecf
Show file tree
Hide file tree
Showing 5 changed files with 126 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 @@ -96,6 +96,8 @@ type Query interface {
OrWhere(query any, args ...any) Query
// OrWhereIn adds an "or where column in" clause to the query.
OrWhereIn(column string, values []any) Query
// OrWhereNotIn adds an "or where column not in" clause to the query.
OrWhereNotIn(column string, values []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 Expand Up @@ -127,6 +129,8 @@ type Query interface {
Where(query any, args ...any) Query
// WhereIn adds a "where column in" clause to the query.
WhereIn(column string, values []any) Query
// WhereNotIn adds a "where column not in" clause to the query.
WhereNotIn(column string, values []any) Query
// WithoutEvents disables event firing for the query.
WithoutEvents() Query
// WithTrashed allows soft deleted models to be included in the results.
Expand Down
8 changes: 8 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ func (r *QueryImpl) OrWhereIn(column string, values []any) ormcontract.Query {
return r.OrWhere(fmt.Sprintf("%s IN ?", column), values)
}

func (r *QueryImpl) WhereNotIn(column string, values []any) ormcontract.Query {
return r.Where(fmt.Sprintf("%s NOT IN ?", column), values)
}

func (r *QueryImpl) OrWhereNotIn(column string, values []any) ormcontract.Query {
return r.OrWhere(fmt.Sprintf("%s NOT IN ?", column), values)
}

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

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

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

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

var user3 User
s.Nil(query.Where("id = ?", user2.ID).WhereNotIn("id", []any{user.ID, user1.ID}).First(&user3))
s.True(user3.ID == user2.ID)
})
}
}

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

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

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

var users []User
s.Nil(query.Where("id = ?", -1).OrWhereNotIn("id", []any{user.ID, user1.ID}).Find(&users))
var user2Found bool
for _, user := range users {
if user.ID == user2.ID {
user2Found = true
}
}
s.True(user2Found)
})
}
}

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

Please sign in to comment.