// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
	"github.com/cloudreve/Cloudreve/v4/ent/storagepolicy"
)

// StoragePolicyDelete is the builder for deleting a StoragePolicy entity.
type StoragePolicyDelete struct {
	config
	hooks    []Hook
	mutation *StoragePolicyMutation
}

// Where appends a list predicates to the StoragePolicyDelete builder.
func (spd *StoragePolicyDelete) Where(ps ...predicate.StoragePolicy) *StoragePolicyDelete {
	spd.mutation.Where(ps...)
	return spd
}

// Exec executes the deletion query and returns how many vertices were deleted.
func (spd *StoragePolicyDelete) Exec(ctx context.Context) (int, error) {
	return withHooks(ctx, spd.sqlExec, spd.mutation, spd.hooks)
}

// ExecX is like Exec, but panics if an error occurs.
func (spd *StoragePolicyDelete) ExecX(ctx context.Context) int {
	n, err := spd.Exec(ctx)
	if err != nil {
		panic(err)
	}
	return n
}

func (spd *StoragePolicyDelete) sqlExec(ctx context.Context) (int, error) {
	_spec := sqlgraph.NewDeleteSpec(storagepolicy.Table, sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt))
	if ps := spd.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	affected, err := sqlgraph.DeleteNodes(ctx, spd.driver, _spec)
	if err != nil && sqlgraph.IsConstraintError(err) {
		err = &ConstraintError{msg: err.Error(), wrap: err}
	}
	spd.mutation.done = true
	return affected, err
}

// StoragePolicyDeleteOne is the builder for deleting a single StoragePolicy entity.
type StoragePolicyDeleteOne struct {
	spd *StoragePolicyDelete
}

// Where appends a list predicates to the StoragePolicyDelete builder.
func (spdo *StoragePolicyDeleteOne) Where(ps ...predicate.StoragePolicy) *StoragePolicyDeleteOne {
	spdo.spd.mutation.Where(ps...)
	return spdo
}

// Exec executes the deletion query.
func (spdo *StoragePolicyDeleteOne) Exec(ctx context.Context) error {
	n, err := spdo.spd.Exec(ctx)
	switch {
	case err != nil:
		return err
	case n == 0:
		return &NotFoundError{storagepolicy.Label}
	default:
		return nil
	}
}

// ExecX is like Exec, but panics if an error occurs.
func (spdo *StoragePolicyDeleteOne) ExecX(ctx context.Context) {
	if err := spdo.Exec(ctx); err != nil {
		panic(err)
	}
}
