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 OrWhereNotIn and WhereNotIn methods #399

Merged
merged 5 commits into from
Jan 30, 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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I do in this line?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd better create user2, then judge users contains user2.

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.

Loading