Skip to content

Commit

Permalink
delete and update at least one condition compiled (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoLiberali authored Jan 7, 2024
1 parent d35bf49 commit b281391
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
12 changes: 9 additions & 3 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
"github.com/FrancoLiberali/cql/model"
)

// Create a Delete to which the conditions are applied inside transaction tx
// Create a Delete to which the conditions are applied inside transaction tx.
//
// At least one condition is required to avoid deleting all values in a table.
// In case this is the desired behavior, use cql.True.
//
// For details see https://compiledquerylenguage.readthedocs.io/en/latest/cql/delete.html
func Delete[T model.Model](tx *gorm.DB, conditions ...condition.Condition[T]) *condition.Delete[T] {
return condition.NewDelete(tx, conditions...)
func Delete[T model.Model](tx *gorm.DB, firstCondition condition.Condition[T], conditions ...condition.Condition[T]) *condition.Delete[T] {
return condition.NewDelete(
tx,
append(conditions, firstCondition)...,
)
}
8 changes: 0 additions & 8 deletions test/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ func NewDeleteIntTestSuite(
}
}

func (ts *DeleteIntTestSuite) TestDeleteWithoutConditions() {
_, err := cql.Delete[models.Product](
ts.db,
).Exec()
ts.ErrorIs(err, cql.ErrEmptyConditions)
ts.ErrorContains(err, "method: Delete")
}

func (ts *DeleteIntTestSuite) TestDeleteWithTrue() {
ts.createProduct("", 0, 0, false, nil)
ts.createProduct("", 1, 0, false, nil)
Expand Down
10 changes: 0 additions & 10 deletions test/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ func NewUpdateIntTestSuite(
}
}

func (ts *UpdateIntTestSuite) TestUpdateWithoutConditions() {
_, err := cql.Update[models.Product](
ts.db,
).Set(
conditions.Product.Int.Set().Eq(0),
)
ts.ErrorIs(err, cql.ErrEmptyConditions)
ts.ErrorContains(err, "method: Update")
}

func (ts *UpdateIntTestSuite) TestUpdateWithTrue() {
ts.createProduct("", 0, 0, false, nil)
ts.createProduct("", 1, 0, false, nil)
Expand Down
12 changes: 9 additions & 3 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
"github.com/FrancoLiberali/cql/model"
)

// Create a Update to which the conditions are applied inside transaction tx
// Create a Update to which the conditions are applied inside transaction tx.
//
// At least one condition is required to avoid updating all values in a table.
// In case this is the desired behavior, use cql.True.
//
// For details see https://compiledquerylenguage.readthedocs.io/en/latest/cql/update.html
func Update[T model.Model](tx *gorm.DB, conditions ...condition.Condition[T]) *condition.Update[T] {
return condition.NewUpdate(tx, conditions...)
func Update[T model.Model](tx *gorm.DB, firstCondition condition.Condition[T], conditions ...condition.Condition[T]) *condition.Update[T] {
return condition.NewUpdate(
tx,
append(conditions, firstCondition)...,
)
}

0 comments on commit b281391

Please sign in to comment.