// 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/oauthgrant"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
)

// OAuthGrantDelete is the builder for deleting a OAuthGrant entity.
type OAuthGrantDelete struct {
	config
	hooks    []Hook
	mutation *OAuthGrantMutation
}

// Where appends a list predicates to the OAuthGrantDelete builder.
func (ogd *OAuthGrantDelete) Where(ps ...predicate.OAuthGrant) *OAuthGrantDelete {
	ogd.mutation.Where(ps...)
	return ogd
}

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

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

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

// OAuthGrantDeleteOne is the builder for deleting a single OAuthGrant entity.
type OAuthGrantDeleteOne struct {
	ogd *OAuthGrantDelete
}

// Where appends a list predicates to the OAuthGrantDelete builder.
func (ogdo *OAuthGrantDeleteOne) Where(ps ...predicate.OAuthGrant) *OAuthGrantDeleteOne {
	ogdo.ogd.mutation.Where(ps...)
	return ogdo
}

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

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