// 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/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testfile"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testresultbes"
)

// TestFileUpdate is the builder for updating TestFile entities.
type TestFileUpdate struct {
	config
	hooks    []Hook
	mutation *TestFileMutation
}

// Where appends a list predicates to the TestFileUpdate builder.
func (tfu *TestFileUpdate) Where(ps ...predicate.TestFile) *TestFileUpdate {
	tfu.mutation.Where(ps...)
	return tfu
}

// SetDigest sets the "digest" field.
func (tfu *TestFileUpdate) SetDigest(s string) *TestFileUpdate {
	tfu.mutation.SetDigest(s)
	return tfu
}

// SetNillableDigest sets the "digest" field if the given value is not nil.
func (tfu *TestFileUpdate) SetNillableDigest(s *string) *TestFileUpdate {
	if s != nil {
		tfu.SetDigest(*s)
	}
	return tfu
}

// ClearDigest clears the value of the "digest" field.
func (tfu *TestFileUpdate) ClearDigest() *TestFileUpdate {
	tfu.mutation.ClearDigest()
	return tfu
}

// SetFile sets the "file" field.
func (tfu *TestFileUpdate) SetFile(s string) *TestFileUpdate {
	tfu.mutation.SetFile(s)
	return tfu
}

// SetNillableFile sets the "file" field if the given value is not nil.
func (tfu *TestFileUpdate) SetNillableFile(s *string) *TestFileUpdate {
	if s != nil {
		tfu.SetFile(*s)
	}
	return tfu
}

// ClearFile clears the value of the "file" field.
func (tfu *TestFileUpdate) ClearFile() *TestFileUpdate {
	tfu.mutation.ClearFile()
	return tfu
}

// SetLength sets the "length" field.
func (tfu *TestFileUpdate) SetLength(i int64) *TestFileUpdate {
	tfu.mutation.ResetLength()
	tfu.mutation.SetLength(i)
	return tfu
}

// SetNillableLength sets the "length" field if the given value is not nil.
func (tfu *TestFileUpdate) SetNillableLength(i *int64) *TestFileUpdate {
	if i != nil {
		tfu.SetLength(*i)
	}
	return tfu
}

// AddLength adds i to the "length" field.
func (tfu *TestFileUpdate) AddLength(i int64) *TestFileUpdate {
	tfu.mutation.AddLength(i)
	return tfu
}

// ClearLength clears the value of the "length" field.
func (tfu *TestFileUpdate) ClearLength() *TestFileUpdate {
	tfu.mutation.ClearLength()
	return tfu
}

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

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

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

// SetPrefix sets the "prefix" field.
func (tfu *TestFileUpdate) SetPrefix(s []string) *TestFileUpdate {
	tfu.mutation.SetPrefix(s)
	return tfu
}

// AppendPrefix appends s to the "prefix" field.
func (tfu *TestFileUpdate) AppendPrefix(s []string) *TestFileUpdate {
	tfu.mutation.AppendPrefix(s)
	return tfu
}

// ClearPrefix clears the value of the "prefix" field.
func (tfu *TestFileUpdate) ClearPrefix() *TestFileUpdate {
	tfu.mutation.ClearPrefix()
	return tfu
}

// SetTestResultID sets the "test_result" edge to the TestResultBES entity by ID.
func (tfu *TestFileUpdate) SetTestResultID(id int) *TestFileUpdate {
	tfu.mutation.SetTestResultID(id)
	return tfu
}

// SetNillableTestResultID sets the "test_result" edge to the TestResultBES entity by ID if the given value is not nil.
func (tfu *TestFileUpdate) SetNillableTestResultID(id *int) *TestFileUpdate {
	if id != nil {
		tfu = tfu.SetTestResultID(*id)
	}
	return tfu
}

// SetTestResult sets the "test_result" edge to the TestResultBES entity.
func (tfu *TestFileUpdate) SetTestResult(t *TestResultBES) *TestFileUpdate {
	return tfu.SetTestResultID(t.ID)
}

// Mutation returns the TestFileMutation object of the builder.
func (tfu *TestFileUpdate) Mutation() *TestFileMutation {
	return tfu.mutation
}

// ClearTestResult clears the "test_result" edge to the TestResultBES entity.
func (tfu *TestFileUpdate) ClearTestResult() *TestFileUpdate {
	tfu.mutation.ClearTestResult()
	return tfu
}

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

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

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

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

func (tfu *TestFileUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(testfile.Table, testfile.Columns, sqlgraph.NewFieldSpec(testfile.FieldID, field.TypeInt))
	if ps := tfu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tfu.mutation.Digest(); ok {
		_spec.SetField(testfile.FieldDigest, field.TypeString, value)
	}
	if tfu.mutation.DigestCleared() {
		_spec.ClearField(testfile.FieldDigest, field.TypeString)
	}
	if value, ok := tfu.mutation.File(); ok {
		_spec.SetField(testfile.FieldFile, field.TypeString, value)
	}
	if tfu.mutation.FileCleared() {
		_spec.ClearField(testfile.FieldFile, field.TypeString)
	}
	if value, ok := tfu.mutation.Length(); ok {
		_spec.SetField(testfile.FieldLength, field.TypeInt64, value)
	}
	if value, ok := tfu.mutation.AddedLength(); ok {
		_spec.AddField(testfile.FieldLength, field.TypeInt64, value)
	}
	if tfu.mutation.LengthCleared() {
		_spec.ClearField(testfile.FieldLength, field.TypeInt64)
	}
	if value, ok := tfu.mutation.Name(); ok {
		_spec.SetField(testfile.FieldName, field.TypeString, value)
	}
	if tfu.mutation.NameCleared() {
		_spec.ClearField(testfile.FieldName, field.TypeString)
	}
	if value, ok := tfu.mutation.Prefix(); ok {
		_spec.SetField(testfile.FieldPrefix, field.TypeJSON, value)
	}
	if value, ok := tfu.mutation.AppendedPrefix(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, testfile.FieldPrefix, value)
		})
	}
	if tfu.mutation.PrefixCleared() {
		_spec.ClearField(testfile.FieldPrefix, field.TypeJSON)
	}
	if tfu.mutation.TestResultCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testfile.TestResultTable,
			Columns: []string{testfile.TestResultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tfu.mutation.TestResultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testfile.TestResultTable,
			Columns: []string{testfile.TestResultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.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, tfu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{testfile.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tfu.mutation.done = true
	return n, nil
}

// TestFileUpdateOne is the builder for updating a single TestFile entity.
type TestFileUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TestFileMutation
}

// SetDigest sets the "digest" field.
func (tfuo *TestFileUpdateOne) SetDigest(s string) *TestFileUpdateOne {
	tfuo.mutation.SetDigest(s)
	return tfuo
}

// SetNillableDigest sets the "digest" field if the given value is not nil.
func (tfuo *TestFileUpdateOne) SetNillableDigest(s *string) *TestFileUpdateOne {
	if s != nil {
		tfuo.SetDigest(*s)
	}
	return tfuo
}

// ClearDigest clears the value of the "digest" field.
func (tfuo *TestFileUpdateOne) ClearDigest() *TestFileUpdateOne {
	tfuo.mutation.ClearDigest()
	return tfuo
}

// SetFile sets the "file" field.
func (tfuo *TestFileUpdateOne) SetFile(s string) *TestFileUpdateOne {
	tfuo.mutation.SetFile(s)
	return tfuo
}

// SetNillableFile sets the "file" field if the given value is not nil.
func (tfuo *TestFileUpdateOne) SetNillableFile(s *string) *TestFileUpdateOne {
	if s != nil {
		tfuo.SetFile(*s)
	}
	return tfuo
}

// ClearFile clears the value of the "file" field.
func (tfuo *TestFileUpdateOne) ClearFile() *TestFileUpdateOne {
	tfuo.mutation.ClearFile()
	return tfuo
}

// SetLength sets the "length" field.
func (tfuo *TestFileUpdateOne) SetLength(i int64) *TestFileUpdateOne {
	tfuo.mutation.ResetLength()
	tfuo.mutation.SetLength(i)
	return tfuo
}

// SetNillableLength sets the "length" field if the given value is not nil.
func (tfuo *TestFileUpdateOne) SetNillableLength(i *int64) *TestFileUpdateOne {
	if i != nil {
		tfuo.SetLength(*i)
	}
	return tfuo
}

// AddLength adds i to the "length" field.
func (tfuo *TestFileUpdateOne) AddLength(i int64) *TestFileUpdateOne {
	tfuo.mutation.AddLength(i)
	return tfuo
}

// ClearLength clears the value of the "length" field.
func (tfuo *TestFileUpdateOne) ClearLength() *TestFileUpdateOne {
	tfuo.mutation.ClearLength()
	return tfuo
}

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

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

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

// SetPrefix sets the "prefix" field.
func (tfuo *TestFileUpdateOne) SetPrefix(s []string) *TestFileUpdateOne {
	tfuo.mutation.SetPrefix(s)
	return tfuo
}

// AppendPrefix appends s to the "prefix" field.
func (tfuo *TestFileUpdateOne) AppendPrefix(s []string) *TestFileUpdateOne {
	tfuo.mutation.AppendPrefix(s)
	return tfuo
}

// ClearPrefix clears the value of the "prefix" field.
func (tfuo *TestFileUpdateOne) ClearPrefix() *TestFileUpdateOne {
	tfuo.mutation.ClearPrefix()
	return tfuo
}

// SetTestResultID sets the "test_result" edge to the TestResultBES entity by ID.
func (tfuo *TestFileUpdateOne) SetTestResultID(id int) *TestFileUpdateOne {
	tfuo.mutation.SetTestResultID(id)
	return tfuo
}

// SetNillableTestResultID sets the "test_result" edge to the TestResultBES entity by ID if the given value is not nil.
func (tfuo *TestFileUpdateOne) SetNillableTestResultID(id *int) *TestFileUpdateOne {
	if id != nil {
		tfuo = tfuo.SetTestResultID(*id)
	}
	return tfuo
}

// SetTestResult sets the "test_result" edge to the TestResultBES entity.
func (tfuo *TestFileUpdateOne) SetTestResult(t *TestResultBES) *TestFileUpdateOne {
	return tfuo.SetTestResultID(t.ID)
}

// Mutation returns the TestFileMutation object of the builder.
func (tfuo *TestFileUpdateOne) Mutation() *TestFileMutation {
	return tfuo.mutation
}

// ClearTestResult clears the "test_result" edge to the TestResultBES entity.
func (tfuo *TestFileUpdateOne) ClearTestResult() *TestFileUpdateOne {
	tfuo.mutation.ClearTestResult()
	return tfuo
}

// Where appends a list predicates to the TestFileUpdate builder.
func (tfuo *TestFileUpdateOne) Where(ps ...predicate.TestFile) *TestFileUpdateOne {
	tfuo.mutation.Where(ps...)
	return tfuo
}

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

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

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

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

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

func (tfuo *TestFileUpdateOne) sqlSave(ctx context.Context) (_node *TestFile, err error) {
	_spec := sqlgraph.NewUpdateSpec(testfile.Table, testfile.Columns, sqlgraph.NewFieldSpec(testfile.FieldID, field.TypeInt))
	id, ok := tfuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TestFile.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tfuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, testfile.FieldID)
		for _, f := range fields {
			if !testfile.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != testfile.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tfuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tfuo.mutation.Digest(); ok {
		_spec.SetField(testfile.FieldDigest, field.TypeString, value)
	}
	if tfuo.mutation.DigestCleared() {
		_spec.ClearField(testfile.FieldDigest, field.TypeString)
	}
	if value, ok := tfuo.mutation.File(); ok {
		_spec.SetField(testfile.FieldFile, field.TypeString, value)
	}
	if tfuo.mutation.FileCleared() {
		_spec.ClearField(testfile.FieldFile, field.TypeString)
	}
	if value, ok := tfuo.mutation.Length(); ok {
		_spec.SetField(testfile.FieldLength, field.TypeInt64, value)
	}
	if value, ok := tfuo.mutation.AddedLength(); ok {
		_spec.AddField(testfile.FieldLength, field.TypeInt64, value)
	}
	if tfuo.mutation.LengthCleared() {
		_spec.ClearField(testfile.FieldLength, field.TypeInt64)
	}
	if value, ok := tfuo.mutation.Name(); ok {
		_spec.SetField(testfile.FieldName, field.TypeString, value)
	}
	if tfuo.mutation.NameCleared() {
		_spec.ClearField(testfile.FieldName, field.TypeString)
	}
	if value, ok := tfuo.mutation.Prefix(); ok {
		_spec.SetField(testfile.FieldPrefix, field.TypeJSON, value)
	}
	if value, ok := tfuo.mutation.AppendedPrefix(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, testfile.FieldPrefix, value)
		})
	}
	if tfuo.mutation.PrefixCleared() {
		_spec.ClearField(testfile.FieldPrefix, field.TypeJSON)
	}
	if tfuo.mutation.TestResultCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testfile.TestResultTable,
			Columns: []string{testfile.TestResultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tfuo.mutation.TestResultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   testfile.TestResultTable,
			Columns: []string{testfile.TestResultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testresultbes.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &TestFile{config: tfuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tfuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{testfile.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tfuo.mutation.done = true
	return _node, nil
}
