// 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/google/uuid"
	"github.com/in-toto/archivista/ent/predicate"
	"github.com/in-toto/archivista/ent/subject"
	"github.com/in-toto/archivista/ent/subjectdigest"
)

// SubjectDigestUpdate is the builder for updating SubjectDigest entities.
type SubjectDigestUpdate struct {
	config
	hooks    []Hook
	mutation *SubjectDigestMutation
}

// Where appends a list predicates to the SubjectDigestUpdate builder.
func (_u *SubjectDigestUpdate) Where(ps ...predicate.SubjectDigest) *SubjectDigestUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetAlgorithm sets the "algorithm" field.
func (_u *SubjectDigestUpdate) SetAlgorithm(v string) *SubjectDigestUpdate {
	_u.mutation.SetAlgorithm(v)
	return _u
}

// SetNillableAlgorithm sets the "algorithm" field if the given value is not nil.
func (_u *SubjectDigestUpdate) SetNillableAlgorithm(v *string) *SubjectDigestUpdate {
	if v != nil {
		_u.SetAlgorithm(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *SubjectDigestUpdate) SetValue(v string) *SubjectDigestUpdate {
	_u.mutation.SetValue(v)
	return _u
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *SubjectDigestUpdate) SetNillableValue(v *string) *SubjectDigestUpdate {
	if v != nil {
		_u.SetValue(*v)
	}
	return _u
}

// SetSubjectID sets the "subject" edge to the Subject entity by ID.
func (_u *SubjectDigestUpdate) SetSubjectID(id uuid.UUID) *SubjectDigestUpdate {
	_u.mutation.SetSubjectID(id)
	return _u
}

// SetNillableSubjectID sets the "subject" edge to the Subject entity by ID if the given value is not nil.
func (_u *SubjectDigestUpdate) SetNillableSubjectID(id *uuid.UUID) *SubjectDigestUpdate {
	if id != nil {
		_u = _u.SetSubjectID(*id)
	}
	return _u
}

// SetSubject sets the "subject" edge to the Subject entity.
func (_u *SubjectDigestUpdate) SetSubject(v *Subject) *SubjectDigestUpdate {
	return _u.SetSubjectID(v.ID)
}

// Mutation returns the SubjectDigestMutation object of the builder.
func (_u *SubjectDigestUpdate) Mutation() *SubjectDigestMutation {
	return _u.mutation
}

// ClearSubject clears the "subject" edge to the Subject entity.
func (_u *SubjectDigestUpdate) ClearSubject() *SubjectDigestUpdate {
	_u.mutation.ClearSubject()
	return _u
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *SubjectDigestUpdate) check() error {
	if v, ok := _u.mutation.Algorithm(); ok {
		if err := subjectdigest.AlgorithmValidator(v); err != nil {
			return &ValidationError{Name: "algorithm", err: fmt.Errorf(`ent: validator failed for field "SubjectDigest.algorithm": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Value(); ok {
		if err := subjectdigest.ValueValidator(v); err != nil {
			return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "SubjectDigest.value": %w`, err)}
		}
	}
	return nil
}

func (_u *SubjectDigestUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(subjectdigest.Table, subjectdigest.Columns, sqlgraph.NewFieldSpec(subjectdigest.FieldID, field.TypeUUID))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Algorithm(); ok {
		_spec.SetField(subjectdigest.FieldAlgorithm, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(subjectdigest.FieldValue, field.TypeString, value)
	}
	if _u.mutation.SubjectCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   subjectdigest.SubjectTable,
			Columns: []string{subjectdigest.SubjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SubjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   subjectdigest.SubjectTable,
			Columns: []string{subjectdigest.SubjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{subjectdigest.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// SubjectDigestUpdateOne is the builder for updating a single SubjectDigest entity.
type SubjectDigestUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SubjectDigestMutation
}

// SetAlgorithm sets the "algorithm" field.
func (_u *SubjectDigestUpdateOne) SetAlgorithm(v string) *SubjectDigestUpdateOne {
	_u.mutation.SetAlgorithm(v)
	return _u
}

// SetNillableAlgorithm sets the "algorithm" field if the given value is not nil.
func (_u *SubjectDigestUpdateOne) SetNillableAlgorithm(v *string) *SubjectDigestUpdateOne {
	if v != nil {
		_u.SetAlgorithm(*v)
	}
	return _u
}

// SetValue sets the "value" field.
func (_u *SubjectDigestUpdateOne) SetValue(v string) *SubjectDigestUpdateOne {
	_u.mutation.SetValue(v)
	return _u
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *SubjectDigestUpdateOne) SetNillableValue(v *string) *SubjectDigestUpdateOne {
	if v != nil {
		_u.SetValue(*v)
	}
	return _u
}

// SetSubjectID sets the "subject" edge to the Subject entity by ID.
func (_u *SubjectDigestUpdateOne) SetSubjectID(id uuid.UUID) *SubjectDigestUpdateOne {
	_u.mutation.SetSubjectID(id)
	return _u
}

// SetNillableSubjectID sets the "subject" edge to the Subject entity by ID if the given value is not nil.
func (_u *SubjectDigestUpdateOne) SetNillableSubjectID(id *uuid.UUID) *SubjectDigestUpdateOne {
	if id != nil {
		_u = _u.SetSubjectID(*id)
	}
	return _u
}

// SetSubject sets the "subject" edge to the Subject entity.
func (_u *SubjectDigestUpdateOne) SetSubject(v *Subject) *SubjectDigestUpdateOne {
	return _u.SetSubjectID(v.ID)
}

// Mutation returns the SubjectDigestMutation object of the builder.
func (_u *SubjectDigestUpdateOne) Mutation() *SubjectDigestMutation {
	return _u.mutation
}

// ClearSubject clears the "subject" edge to the Subject entity.
func (_u *SubjectDigestUpdateOne) ClearSubject() *SubjectDigestUpdateOne {
	_u.mutation.ClearSubject()
	return _u
}

// Where appends a list predicates to the SubjectDigestUpdate builder.
func (_u *SubjectDigestUpdateOne) Where(ps ...predicate.SubjectDigest) *SubjectDigestUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *SubjectDigestUpdateOne) check() error {
	if v, ok := _u.mutation.Algorithm(); ok {
		if err := subjectdigest.AlgorithmValidator(v); err != nil {
			return &ValidationError{Name: "algorithm", err: fmt.Errorf(`ent: validator failed for field "SubjectDigest.algorithm": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Value(); ok {
		if err := subjectdigest.ValueValidator(v); err != nil {
			return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "SubjectDigest.value": %w`, err)}
		}
	}
	return nil
}

func (_u *SubjectDigestUpdateOne) sqlSave(ctx context.Context) (_node *SubjectDigest, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(subjectdigest.Table, subjectdigest.Columns, sqlgraph.NewFieldSpec(subjectdigest.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SubjectDigest.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, subjectdigest.FieldID)
		for _, f := range fields {
			if !subjectdigest.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != subjectdigest.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Algorithm(); ok {
		_spec.SetField(subjectdigest.FieldAlgorithm, field.TypeString, value)
	}
	if value, ok := _u.mutation.Value(); ok {
		_spec.SetField(subjectdigest.FieldValue, field.TypeString, value)
	}
	if _u.mutation.SubjectCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   subjectdigest.SubjectTable,
			Columns: []string{subjectdigest.SubjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SubjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   subjectdigest.SubjectTable,
			Columns: []string{subjectdigest.SubjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &SubjectDigest{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{subjectdigest.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
