Skip to content

Commit

Permalink
feat: add WhereNull method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamandlou committed Feb 1, 2024
1 parent fec7da9 commit 3e28bce
Show file tree
Hide file tree
Showing 5 changed files with 52 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 @@ -133,6 +133,8 @@ type Query interface {
WhereIn(column string, values []any) Query
// WhereNotIn adds a "where column not in" clause to the query.
WhereNotIn(column string, values []any) Query
// WhereNull adds a "where column is null" clause to the query.
WhereNull(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 @@ -684,6 +684,10 @@ func (r *QueryImpl) OrWhereNotIn(column string, values []any) ormcontract.Query
return r.OrWhere(fmt.Sprintf("%s NOT IN ?", column), values)
}

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

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

func (s *QueryTestSuite) TestWhereNull() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "where_null_user", Avatar: "where_null_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

var users []User
s.Nil(query.WhereNull("name").Find(&users))
s.True(len(users) == 0)
})
}
}

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 3e28bce

Please sign in to comment.