From 3e28bce559cf549b49bc493aae2f5cf1821a35ea Mon Sep 17 00:00:00 2001 From: kamandlou Date: Thu, 1 Feb 2024 13:48:23 +0330 Subject: [PATCH] feat: add WhereNull method --- contracts/database/orm/orm.go | 2 ++ database/gorm/query.go | 4 ++++ database/gorm/query_test.go | 14 ++++++++++++++ mocks/database/orm/Query.go | 16 ++++++++++++++++ mocks/database/orm/Transaction.go | 16 ++++++++++++++++ 5 files changed, 52 insertions(+) diff --git a/contracts/database/orm/orm.go b/contracts/database/orm/orm.go index 56be9874e..36fd1a260 100644 --- a/contracts/database/orm/orm.go +++ b/contracts/database/orm/orm.go @@ -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. diff --git a/database/gorm/query.go b/database/gorm/query.go index 95059e5b7..e5f5a80e2 100644 --- a/database/gorm/query.go +++ b/database/gorm/query.go @@ -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, diff --git a/database/gorm/query_test.go b/database/gorm/query_test.go index 74b15c4e0..42a4cc317 100644 --- a/database/gorm/query_test.go +++ b/database/gorm/query_test.go @@ -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 { diff --git a/mocks/database/orm/Query.go b/mocks/database/orm/Query.go index 3d88b7659..507a7680e 100644 --- a/mocks/database/orm/Query.go +++ b/mocks/database/orm/Query.go @@ -881,6 +881,22 @@ func (_m *Query) WhereNotIn(column string, values []interface{}) orm.Query { return r0 } +// WhereNull provides a mock function with given fields: column +func (_m *Query) WhereNull(column string) orm.Query { + ret := _m.Called(column) + + var r0 orm.Query + if rf, ok := ret.Get(0).(func(string) orm.Query); ok { + r0 = rf(column) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(orm.Query) + } + } + + return r0 +} + // With provides a mock function with given fields: query, args func (_m *Query) With(query string, args ...interface{}) orm.Query { var _ca []interface{} diff --git a/mocks/database/orm/Transaction.go b/mocks/database/orm/Transaction.go index 4a378061c..6e73a95bd 100644 --- a/mocks/database/orm/Transaction.go +++ b/mocks/database/orm/Transaction.go @@ -909,6 +909,22 @@ func (_m *Transaction) WhereNotIn(column string, values []interface{}) orm.Query return r0 } +// WhereNull provides a mock function with given fields: column +func (_m *Transaction) WhereNull(column string) orm.Query { + ret := _m.Called(column) + + var r0 orm.Query + if rf, ok := ret.Get(0).(func(string) orm.Query); ok { + r0 = rf(column) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(orm.Query) + } + } + + return r0 +} + // With provides a mock function with given fields: query, args func (_m *Transaction) With(query string, args ...interface{}) orm.Query { var _ca []interface{}