Skip to content

Commit

Permalink
feature/count (#34)
Browse files Browse the repository at this point in the history
* add count

* fix typo
  • Loading branch information
FrancoLiberali authored Dec 29, 2023
1 parent 5732935 commit 1786d04
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
15 changes: 14 additions & 1 deletion condition/gorm_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func (query *GormQuery) Limit(limit int) {
query.GormDB = query.GormDB.Limit(limit)
}

// Count returns the amount of models that fulfill the conditions
func (query *GormQuery) Count() (int64, error) {
query.cleanSelects()

var count int64

return count, query.GormDB.Count(&count).Error
}

// First finds the first record ordered by primary key, matching given conditions
func (query *GormQuery) First(dest any) error {
return query.GormDB.First(dest).Error
Expand Down Expand Up @@ -243,6 +252,10 @@ func (query *GormQuery) Returning(dest any) error {
return nil
}

func (query *GormQuery) cleanSelects() {
query.GormDB.Statement.Selects = []string{}
}

// Find finds all models matching given conditions
func (query *GormQuery) Update(sets []ISet) (int64, error) {
tablesAndValues, err := getUpdateTablesAndValues(query, sets)
Expand All @@ -252,7 +265,7 @@ func (query *GormQuery) Update(sets []ISet) (int64, error) {

updateMap := map[string]any{}

query.GormDB.Statement.Selects = []string{}
query.cleanSelects()

switch query.Dialector() {
case sql.Postgres, sql.SQLServer, sql.SQLite: // support UPDATE SET FROM
Expand Down
11 changes: 11 additions & 0 deletions condition/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func (query *Query[T]) Offset(offset int) *Query[T] {
return query
}

// Finishing methods

// Count returns the amount of models that fulfill the conditions
func (query *Query[T]) Count() (int64, error) {
if query.err != nil {
return 0, query.err
}

return query.gormQuery.Count()
}

// First finds the first model ordered by primary key, matching given conditions
// or returns gorm.ErrRecordNotFound is if no model does it
func (query *Query[T]) First() (*T, error) {
Expand Down
1 change: 1 addition & 0 deletions docs/cql/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Finishing methods

Finishing methods are those that cause the query to be executed and the result(s) of the query to be returned:

- Count: returns the amount of models that fulfill the conditions.
- First: finds the first model ordered by primary key.
- Take: finds the first model returned by the database in no specified order.
- Last: finds the last model ordered by primary key.
Expand Down
43 changes: 43 additions & 0 deletions test/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@ func NewQueryIntTestSuite(
}
}

// ------------------------- Count --------------------------------

func (ts *QueryIntTestSuite) TestCountReturns0IfNotModels() {
count, err := cql.Query[models.Product](
ts.db,
).Count()

ts.Require().NoError(err)
ts.Equal(int64(0), count)
}

func (ts *QueryIntTestSuite) TestCountReturns0IfConditionsDontMatch() {
ts.createProduct("", 0, 0, false, nil)
count, err := cql.Query[models.Product](
ts.db,
conditions.Product.Int.Is().Eq(1),
).Count()

ts.Require().NoError(err)
ts.Equal(int64(0), count)
}

func (ts *QueryIntTestSuite) TestCountReturns1IfConditionsMatch() {
ts.createProduct("", 1, 0, false, nil)
count, err := cql.Query[models.Product](
ts.db,
conditions.Product.Int.Is().Eq(1),
).Count()
ts.Require().NoError(err)
ts.Equal(int64(1), count)
}

func (ts *QueryIntTestSuite) TestReturnsNIfMoreThanOneMatchConditions() {
ts.createProduct("", 0, 0, false, nil)
ts.createProduct("", 0, 0, false, nil)
count, err := cql.Query[models.Product](
ts.db,
conditions.Product.Int.Is().Eq(0),
).Count()
ts.Require().NoError(err)
ts.Equal(int64(2), count)
}

// ------------------------- FindOne --------------------------------

func (ts *QueryIntTestSuite) TestFindOneReturnsErrorIfConditionsDontMatch() {
Expand Down

0 comments on commit 1786d04

Please sign in to comment.