diff --git a/delete.go b/delete.go index 2799d28..1a2b33e 100644 --- a/delete.go +++ b/delete.go @@ -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)..., + ) } diff --git a/test/delete_test.go b/test/delete_test.go index fbb18d1..50a8905 100644 --- a/test/delete_test.go +++ b/test/delete_test.go @@ -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) diff --git a/test/update_test.go b/test/update_test.go index 6ab6210..7df165c 100644 --- a/test/update_test.go +++ b/test/update_test.go @@ -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) diff --git a/update.go b/update.go index 246e7f2..cecd4d4 100644 --- a/update.go +++ b/update.go @@ -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)..., + ) }