// 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/TSELab/astra/internal/store/ent/edge"
	"github.com/TSELab/astra/internal/store/ent/predicate"
)

// EdgeUpdate is the builder for updating Edge entities.
type EdgeUpdate struct {
	config
	hooks    []Hook
	mutation *EdgeMutation
}

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

// SetSource sets the "source" field.
func (_u *EdgeUpdate) SetSource(v string) *EdgeUpdate {
	_u.mutation.SetSource(v)
	return _u
}

// SetNillableSource sets the "source" field if the given value is not nil.
func (_u *EdgeUpdate) SetNillableSource(v *string) *EdgeUpdate {
	if v != nil {
		_u.SetSource(*v)
	}
	return _u
}

// SetTarget sets the "target" field.
func (_u *EdgeUpdate) SetTarget(v string) *EdgeUpdate {
	_u.mutation.SetTarget(v)
	return _u
}

// SetNillableTarget sets the "target" field if the given value is not nil.
func (_u *EdgeUpdate) SetNillableTarget(v *string) *EdgeUpdate {
	if v != nil {
		_u.SetTarget(*v)
	}
	return _u
}

// SetRelation sets the "relation" field.
func (_u *EdgeUpdate) SetRelation(v string) *EdgeUpdate {
	_u.mutation.SetRelation(v)
	return _u
}

// SetNillableRelation sets the "relation" field if the given value is not nil.
func (_u *EdgeUpdate) SetNillableRelation(v *string) *EdgeUpdate {
	if v != nil {
		_u.SetRelation(*v)
	}
	return _u
}

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *EdgeUpdate) 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 *EdgeUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

func (_u *EdgeUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(edge.Table, edge.Columns, sqlgraph.NewFieldSpec(edge.FieldID, field.TypeInt))
	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.Source(); ok {
		_spec.SetField(edge.FieldSource, field.TypeString, value)
	}
	if value, ok := _u.mutation.Target(); ok {
		_spec.SetField(edge.FieldTarget, field.TypeString, value)
	}
	if value, ok := _u.mutation.Relation(); ok {
		_spec.SetField(edge.FieldRelation, field.TypeString, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{edge.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// EdgeUpdateOne is the builder for updating a single Edge entity.
type EdgeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *EdgeMutation
}

// SetSource sets the "source" field.
func (_u *EdgeUpdateOne) SetSource(v string) *EdgeUpdateOne {
	_u.mutation.SetSource(v)
	return _u
}

// SetNillableSource sets the "source" field if the given value is not nil.
func (_u *EdgeUpdateOne) SetNillableSource(v *string) *EdgeUpdateOne {
	if v != nil {
		_u.SetSource(*v)
	}
	return _u
}

// SetTarget sets the "target" field.
func (_u *EdgeUpdateOne) SetTarget(v string) *EdgeUpdateOne {
	_u.mutation.SetTarget(v)
	return _u
}

// SetNillableTarget sets the "target" field if the given value is not nil.
func (_u *EdgeUpdateOne) SetNillableTarget(v *string) *EdgeUpdateOne {
	if v != nil {
		_u.SetTarget(*v)
	}
	return _u
}

// SetRelation sets the "relation" field.
func (_u *EdgeUpdateOne) SetRelation(v string) *EdgeUpdateOne {
	_u.mutation.SetRelation(v)
	return _u
}

// SetNillableRelation sets the "relation" field if the given value is not nil.
func (_u *EdgeUpdateOne) SetNillableRelation(v *string) *EdgeUpdateOne {
	if v != nil {
		_u.SetRelation(*v)
	}
	return _u
}

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

// Where appends a list predicates to the EdgeUpdate builder.
func (_u *EdgeUpdateOne) Where(ps ...predicate.Edge) *EdgeUpdateOne {
	_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 *EdgeUpdateOne) Select(field string, fields ...string) *EdgeUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

func (_u *EdgeUpdateOne) sqlSave(ctx context.Context) (_node *Edge, err error) {
	_spec := sqlgraph.NewUpdateSpec(edge.Table, edge.Columns, sqlgraph.NewFieldSpec(edge.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Edge.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, edge.FieldID)
		for _, f := range fields {
			if !edge.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != edge.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.Source(); ok {
		_spec.SetField(edge.FieldSource, field.TypeString, value)
	}
	if value, ok := _u.mutation.Target(); ok {
		_spec.SetField(edge.FieldTarget, field.TypeString, value)
	}
	if value, ok := _u.mutation.Relation(); ok {
		_spec.SetField(edge.FieldRelation, field.TypeString, value)
	}
	_node = &Edge{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{edge.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
