Skip to content

Commit 1a6c6ca

Browse files
Kamandlouhwbrzzl
andauthored
feat: add OrWhereIn method (#397)
* feat: add OrWhereIn method * fix: update orm.go --------- Co-authored-by: Wenbo Han <[email protected]>
1 parent c00b035 commit 1a6c6ca

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

contracts/database/orm/orm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ type Query interface {
9494
Order(value any) Query
9595
// OrWhere add an "or where" clause to the query.
9696
OrWhere(query any, args ...any) Query
97+
// OrWhereIn adds an "or where column in" clause to the query.
98+
OrWhereIn(column string, values []any) Query
9799
// Paginate the given query into a simple paginator.
98100
Paginate(page, limit int, dest any, total *int64) error
99101
// Pluck retrieves a single column from the database.

database/gorm/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,10 @@ func (r *QueryImpl) WhereIn(column string, values []any) ormcontract.Query {
668668
return r.Where(fmt.Sprintf("%s IN ?", column), values)
669669
}
670670

671+
func (r *QueryImpl) OrWhereIn(column string, values []any) ormcontract.Query {
672+
return r.OrWhere(fmt.Sprintf("%s IN ?", column), values)
673+
}
674+
671675
func (r *QueryImpl) WithoutEvents() ormcontract.Query {
672676
return NewQueryImplByInstance(r.instance, &QueryImpl{
673677
config: r.config,

database/gorm/query_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,24 @@ func (s *QueryTestSuite) TestWhereIn() {
27252725
}
27262726
}
27272727

2728+
func (s *QueryTestSuite) TestOrWhereIn() {
2729+
for driver, query := range s.queries {
2730+
s.Run(driver.String(), func() {
2731+
user := User{Name: "where_in_user", Avatar: "where_in_avatar"}
2732+
s.Nil(query.Create(&user))
2733+
s.True(user.ID > 0)
2734+
2735+
user1 := User{Name: "where_in_user_1", Avatar: "where_in_avatar_1"}
2736+
s.Nil(query.Create(&user1))
2737+
s.True(user1.ID > 0)
2738+
2739+
var users []User
2740+
s.Nil(query.Where("id = ?", -1).OrWhereIn("id", []any{user.ID, user1.ID}).Find(&users))
2741+
s.True(len(users) == 2)
2742+
})
2743+
}
2744+
}
2745+
27282746
func (s *QueryTestSuite) TestWithoutEvents() {
27292747
for _, query := range s.queries {
27302748
tests := []struct {

mocks/database/orm/Query.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/database/orm/Transaction.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)