Skip to content

Commit e503ecf

Browse files
authored
feat: add OrWhereNotIn and WhereNotIn methods (#399)
* feat: add OrWhereNotIn and WhereNotIn methods * update: update TestWhereNotIn method * update: update TestOrWhereNotIn method * check users contain user2
1 parent 2e2c84a commit e503ecf

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

contracts/database/orm/orm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type Query interface {
9696
OrWhere(query any, args ...any) Query
9797
// OrWhereIn adds an "or where column in" clause to the query.
9898
OrWhereIn(column string, values []any) Query
99+
// OrWhereNotIn adds an "or where column not in" clause to the query.
100+
OrWhereNotIn(column string, values []any) Query
99101
// Paginate the given query into a simple paginator.
100102
Paginate(page, limit int, dest any, total *int64) error
101103
// Pluck retrieves a single column from the database.
@@ -127,6 +129,8 @@ type Query interface {
127129
Where(query any, args ...any) Query
128130
// WhereIn adds a "where column in" clause to the query.
129131
WhereIn(column string, values []any) Query
132+
// WhereNotIn adds a "where column not in" clause to the query.
133+
WhereNotIn(column string, values []any) Query
130134
// WithoutEvents disables event firing for the query.
131135
WithoutEvents() Query
132136
// WithTrashed allows soft deleted models to be included in the results.

database/gorm/query.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,14 @@ func (r *QueryImpl) OrWhereIn(column string, values []any) ormcontract.Query {
672672
return r.OrWhere(fmt.Sprintf("%s IN ?", column), values)
673673
}
674674

675+
func (r *QueryImpl) WhereNotIn(column string, values []any) ormcontract.Query {
676+
return r.Where(fmt.Sprintf("%s NOT IN ?", column), values)
677+
}
678+
679+
func (r *QueryImpl) OrWhereNotIn(column string, values []any) ormcontract.Query {
680+
return r.OrWhere(fmt.Sprintf("%s NOT IN ?", column), values)
681+
}
682+
675683
func (r *QueryImpl) WithoutEvents() ormcontract.Query {
676684
return NewQueryImplByInstance(r.instance, &QueryImpl{
677685
config: r.config,

database/gorm/query_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,56 @@ func (s *QueryTestSuite) TestOrWhereIn() {
27432743
}
27442744
}
27452745

2746+
func (s *QueryTestSuite) TestWhereNotIn() {
2747+
for driver, query := range s.queries {
2748+
s.Run(driver.String(), func() {
2749+
user := User{Name: "where_in_user", Avatar: "where_in_avatar"}
2750+
s.Nil(query.Create(&user))
2751+
s.True(user.ID > 0)
2752+
2753+
user1 := User{Name: "where_in_user_1", Avatar: "where_in_avatar_1"}
2754+
s.Nil(query.Create(&user1))
2755+
s.True(user1.ID > 0)
2756+
2757+
user2 := User{Name: "where_in_user_2", Avatar: "where_in_avatar_2"}
2758+
s.Nil(query.Create(&user2))
2759+
s.True(user2.ID > 0)
2760+
2761+
var user3 User
2762+
s.Nil(query.Where("id = ?", user2.ID).WhereNotIn("id", []any{user.ID, user1.ID}).First(&user3))
2763+
s.True(user3.ID == user2.ID)
2764+
})
2765+
}
2766+
}
2767+
2768+
func (s *QueryTestSuite) TestOrWhereNotIn() {
2769+
for driver, query := range s.queries {
2770+
s.Run(driver.String(), func() {
2771+
user := User{Name: "where_in_user", Avatar: "where_in_avatar"}
2772+
s.Nil(query.Create(&user))
2773+
s.True(user.ID > 0)
2774+
2775+
user1 := User{Name: "where_in_user_1", Avatar: "where_in_avatar_1"}
2776+
s.Nil(query.Create(&user1))
2777+
s.True(user1.ID > 0)
2778+
2779+
user2 := User{Name: "where_in_user_2", Avatar: "where_in_avatar_2"}
2780+
s.Nil(query.Create(&user2))
2781+
s.True(user2.ID > 0)
2782+
2783+
var users []User
2784+
s.Nil(query.Where("id = ?", -1).OrWhereNotIn("id", []any{user.ID, user1.ID}).Find(&users))
2785+
var user2Found bool
2786+
for _, user := range users {
2787+
if user.ID == user2.ID {
2788+
user2Found = true
2789+
}
2790+
}
2791+
s.True(user2Found)
2792+
})
2793+
}
2794+
}
2795+
27462796
func (s *QueryTestSuite) TestWithoutEvents() {
27472797
for _, query := range s.queries {
27482798
tests := []struct {

mocks/database/orm/Query.go

Lines changed: 32 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: 32 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)