Skip to content

Commit

Permalink
feat: add WhereNotNull method (#416)
Browse files Browse the repository at this point in the history
Co-authored-by: Wenbo Han <[email protected]>
  • Loading branch information
Kamandlou and hwbrzzl authored Feb 13, 2024
1 parent a441774 commit c12a383
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type Query interface {
WhereNotBetween(column string, x, y any) Query
// WhereNull adds a "where column is null" clause to the query.
WhereNull(column string) Query
// WhereNotNull adds a "where column is not null" clause to the query.
WhereNotNull(column string) Query
// WithoutEvents disables event firing for the query.
WithoutEvents() Query
// WithTrashed allows soft deleted models to be included in the results.
Expand Down
4 changes: 4 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ func (r *QueryImpl) WhereNull(column string) ormcontract.Query {
return r.Where(fmt.Sprintf("%s IS NULL", column))
}

func (r *QueryImpl) WhereNotNull(column string) ormcontract.Query {
return r.Where(fmt.Sprintf("%s IS NOT NULL", column))
}

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

func (s *QueryTestSuite) TestWhereNotNull() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
bio := "where_not_null_bio"
user := User{Name: "where_not_null_user", Avatar: "where_not_null_avatar", Bio: &bio}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "where_not_null_user", Avatar: "where_not_null_avatar_1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

var users []User
s.Nil(query.Where("name = ?", "where_not_null_user").WhereNotNull("bio").Find(&users))
s.True(len(users) == 1)
s.True(users[0].ID == user.ID)
})
}
}

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.

0 comments on commit c12a383

Please sign in to comment.