// Copyright (c) 2004-present Facebook All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

	"github.com/facebook/ent/dialect/sql"
	"github.com/facebook/ent/dialect/sql/sqlgraph"
	"github.com/facebook/ent/schema/field"
	"github.com/facebookincubator/symphony/pkg/ent/predicate"
	"github.com/facebookincubator/symphony/pkg/ent/rulelimit"
)

// RuleLimitDelete is the builder for deleting a RuleLimit entity.
type RuleLimitDelete struct {
	config
	hooks    []Hook
	mutation *RuleLimitMutation
}

// Where adds a new predicate to the delete builder.
func (rld *RuleLimitDelete) Where(ps ...predicate.RuleLimit) *RuleLimitDelete {
	rld.mutation.predicates = append(rld.mutation.predicates, ps...)
	return rld
}

// Exec executes the deletion query and returns how many vertices were deleted.
func (rld *RuleLimitDelete) Exec(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	if len(rld.hooks) == 0 {
		affected, err = rld.sqlExec(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*RuleLimitMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			rld.mutation = mutation
			affected, err = rld.sqlExec(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(rld.hooks) - 1; i >= 0; i-- {
			mut = rld.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, rld.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

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

func (rld *RuleLimitDelete) sqlExec(ctx context.Context) (int, error) {
	_spec := &sqlgraph.DeleteSpec{
		Node: &sqlgraph.NodeSpec{
			Table: rulelimit.Table,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: rulelimit.FieldID,
			},
		},
	}
	if ps := rld.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return sqlgraph.DeleteNodes(ctx, rld.driver, _spec)
}

// RuleLimitDeleteOne is the builder for deleting a single RuleLimit entity.
type RuleLimitDeleteOne struct {
	rld *RuleLimitDelete
}

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

// ExecX is like Exec, but panics if an error occurs.
func (rldo *RuleLimitDeleteOne) ExecX(ctx context.Context) {
	rldo.rld.ExecX(ctx)
}
