Skip to content

Commit 3e28bce

Browse files
committed
feat: add WhereNull method
1 parent fec7da9 commit 3e28bce

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

contracts/database/orm/orm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ type Query interface {
133133
WhereIn(column string, values []any) Query
134134
// WhereNotIn adds a "where column not in" clause to the query.
135135
WhereNotIn(column string, values []any) Query
136+
// WhereNull adds a "where column is null" clause to the query.
137+
WhereNull(column string) Query
136138
// WithoutEvents disables event firing for the query.
137139
WithoutEvents() Query
138140
// WithTrashed allows soft deleted models to be included in the results.

database/gorm/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ func (r *QueryImpl) OrWhereNotIn(column string, values []any) ormcontract.Query
684684
return r.OrWhere(fmt.Sprintf("%s NOT IN ?", column), values)
685685
}
686686

687+
func (r *QueryImpl) WhereNull(column string) ormcontract.Query {
688+
return r.Where(fmt.Sprintf("%s IS NULL", column))
689+
}
690+
687691
func (r *QueryImpl) WithoutEvents() ormcontract.Query {
688692
return NewQueryImplByInstance(r.instance, &QueryImpl{
689693
config: r.config,

database/gorm/query_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,20 @@ func (s *QueryTestSuite) TestOrWhereNotIn() {
28132813
}
28142814
}
28152815

2816+
func (s *QueryTestSuite) TestWhereNull() {
2817+
for driver, query := range s.queries {
2818+
s.Run(driver.String(), func() {
2819+
user := User{Name: "where_null_user", Avatar: "where_null_avatar"}
2820+
s.Nil(query.Create(&user))
2821+
s.True(user.ID > 0)
2822+
2823+
var users []User
2824+
s.Nil(query.WhereNull("name").Find(&users))
2825+
s.True(len(users) == 0)
2826+
})
2827+
}
2828+
}
2829+
28162830
func (s *QueryTestSuite) TestWithoutEvents() {
28172831
for _, query := range s.queries {
28182832
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)