// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

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

// BuildUpdate is the builder for updating Build entities.
type BuildUpdate struct {
	config
	hooks    []Hook
	mutation *BuildMutation
}

// Where appends a list predicates to the BuildUpdate builder.
func (bu *BuildUpdate) Where(ps ...predicate.Build) *BuildUpdate {
	bu.mutation.Where(ps...)
	return bu
}

// SetEnv sets the "env" field.
func (bu *BuildUpdate) SetEnv(m map[string]string) *BuildUpdate {
	bu.mutation.SetEnv(m)
	return bu
}

// SetTimestamp sets the "timestamp" field.
func (bu *BuildUpdate) SetTimestamp(t time.Time) *BuildUpdate {
	bu.mutation.SetTimestamp(t)
	return bu
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (bu *BuildUpdate) SetNillableTimestamp(t *time.Time) *BuildUpdate {
	if t != nil {
		bu.SetTimestamp(*t)
	}
	return bu
}

// ClearTimestamp clears the value of the "timestamp" field.
func (bu *BuildUpdate) ClearTimestamp() *BuildUpdate {
	bu.mutation.ClearTimestamp()
	return bu
}

// AddInvocationIDs adds the "invocations" edge to the BazelInvocation entity by IDs.
func (bu *BuildUpdate) AddInvocationIDs(ids ...int) *BuildUpdate {
	bu.mutation.AddInvocationIDs(ids...)
	return bu
}

// AddInvocations adds the "invocations" edges to the BazelInvocation entity.
func (bu *BuildUpdate) AddInvocations(b ...*BazelInvocation) *BuildUpdate {
	ids := make([]int, len(b))
	for i := range b {
		ids[i] = b[i].ID
	}
	return bu.AddInvocationIDs(ids...)
}

// Mutation returns the BuildMutation object of the builder.
func (bu *BuildUpdate) Mutation() *BuildMutation {
	return bu.mutation
}

// ClearInvocations clears all "invocations" edges to the BazelInvocation entity.
func (bu *BuildUpdate) ClearInvocations() *BuildUpdate {
	bu.mutation.ClearInvocations()
	return bu
}

// RemoveInvocationIDs removes the "invocations" edge to BazelInvocation entities by IDs.
func (bu *BuildUpdate) RemoveInvocationIDs(ids ...int) *BuildUpdate {
	bu.mutation.RemoveInvocationIDs(ids...)
	return bu
}

// RemoveInvocations removes "invocations" edges to BazelInvocation entities.
func (bu *BuildUpdate) RemoveInvocations(b ...*BazelInvocation) *BuildUpdate {
	ids := make([]int, len(b))
	for i := range b {
		ids[i] = b[i].ID
	}
	return bu.RemoveInvocationIDs(ids...)
}

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

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

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

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

func (bu *BuildUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(build.Table, build.Columns, sqlgraph.NewFieldSpec(build.FieldID, field.TypeInt))
	if ps := bu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := bu.mutation.Env(); ok {
		_spec.SetField(build.FieldEnv, field.TypeJSON, value)
	}
	if value, ok := bu.mutation.Timestamp(); ok {
		_spec.SetField(build.FieldTimestamp, field.TypeTime, value)
	}
	if bu.mutation.TimestampCleared() {
		_spec.ClearField(build.FieldTimestamp, field.TypeTime)
	}
	if bu.mutation.InvocationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := bu.mutation.RemovedInvocationsIDs(); len(nodes) > 0 && !bu.mutation.InvocationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.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 := bu.mutation.InvocationsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.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, bu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{build.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	bu.mutation.done = true
	return n, nil
}

// BuildUpdateOne is the builder for updating a single Build entity.
type BuildUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *BuildMutation
}

// SetEnv sets the "env" field.
func (buo *BuildUpdateOne) SetEnv(m map[string]string) *BuildUpdateOne {
	buo.mutation.SetEnv(m)
	return buo
}

// SetTimestamp sets the "timestamp" field.
func (buo *BuildUpdateOne) SetTimestamp(t time.Time) *BuildUpdateOne {
	buo.mutation.SetTimestamp(t)
	return buo
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (buo *BuildUpdateOne) SetNillableTimestamp(t *time.Time) *BuildUpdateOne {
	if t != nil {
		buo.SetTimestamp(*t)
	}
	return buo
}

// ClearTimestamp clears the value of the "timestamp" field.
func (buo *BuildUpdateOne) ClearTimestamp() *BuildUpdateOne {
	buo.mutation.ClearTimestamp()
	return buo
}

// AddInvocationIDs adds the "invocations" edge to the BazelInvocation entity by IDs.
func (buo *BuildUpdateOne) AddInvocationIDs(ids ...int) *BuildUpdateOne {
	buo.mutation.AddInvocationIDs(ids...)
	return buo
}

// AddInvocations adds the "invocations" edges to the BazelInvocation entity.
func (buo *BuildUpdateOne) AddInvocations(b ...*BazelInvocation) *BuildUpdateOne {
	ids := make([]int, len(b))
	for i := range b {
		ids[i] = b[i].ID
	}
	return buo.AddInvocationIDs(ids...)
}

// Mutation returns the BuildMutation object of the builder.
func (buo *BuildUpdateOne) Mutation() *BuildMutation {
	return buo.mutation
}

// ClearInvocations clears all "invocations" edges to the BazelInvocation entity.
func (buo *BuildUpdateOne) ClearInvocations() *BuildUpdateOne {
	buo.mutation.ClearInvocations()
	return buo
}

// RemoveInvocationIDs removes the "invocations" edge to BazelInvocation entities by IDs.
func (buo *BuildUpdateOne) RemoveInvocationIDs(ids ...int) *BuildUpdateOne {
	buo.mutation.RemoveInvocationIDs(ids...)
	return buo
}

// RemoveInvocations removes "invocations" edges to BazelInvocation entities.
func (buo *BuildUpdateOne) RemoveInvocations(b ...*BazelInvocation) *BuildUpdateOne {
	ids := make([]int, len(b))
	for i := range b {
		ids[i] = b[i].ID
	}
	return buo.RemoveInvocationIDs(ids...)
}

// Where appends a list predicates to the BuildUpdate builder.
func (buo *BuildUpdateOne) Where(ps ...predicate.Build) *BuildUpdateOne {
	buo.mutation.Where(ps...)
	return buo
}

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

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

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

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

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

func (buo *BuildUpdateOne) sqlSave(ctx context.Context) (_node *Build, err error) {
	_spec := sqlgraph.NewUpdateSpec(build.Table, build.Columns, sqlgraph.NewFieldSpec(build.FieldID, field.TypeInt))
	id, ok := buo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Build.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := buo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, build.FieldID)
		for _, f := range fields {
			if !build.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != build.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := buo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := buo.mutation.Env(); ok {
		_spec.SetField(build.FieldEnv, field.TypeJSON, value)
	}
	if value, ok := buo.mutation.Timestamp(); ok {
		_spec.SetField(build.FieldTimestamp, field.TypeTime, value)
	}
	if buo.mutation.TimestampCleared() {
		_spec.ClearField(build.FieldTimestamp, field.TypeTime)
	}
	if buo.mutation.InvocationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := buo.mutation.RemovedInvocationsIDs(); len(nodes) > 0 && !buo.mutation.InvocationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.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 := buo.mutation.InvocationsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   build.InvocationsTable,
			Columns: []string{build.InvocationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Build{config: buo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, buo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{build.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	buo.mutation.done = true
	return _node, nil
}
