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

// GarbageMetricsUpdate is the builder for updating GarbageMetrics entities.
type GarbageMetricsUpdate struct {
	config
	hooks    []Hook
	mutation *GarbageMetricsMutation
}

// Where appends a list predicates to the GarbageMetricsUpdate builder.
func (gmu *GarbageMetricsUpdate) Where(ps ...predicate.GarbageMetrics) *GarbageMetricsUpdate {
	gmu.mutation.Where(ps...)
	return gmu
}

// SetType sets the "type" field.
func (gmu *GarbageMetricsUpdate) SetType(s string) *GarbageMetricsUpdate {
	gmu.mutation.SetType(s)
	return gmu
}

// SetNillableType sets the "type" field if the given value is not nil.
func (gmu *GarbageMetricsUpdate) SetNillableType(s *string) *GarbageMetricsUpdate {
	if s != nil {
		gmu.SetType(*s)
	}
	return gmu
}

// ClearType clears the value of the "type" field.
func (gmu *GarbageMetricsUpdate) ClearType() *GarbageMetricsUpdate {
	gmu.mutation.ClearType()
	return gmu
}

// SetGarbageCollected sets the "garbage_collected" field.
func (gmu *GarbageMetricsUpdate) SetGarbageCollected(i int64) *GarbageMetricsUpdate {
	gmu.mutation.ResetGarbageCollected()
	gmu.mutation.SetGarbageCollected(i)
	return gmu
}

// SetNillableGarbageCollected sets the "garbage_collected" field if the given value is not nil.
func (gmu *GarbageMetricsUpdate) SetNillableGarbageCollected(i *int64) *GarbageMetricsUpdate {
	if i != nil {
		gmu.SetGarbageCollected(*i)
	}
	return gmu
}

// AddGarbageCollected adds i to the "garbage_collected" field.
func (gmu *GarbageMetricsUpdate) AddGarbageCollected(i int64) *GarbageMetricsUpdate {
	gmu.mutation.AddGarbageCollected(i)
	return gmu
}

// ClearGarbageCollected clears the value of the "garbage_collected" field.
func (gmu *GarbageMetricsUpdate) ClearGarbageCollected() *GarbageMetricsUpdate {
	gmu.mutation.ClearGarbageCollected()
	return gmu
}

// SetMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID.
func (gmu *GarbageMetricsUpdate) SetMemoryMetricsID(id int) *GarbageMetricsUpdate {
	gmu.mutation.SetMemoryMetricsID(id)
	return gmu
}

// SetNillableMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID if the given value is not nil.
func (gmu *GarbageMetricsUpdate) SetNillableMemoryMetricsID(id *int) *GarbageMetricsUpdate {
	if id != nil {
		gmu = gmu.SetMemoryMetricsID(*id)
	}
	return gmu
}

// SetMemoryMetrics sets the "memory_metrics" edge to the MemoryMetrics entity.
func (gmu *GarbageMetricsUpdate) SetMemoryMetrics(m *MemoryMetrics) *GarbageMetricsUpdate {
	return gmu.SetMemoryMetricsID(m.ID)
}

// Mutation returns the GarbageMetricsMutation object of the builder.
func (gmu *GarbageMetricsUpdate) Mutation() *GarbageMetricsMutation {
	return gmu.mutation
}

// ClearMemoryMetrics clears the "memory_metrics" edge to the MemoryMetrics entity.
func (gmu *GarbageMetricsUpdate) ClearMemoryMetrics() *GarbageMetricsUpdate {
	gmu.mutation.ClearMemoryMetrics()
	return gmu
}

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

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

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

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

func (gmu *GarbageMetricsUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(garbagemetrics.Table, garbagemetrics.Columns, sqlgraph.NewFieldSpec(garbagemetrics.FieldID, field.TypeInt))
	if ps := gmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmu.mutation.GetType(); ok {
		_spec.SetField(garbagemetrics.FieldType, field.TypeString, value)
	}
	if gmu.mutation.TypeCleared() {
		_spec.ClearField(garbagemetrics.FieldType, field.TypeString)
	}
	if value, ok := gmu.mutation.GarbageCollected(); ok {
		_spec.SetField(garbagemetrics.FieldGarbageCollected, field.TypeInt64, value)
	}
	if value, ok := gmu.mutation.AddedGarbageCollected(); ok {
		_spec.AddField(garbagemetrics.FieldGarbageCollected, field.TypeInt64, value)
	}
	if gmu.mutation.GarbageCollectedCleared() {
		_spec.ClearField(garbagemetrics.FieldGarbageCollected, field.TypeInt64)
	}
	if gmu.mutation.MemoryMetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   garbagemetrics.MemoryMetricsTable,
			Columns: []string{garbagemetrics.MemoryMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(memorymetrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmu.mutation.MemoryMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   garbagemetrics.MemoryMetricsTable,
			Columns: []string{garbagemetrics.MemoryMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(memorymetrics.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, gmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{garbagemetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	gmu.mutation.done = true
	return n, nil
}

// GarbageMetricsUpdateOne is the builder for updating a single GarbageMetrics entity.
type GarbageMetricsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GarbageMetricsMutation
}

// SetType sets the "type" field.
func (gmuo *GarbageMetricsUpdateOne) SetType(s string) *GarbageMetricsUpdateOne {
	gmuo.mutation.SetType(s)
	return gmuo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (gmuo *GarbageMetricsUpdateOne) SetNillableType(s *string) *GarbageMetricsUpdateOne {
	if s != nil {
		gmuo.SetType(*s)
	}
	return gmuo
}

// ClearType clears the value of the "type" field.
func (gmuo *GarbageMetricsUpdateOne) ClearType() *GarbageMetricsUpdateOne {
	gmuo.mutation.ClearType()
	return gmuo
}

// SetGarbageCollected sets the "garbage_collected" field.
func (gmuo *GarbageMetricsUpdateOne) SetGarbageCollected(i int64) *GarbageMetricsUpdateOne {
	gmuo.mutation.ResetGarbageCollected()
	gmuo.mutation.SetGarbageCollected(i)
	return gmuo
}

// SetNillableGarbageCollected sets the "garbage_collected" field if the given value is not nil.
func (gmuo *GarbageMetricsUpdateOne) SetNillableGarbageCollected(i *int64) *GarbageMetricsUpdateOne {
	if i != nil {
		gmuo.SetGarbageCollected(*i)
	}
	return gmuo
}

// AddGarbageCollected adds i to the "garbage_collected" field.
func (gmuo *GarbageMetricsUpdateOne) AddGarbageCollected(i int64) *GarbageMetricsUpdateOne {
	gmuo.mutation.AddGarbageCollected(i)
	return gmuo
}

// ClearGarbageCollected clears the value of the "garbage_collected" field.
func (gmuo *GarbageMetricsUpdateOne) ClearGarbageCollected() *GarbageMetricsUpdateOne {
	gmuo.mutation.ClearGarbageCollected()
	return gmuo
}

// SetMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID.
func (gmuo *GarbageMetricsUpdateOne) SetMemoryMetricsID(id int) *GarbageMetricsUpdateOne {
	gmuo.mutation.SetMemoryMetricsID(id)
	return gmuo
}

// SetNillableMemoryMetricsID sets the "memory_metrics" edge to the MemoryMetrics entity by ID if the given value is not nil.
func (gmuo *GarbageMetricsUpdateOne) SetNillableMemoryMetricsID(id *int) *GarbageMetricsUpdateOne {
	if id != nil {
		gmuo = gmuo.SetMemoryMetricsID(*id)
	}
	return gmuo
}

// SetMemoryMetrics sets the "memory_metrics" edge to the MemoryMetrics entity.
func (gmuo *GarbageMetricsUpdateOne) SetMemoryMetrics(m *MemoryMetrics) *GarbageMetricsUpdateOne {
	return gmuo.SetMemoryMetricsID(m.ID)
}

// Mutation returns the GarbageMetricsMutation object of the builder.
func (gmuo *GarbageMetricsUpdateOne) Mutation() *GarbageMetricsMutation {
	return gmuo.mutation
}

// ClearMemoryMetrics clears the "memory_metrics" edge to the MemoryMetrics entity.
func (gmuo *GarbageMetricsUpdateOne) ClearMemoryMetrics() *GarbageMetricsUpdateOne {
	gmuo.mutation.ClearMemoryMetrics()
	return gmuo
}

// Where appends a list predicates to the GarbageMetricsUpdate builder.
func (gmuo *GarbageMetricsUpdateOne) Where(ps ...predicate.GarbageMetrics) *GarbageMetricsUpdateOne {
	gmuo.mutation.Where(ps...)
	return gmuo
}

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

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

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

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

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

func (gmuo *GarbageMetricsUpdateOne) sqlSave(ctx context.Context) (_node *GarbageMetrics, err error) {
	_spec := sqlgraph.NewUpdateSpec(garbagemetrics.Table, garbagemetrics.Columns, sqlgraph.NewFieldSpec(garbagemetrics.FieldID, field.TypeInt))
	id, ok := gmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "GarbageMetrics.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := gmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, garbagemetrics.FieldID)
		for _, f := range fields {
			if !garbagemetrics.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != garbagemetrics.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := gmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmuo.mutation.GetType(); ok {
		_spec.SetField(garbagemetrics.FieldType, field.TypeString, value)
	}
	if gmuo.mutation.TypeCleared() {
		_spec.ClearField(garbagemetrics.FieldType, field.TypeString)
	}
	if value, ok := gmuo.mutation.GarbageCollected(); ok {
		_spec.SetField(garbagemetrics.FieldGarbageCollected, field.TypeInt64, value)
	}
	if value, ok := gmuo.mutation.AddedGarbageCollected(); ok {
		_spec.AddField(garbagemetrics.FieldGarbageCollected, field.TypeInt64, value)
	}
	if gmuo.mutation.GarbageCollectedCleared() {
		_spec.ClearField(garbagemetrics.FieldGarbageCollected, field.TypeInt64)
	}
	if gmuo.mutation.MemoryMetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   garbagemetrics.MemoryMetricsTable,
			Columns: []string{garbagemetrics.MemoryMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(memorymetrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmuo.mutation.MemoryMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   garbagemetrics.MemoryMetricsTable,
			Columns: []string{garbagemetrics.MemoryMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(memorymetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &GarbageMetrics{config: gmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, gmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{garbagemetrics.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	gmuo.mutation.done = true
	return _node, nil
}
