// 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/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/timingbreakdown"
	"github.com/buildbarn/bb-portal/ent/gen/ent/timingchild"
)

// TimingChildUpdate is the builder for updating TimingChild entities.
type TimingChildUpdate struct {
	config
	hooks    []Hook
	mutation *TimingChildMutation
}

// Where appends a list predicates to the TimingChildUpdate builder.
func (tcu *TimingChildUpdate) Where(ps ...predicate.TimingChild) *TimingChildUpdate {
	tcu.mutation.Where(ps...)
	return tcu
}

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

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

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

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

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

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

// SetTimingBreakdownID sets the "timing_breakdown" edge to the TimingBreakdown entity by ID.
func (tcu *TimingChildUpdate) SetTimingBreakdownID(id int) *TimingChildUpdate {
	tcu.mutation.SetTimingBreakdownID(id)
	return tcu
}

// SetNillableTimingBreakdownID sets the "timing_breakdown" edge to the TimingBreakdown entity by ID if the given value is not nil.
func (tcu *TimingChildUpdate) SetNillableTimingBreakdownID(id *int) *TimingChildUpdate {
	if id != nil {
		tcu = tcu.SetTimingBreakdownID(*id)
	}
	return tcu
}

// SetTimingBreakdown sets the "timing_breakdown" edge to the TimingBreakdown entity.
func (tcu *TimingChildUpdate) SetTimingBreakdown(t *TimingBreakdown) *TimingChildUpdate {
	return tcu.SetTimingBreakdownID(t.ID)
}

// Mutation returns the TimingChildMutation object of the builder.
func (tcu *TimingChildUpdate) Mutation() *TimingChildMutation {
	return tcu.mutation
}

// ClearTimingBreakdown clears the "timing_breakdown" edge to the TimingBreakdown entity.
func (tcu *TimingChildUpdate) ClearTimingBreakdown() *TimingChildUpdate {
	tcu.mutation.ClearTimingBreakdown()
	return tcu
}

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

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

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

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

func (tcu *TimingChildUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(timingchild.Table, timingchild.Columns, sqlgraph.NewFieldSpec(timingchild.FieldID, field.TypeInt))
	if ps := tcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tcu.mutation.Name(); ok {
		_spec.SetField(timingchild.FieldName, field.TypeString, value)
	}
	if tcu.mutation.NameCleared() {
		_spec.ClearField(timingchild.FieldName, field.TypeString)
	}
	if value, ok := tcu.mutation.Time(); ok {
		_spec.SetField(timingchild.FieldTime, field.TypeString, value)
	}
	if tcu.mutation.TimeCleared() {
		_spec.ClearField(timingchild.FieldTime, field.TypeString)
	}
	if tcu.mutation.TimingBreakdownCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timingchild.TimingBreakdownTable,
			Columns: []string{timingchild.TimingBreakdownColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingbreakdown.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tcu.mutation.TimingBreakdownIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timingchild.TimingBreakdownTable,
			Columns: []string{timingchild.TimingBreakdownColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timingbreakdown.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, tcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{timingchild.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tcu.mutation.done = true
	return n, nil
}

// TimingChildUpdateOne is the builder for updating a single TimingChild entity.
type TimingChildUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TimingChildMutation
}

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

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

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

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

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

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

// SetTimingBreakdownID sets the "timing_breakdown" edge to the TimingBreakdown entity by ID.
func (tcuo *TimingChildUpdateOne) SetTimingBreakdownID(id int) *TimingChildUpdateOne {
	tcuo.mutation.SetTimingBreakdownID(id)
	return tcuo
}

// SetNillableTimingBreakdownID sets the "timing_breakdown" edge to the TimingBreakdown entity by ID if the given value is not nil.
func (tcuo *TimingChildUpdateOne) SetNillableTimingBreakdownID(id *int) *TimingChildUpdateOne {
	if id != nil {
		tcuo = tcuo.SetTimingBreakdownID(*id)
	}
	return tcuo
}

// SetTimingBreakdown sets the "timing_breakdown" edge to the TimingBreakdown entity.
func (tcuo *TimingChildUpdateOne) SetTimingBreakdown(t *TimingBreakdown) *TimingChildUpdateOne {
	return tcuo.SetTimingBreakdownID(t.ID)
}

// Mutation returns the TimingChildMutation object of the builder.
func (tcuo *TimingChildUpdateOne) Mutation() *TimingChildMutation {
	return tcuo.mutation
}

// ClearTimingBreakdown clears the "timing_breakdown" edge to the TimingBreakdown entity.
func (tcuo *TimingChildUpdateOne) ClearTimingBreakdown() *TimingChildUpdateOne {
	tcuo.mutation.ClearTimingBreakdown()
	return tcuo
}

// Where appends a list predicates to the TimingChildUpdate builder.
func (tcuo *TimingChildUpdateOne) Where(ps ...predicate.TimingChild) *TimingChildUpdateOne {
	tcuo.mutation.Where(ps...)
	return tcuo
}

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

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

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

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

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

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