// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/exectioninfo"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/timingbreakdown"
	"github.com/buildbarn/bb-portal/ent/gen/ent/timingchild"
)

// TimingBreakdownUpdate is the builder for updating TimingBreakdown entities.
type TimingBreakdownUpdate struct {
	config
	hooks    []Hook
	mutation *TimingBreakdownMutation
}

// Where appends a list predicates to the TimingBreakdownUpdate builder.
func (tbu *TimingBreakdownUpdate) Where(ps ...predicate.TimingBreakdown) *TimingBreakdownUpdate {
	tbu.mutation.Where(ps...)
	return tbu
}

// SetName sets the "name" field.
func (tbu *TimingBreakdownUpdate) SetName(s string) *TimingBreakdownUpdate {
	tbu.mutation.SetName(s)
	return tbu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (tbu *TimingBreakdownUpdate) SetNillableName(s *string) *TimingBreakdownUpdate {
	if s != nil {
		tbu.SetName(*s)
	}
	return tbu
}

// ClearName clears the value of the "name" field.
func (tbu *TimingBreakdownUpdate) ClearName() *TimingBreakdownUpdate {
	tbu.mutation.ClearName()
	return tbu
}

// SetTime sets the "time" field.
func (tbu *TimingBreakdownUpdate) SetTime(s string) *TimingBreakdownUpdate {
	tbu.mutation.SetTime(s)
	return tbu
}

// SetNillableTime sets the "time" field if the given value is not nil.
func (tbu *TimingBreakdownUpdate) SetNillableTime(s *string) *TimingBreakdownUpdate {
	if s != nil {
		tbu.SetTime(*s)
	}
	return tbu
}

// ClearTime clears the value of the "time" field.
func (tbu *TimingBreakdownUpdate) ClearTime() *TimingBreakdownUpdate {
	tbu.mutation.ClearTime()
	return tbu
}

// SetExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID.
func (tbu *TimingBreakdownUpdate) SetExecutionInfoID(id int) *TimingBreakdownUpdate {
	tbu.mutation.SetExecutionInfoID(id)
	return tbu
}

// SetNillableExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID if the given value is not nil.
func (tbu *TimingBreakdownUpdate) SetNillableExecutionInfoID(id *int) *TimingBreakdownUpdate {
	if id != nil {
		tbu = tbu.SetExecutionInfoID(*id)
	}
	return tbu
}

// SetExecutionInfo sets the "execution_info" edge to the ExectionInfo entity.
func (tbu *TimingBreakdownUpdate) SetExecutionInfo(e *ExectionInfo) *TimingBreakdownUpdate {
	return tbu.SetExecutionInfoID(e.ID)
}

// AddChildIDs adds the "child" edge to the TimingChild entity by IDs.
func (tbu *TimingBreakdownUpdate) AddChildIDs(ids ...int) *TimingBreakdownUpdate {
	tbu.mutation.AddChildIDs(ids...)
	return tbu
}

// AddChild adds the "child" edges to the TimingChild entity.
func (tbu *TimingBreakdownUpdate) AddChild(t ...*TimingChild) *TimingBreakdownUpdate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tbu.AddChildIDs(ids...)
}

// Mutation returns the TimingBreakdownMutation object of the builder.
func (tbu *TimingBreakdownUpdate) Mutation() *TimingBreakdownMutation {
	return tbu.mutation
}

// ClearExecutionInfo clears the "execution_info" edge to the ExectionInfo entity.
func (tbu *TimingBreakdownUpdate) ClearExecutionInfo() *TimingBreakdownUpdate {
	tbu.mutation.ClearExecutionInfo()
	return tbu
}

// ClearChild clears all "child" edges to the TimingChild entity.
func (tbu *TimingBreakdownUpdate) ClearChild() *TimingBreakdownUpdate {
	tbu.mutation.ClearChild()
	return tbu
}

// RemoveChildIDs removes the "child" edge to TimingChild entities by IDs.
func (tbu *TimingBreakdownUpdate) RemoveChildIDs(ids ...int) *TimingBreakdownUpdate {
	tbu.mutation.RemoveChildIDs(ids...)
	return tbu
}

// RemoveChild removes "child" edges to TimingChild entities.
func (tbu *TimingBreakdownUpdate) RemoveChild(t ...*TimingChild) *TimingBreakdownUpdate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tbu.RemoveChildIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (tbu *TimingBreakdownUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, tbu.sqlSave, tbu.mutation, tbu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tbu *TimingBreakdownUpdate) SaveX(ctx context.Context) int {
	affected, err := tbu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (tbu *TimingBreakdownUpdate) Exec(ctx context.Context) error {
	_, err := tbu.Save(ctx)
	return err
}

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

func (tbu *TimingBreakdownUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(timingbreakdown.Table, timingbreakdown.Columns, sqlgraph.NewFieldSpec(timingbreakdown.FieldID, field.TypeInt))
	if ps := tbu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tbu.mutation.Name(); ok {
		_spec.SetField(timingbreakdown.FieldName, field.TypeString, value)
	}
	if tbu.mutation.NameCleared() {
		_spec.ClearField(timingbreakdown.FieldName, field.TypeString)
	}
	if value, ok := tbu.mutation.Time(); ok {
		_spec.SetField(timingbreakdown.FieldTime, field.TypeString, value)
	}
	if tbu.mutation.TimeCleared() {
		_spec.ClearField(timingbreakdown.FieldTime, field.TypeString)
	}
	if tbu.mutation.ExecutionInfoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   timingbreakdown.ExecutionInfoTable,
			Columns: []string{timingbreakdown.ExecutionInfoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(exectioninfo.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbu.mutation.ExecutionInfoIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   timingbreakdown.ExecutionInfoTable,
			Columns: []string{timingbreakdown.ExecutionInfoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(exectioninfo.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tbu.mutation.ChildCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbu.mutation.RemovedChildIDs(); len(nodes) > 0 && !tbu.mutation.ChildCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbu.mutation.ChildIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, tbu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{timingbreakdown.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tbu.mutation.done = true
	return n, nil
}

// TimingBreakdownUpdateOne is the builder for updating a single TimingBreakdown entity.
type TimingBreakdownUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TimingBreakdownMutation
}

// SetName sets the "name" field.
func (tbuo *TimingBreakdownUpdateOne) SetName(s string) *TimingBreakdownUpdateOne {
	tbuo.mutation.SetName(s)
	return tbuo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (tbuo *TimingBreakdownUpdateOne) SetNillableName(s *string) *TimingBreakdownUpdateOne {
	if s != nil {
		tbuo.SetName(*s)
	}
	return tbuo
}

// ClearName clears the value of the "name" field.
func (tbuo *TimingBreakdownUpdateOne) ClearName() *TimingBreakdownUpdateOne {
	tbuo.mutation.ClearName()
	return tbuo
}

// SetTime sets the "time" field.
func (tbuo *TimingBreakdownUpdateOne) SetTime(s string) *TimingBreakdownUpdateOne {
	tbuo.mutation.SetTime(s)
	return tbuo
}

// SetNillableTime sets the "time" field if the given value is not nil.
func (tbuo *TimingBreakdownUpdateOne) SetNillableTime(s *string) *TimingBreakdownUpdateOne {
	if s != nil {
		tbuo.SetTime(*s)
	}
	return tbuo
}

// ClearTime clears the value of the "time" field.
func (tbuo *TimingBreakdownUpdateOne) ClearTime() *TimingBreakdownUpdateOne {
	tbuo.mutation.ClearTime()
	return tbuo
}

// SetExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID.
func (tbuo *TimingBreakdownUpdateOne) SetExecutionInfoID(id int) *TimingBreakdownUpdateOne {
	tbuo.mutation.SetExecutionInfoID(id)
	return tbuo
}

// SetNillableExecutionInfoID sets the "execution_info" edge to the ExectionInfo entity by ID if the given value is not nil.
func (tbuo *TimingBreakdownUpdateOne) SetNillableExecutionInfoID(id *int) *TimingBreakdownUpdateOne {
	if id != nil {
		tbuo = tbuo.SetExecutionInfoID(*id)
	}
	return tbuo
}

// SetExecutionInfo sets the "execution_info" edge to the ExectionInfo entity.
func (tbuo *TimingBreakdownUpdateOne) SetExecutionInfo(e *ExectionInfo) *TimingBreakdownUpdateOne {
	return tbuo.SetExecutionInfoID(e.ID)
}

// AddChildIDs adds the "child" edge to the TimingChild entity by IDs.
func (tbuo *TimingBreakdownUpdateOne) AddChildIDs(ids ...int) *TimingBreakdownUpdateOne {
	tbuo.mutation.AddChildIDs(ids...)
	return tbuo
}

// AddChild adds the "child" edges to the TimingChild entity.
func (tbuo *TimingBreakdownUpdateOne) AddChild(t ...*TimingChild) *TimingBreakdownUpdateOne {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tbuo.AddChildIDs(ids...)
}

// Mutation returns the TimingBreakdownMutation object of the builder.
func (tbuo *TimingBreakdownUpdateOne) Mutation() *TimingBreakdownMutation {
	return tbuo.mutation
}

// ClearExecutionInfo clears the "execution_info" edge to the ExectionInfo entity.
func (tbuo *TimingBreakdownUpdateOne) ClearExecutionInfo() *TimingBreakdownUpdateOne {
	tbuo.mutation.ClearExecutionInfo()
	return tbuo
}

// ClearChild clears all "child" edges to the TimingChild entity.
func (tbuo *TimingBreakdownUpdateOne) ClearChild() *TimingBreakdownUpdateOne {
	tbuo.mutation.ClearChild()
	return tbuo
}

// RemoveChildIDs removes the "child" edge to TimingChild entities by IDs.
func (tbuo *TimingBreakdownUpdateOne) RemoveChildIDs(ids ...int) *TimingBreakdownUpdateOne {
	tbuo.mutation.RemoveChildIDs(ids...)
	return tbuo
}

// RemoveChild removes "child" edges to TimingChild entities.
func (tbuo *TimingBreakdownUpdateOne) RemoveChild(t ...*TimingChild) *TimingBreakdownUpdateOne {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tbuo.RemoveChildIDs(ids...)
}

// Where appends a list predicates to the TimingBreakdownUpdate builder.
func (tbuo *TimingBreakdownUpdateOne) Where(ps ...predicate.TimingBreakdown) *TimingBreakdownUpdateOne {
	tbuo.mutation.Where(ps...)
	return tbuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (tbuo *TimingBreakdownUpdateOne) Select(field string, fields ...string) *TimingBreakdownUpdateOne {
	tbuo.fields = append([]string{field}, fields...)
	return tbuo
}

// Save executes the query and returns the updated TimingBreakdown entity.
func (tbuo *TimingBreakdownUpdateOne) Save(ctx context.Context) (*TimingBreakdown, error) {
	return withHooks(ctx, tbuo.sqlSave, tbuo.mutation, tbuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (tbuo *TimingBreakdownUpdateOne) SaveX(ctx context.Context) *TimingBreakdown {
	node, err := tbuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (tbuo *TimingBreakdownUpdateOne) Exec(ctx context.Context) error {
	_, err := tbuo.Save(ctx)
	return err
}

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

func (tbuo *TimingBreakdownUpdateOne) sqlSave(ctx context.Context) (_node *TimingBreakdown, err error) {
	_spec := sqlgraph.NewUpdateSpec(timingbreakdown.Table, timingbreakdown.Columns, sqlgraph.NewFieldSpec(timingbreakdown.FieldID, field.TypeInt))
	id, ok := tbuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TimingBreakdown.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tbuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, timingbreakdown.FieldID)
		for _, f := range fields {
			if !timingbreakdown.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != timingbreakdown.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tbuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tbuo.mutation.Name(); ok {
		_spec.SetField(timingbreakdown.FieldName, field.TypeString, value)
	}
	if tbuo.mutation.NameCleared() {
		_spec.ClearField(timingbreakdown.FieldName, field.TypeString)
	}
	if value, ok := tbuo.mutation.Time(); ok {
		_spec.SetField(timingbreakdown.FieldTime, field.TypeString, value)
	}
	if tbuo.mutation.TimeCleared() {
		_spec.ClearField(timingbreakdown.FieldTime, field.TypeString)
	}
	if tbuo.mutation.ExecutionInfoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   timingbreakdown.ExecutionInfoTable,
			Columns: []string{timingbreakdown.ExecutionInfoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(exectioninfo.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbuo.mutation.ExecutionInfoIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   timingbreakdown.ExecutionInfoTable,
			Columns: []string{timingbreakdown.ExecutionInfoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(exectioninfo.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tbuo.mutation.ChildCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbuo.mutation.RemovedChildIDs(); len(nodes) > 0 && !tbuo.mutation.ChildCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tbuo.mutation.ChildIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   timingbreakdown.ChildTable,
			Columns: []string{timingbreakdown.ChildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &TimingBreakdown{config: tbuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tbuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{timingbreakdown.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tbuo.mutation.done = true
	return _node, nil
}
