Skip to content

Commit c12a383

Browse files
Kamandlouhwbrzzl
andauthored
feat: add WhereNotNull method (#416)
Co-authored-by: Wenbo Han <[email protected]>
1 parent a441774 commit c12a383

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

contracts/database/orm/orm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ type Query interface {
143143
WhereNotBetween(column string, x, y any) Query
144144
// WhereNull adds a "where column is null" clause to the query.
145145
WhereNull(column string) Query
146+
// WhereNotNull adds a "where column is not null" clause to the query.
147+
WhereNotNull(column string) Query
146148
// WithoutEvents disables event firing for the query.
147149
WithoutEvents() Query
148150
// 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
@@ -721,6 +721,10 @@ func (r *QueryImpl) WhereNull(column string) ormcontract.Query {
721721
return r.Where(fmt.Sprintf("%s IS NULL", column))
722722
}
723723

724+
func (r *QueryImpl) WhereNotNull(column string) ormcontract.Query {
725+
return r.Where(fmt.Sprintf("%s IS NOT NULL", column))
726+
}
727+
724728
func (r *QueryImpl) WithoutEvents() ormcontract.Query {
725729
return NewQueryImplByInstance(r.instance, &QueryImpl{
726730
config: r.config,

database/gorm/query_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,26 @@ func (s *QueryTestSuite) TestWhereNull() {
29292929
}
29302930
}
29312931

2932+
func (s *QueryTestSuite) TestWhereNotNull() {
2933+
for driver, query := range s.queries {
2934+
s.Run(driver.String(), func() {
2935+
bio := "where_not_null_bio"
2936+
user := User{Name: "where_not_null_user", Avatar: "where_not_null_avatar", Bio: &bio}
2937+
s.Nil(query.Create(&user))
2938+
s.True(user.ID > 0)
2939+
2940+
user1 := User{Name: "where_not_null_user", Avatar: "where_not_null_avatar_1"}
2941+
s.Nil(query.Create(&user1))
2942+
s.True(user1.ID > 0)
2943+
2944+
var users []User
2945+
s.Nil(query.Where("name = ?", "where_not_null_user").WhereNotNull("bio").Find(&users))
2946+
s.True(len(users) == 1)
2947+
s.True(users[0].ID == user.ID)
2948+
})
2949+
}
2950+
}
2951+
29322952
func (s *QueryTestSuite) TestWithoutEvents() {
29332953
for _, query := range s.queries {
29342954
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)