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 OrWhereIn method #397

Merged
merged 3 commits into from
Jan 27, 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 @@ -94,6 +94,8 @@ type Query interface {
Order(value any) Query
// OrWhere add an "or where" clause to the query.
OrWhere(query any, args ...any) Query
// OrWhereIn adds an "or where column in" clause to the query.
OrWhereIn(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
4 changes: 4 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ func (r *QueryImpl) WhereIn(column string, values []any) ormcontract.Query {
return r.Where(fmt.Sprintf("%s IN ?", column), values)
}

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

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

func (s *QueryTestSuite) TestOrWhereIn() {
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)

var users []User
s.Nil(query.Where("id = ?", -1).OrWhereIn("id", []any{user.ID, user1.ID}).Find(&users))
s.True(len(users) == 2)
})
}
}

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