// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"

	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/internal"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/predicate"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/testbedinfo"
)

// TestbedInfoDelete is the builder for deleting a TestbedInfo entity.
type TestbedInfoDelete struct {
	config
	hooks    []Hook
	mutation *TestbedInfoMutation
}

// Where appends a list predicates to the TestbedInfoDelete builder.
func (tid *TestbedInfoDelete) Where(ps ...predicate.TestbedInfo) *TestbedInfoDelete {
	tid.mutation.Where(ps...)
	return tid
}

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

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

func (tid *TestbedInfoDelete) sqlExec(ctx context.Context) (int, error) {
	_spec := &sqlgraph.DeleteSpec{
		Node: &sqlgraph.NodeSpec{
			Table: testbedinfo.Table,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeUint64,
				Column: testbedinfo.FieldID,
			},
		},
	}
	_spec.Node.Schema = tid.schemaConfig.TestbedInfo
	ctx = internal.NewSchemaConfigContext(ctx, tid.schemaConfig)
	if ps := tid.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return sqlgraph.DeleteNodes(ctx, tid.driver, _spec)
}

// TestbedInfoDeleteOne is the builder for deleting a single TestbedInfo entity.
type TestbedInfoDeleteOne struct {
	tid *TestbedInfoDelete
}

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

// ExecX is like Exec, but panics if an error occurs.
func (tido *TestbedInfoDeleteOne) ExecX(ctx context.Context) {
	tido.tid.ExecX(ctx)
}
