diff options
author | delvh <dev.lh@web.de> | 2023-12-25 21:25:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-25 21:25:29 +0100 |
commit | 778ad795fd4a19dc15723b59a846a250034c7c3a (patch) | |
tree | 8d64a21734517a9826655955d77cb0bcc187f784 /models/db | |
parent | b41925cee3d67a1fe546c7a219174e4a8b2302b7 (diff) | |
download | gitea-778ad795fd4a19dc15723b59a846a250034c7c3a.tar.gz gitea-778ad795fd4a19dc15723b59a846a250034c7c3a.zip |
Refactor deletion (#28610)
Introduce the new generic deletion methods
- `func DeleteByID[T any](ctx context.Context, id int64) (int64, error)`
- `func DeleteByIDs[T any](ctx context.Context, ids ...int64) error`
- `func Delete[T any](ctx context.Context, opts FindOptions) (int64,
error)`
So, we no longer need any specific deletion method and can just use
the generic ones instead.
Replacement of #28450
Closes #28450
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/db')
-rw-r--r-- | models/db/context.go | 34 | ||||
-rw-r--r-- | models/db/error.go | 18 |
2 files changed, 27 insertions, 25 deletions
diff --git a/models/db/context.go b/models/db/context.go index 7b739f7e9f..cda608af19 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -175,7 +175,7 @@ func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error) { func Get[T any](ctx context.Context, cond builder.Cond) (object *T, exist bool, err error) { if !cond.IsValid() { - return nil, false, ErrConditionRequired{} + panic("cond is invalid in db.Get(ctx, cond). This should not be possible.") } var bean T @@ -201,7 +201,7 @@ func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err e func Exist[T any](ctx context.Context, cond builder.Cond) (bool, error) { if !cond.IsValid() { - return false, ErrConditionRequired{} + panic("cond is invalid in db.Exist(ctx, cond). This should not be possible.") } var bean T @@ -213,16 +213,36 @@ func ExistByID[T any](ctx context.Context, id int64) (bool, error) { return GetEngine(ctx).ID(id).NoAutoCondition().Exist(&bean) } +// DeleteByID deletes the given bean with the given ID +func DeleteByID[T any](ctx context.Context, id int64) (int64, error) { + var bean T + return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(&bean) +} + +func DeleteByIDs[T any](ctx context.Context, ids ...int64) error { + if len(ids) == 0 { + return nil + } + + var bean T + _, err := GetEngine(ctx).In("id", ids).NoAutoCondition().NoAutoTime().Delete(&bean) + return err +} + +func Delete[T any](ctx context.Context, opts FindOptions) (int64, error) { + if opts == nil || !opts.ToConds().IsValid() { + panic("opts are empty or invalid in db.Delete(ctx, opts). This should not be possible.") + } + + var bean T + return GetEngine(ctx).Where(opts.ToConds()).NoAutoCondition().NoAutoTime().Delete(&bean) +} + // DeleteByBean deletes all records according non-empty fields of the bean as conditions. func DeleteByBean(ctx context.Context, bean any) (int64, error) { return GetEngine(ctx).Delete(bean) } -// DeleteByID deletes the given bean with the given ID -func DeleteByID(ctx context.Context, id int64, bean any) (int64, error) { - return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(bean) -} - // FindIDs finds the IDs for the given table name satisfying the given condition // By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error) { diff --git a/models/db/error.go b/models/db/error.go index f601a15c01..665e970e17 100644 --- a/models/db/error.go +++ b/models/db/error.go @@ -72,21 +72,3 @@ func (err ErrNotExist) Error() string { func (err ErrNotExist) Unwrap() error { return util.ErrNotExist } - -// ErrConditionRequired represents an error which require condition. -type ErrConditionRequired struct{} - -// IsErrConditionRequired checks if an error is an ErrConditionRequired -func IsErrConditionRequired(err error) bool { - _, ok := err.(ErrConditionRequired) - return ok -} - -func (err ErrConditionRequired) Error() string { - return "condition is required" -} - -// Unwrap unwraps this as a ErrNotExist err -func (err ErrConditionRequired) Unwrap() error { - return util.ErrInvalidArgument -} |