package ent

import (
	"context"
	"fmt"
	"time"

	"github.com/facebookincubator/ent/dialect/sql"
	"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
	"github.com/facebookincubator/ent/schema/field"
	"github.com/paulvollmer/robotstxt-datastore/server/ent/predicate"
	"github.com/paulvollmer/robotstxt-datastore/server/ent/robotstxt"
)

type RobotstxtUpdate struct {
	config
	hooks      []Hook
	mutation   *RobotstxtMutation
	predicates []predicate.Robotstxt
}

func (ru *RobotstxtUpdate) Where(ps ...predicate.Robotstxt) *RobotstxtUpdate {
	ru.predicates = append(ru.predicates, ps...)
	return ru
}

func (ru *RobotstxtUpdate) SetUpdatedAt(t time.Time) *RobotstxtUpdate {
	ru.mutation.SetUpdatedAt(t)
	return ru
}

func (ru *RobotstxtUpdate) SetHost(s string) *RobotstxtUpdate {
	ru.mutation.SetHost(s)
	return ru
}

func (ru *RobotstxtUpdate) SetScheme(s string) *RobotstxtUpdate {
	ru.mutation.SetScheme(s)
	return ru
}

func (ru *RobotstxtUpdate) SetResponseURL(s string) *RobotstxtUpdate {
	ru.mutation.SetResponseURL(s)
	return ru
}

func (ru *RobotstxtUpdate) SetBody(b []byte) *RobotstxtUpdate {
	ru.mutation.SetBody(b)
	return ru
}

func (ru *RobotstxtUpdate) SetStatuscode(i int32) *RobotstxtUpdate {
	ru.mutation.ResetStatuscode()
	ru.mutation.SetStatuscode(i)
	return ru
}

func (ru *RobotstxtUpdate) AddStatuscode(i int32) *RobotstxtUpdate {
	ru.mutation.AddStatuscode(i)
	return ru
}

func (ru *RobotstxtUpdate) SetResponseTime(i int64) *RobotstxtUpdate {
	ru.mutation.ResetResponseTime()
	ru.mutation.SetResponseTime(i)
	return ru
}

func (ru *RobotstxtUpdate) AddResponseTime(i int64) *RobotstxtUpdate {
	ru.mutation.AddResponseTime(i)
	return ru
}

func (ru *RobotstxtUpdate) Save(ctx context.Context) (int, error) {
	if _, ok := ru.mutation.UpdatedAt(); !ok {
		v := robotstxt.UpdateDefaultUpdatedAt()
		ru.mutation.SetUpdatedAt(v)
	}
	var (
		err      error
		affected int
	)
	if len(ru.hooks) == 0 {
		affected, err = ru.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*RobotstxtMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			ru.mutation = mutation
			affected, err = ru.sqlSave(ctx)
			return affected, err
		})
		for i := len(ru.hooks) - 1; i >= 0; i-- {
			mut = ru.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ru.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

func (ru *RobotstxtUpdate) SaveX(ctx context.Context) int {
	affected, err := ru.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

func (ru *RobotstxtUpdate) Exec(ctx context.Context) error {
	_, err := ru.Save(ctx)
	return err
}

func (ru *RobotstxtUpdate) ExecX(ctx context.Context) {
	if err := ru.Exec(ctx); err != nil {
		panic(err)
	}
}

func (ru *RobotstxtUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   robotstxt.Table,
			Columns: robotstxt.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: robotstxt.FieldID,
			},
		},
	}
	if ps := ru.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ru.mutation.UpdatedAt(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: robotstxt.FieldUpdatedAt,
		})
	}
	if value, ok := ru.mutation.Host(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldHost,
		})
	}
	if value, ok := ru.mutation.Scheme(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldScheme,
		})
	}
	if value, ok := ru.mutation.ResponseURL(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldResponseURL,
		})
	}
	if value, ok := ru.mutation.Body(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBytes,
			Value:  value,
			Column: robotstxt.FieldBody,
		})
	}
	if value, ok := ru.mutation.Statuscode(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ru.mutation.AddedStatuscode(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ru.mutation.ResponseTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if value, ok := ru.mutation.AddedResponseTime(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotstxt.Label}
		} else if cerr, ok := isSQLConstraintError(err); ok {
			err = cerr
		}
		return 0, err
	}
	return n, nil
}

type RobotstxtUpdateOne struct {
	config
	hooks    []Hook
	mutation *RobotstxtMutation
}

func (ruo *RobotstxtUpdateOne) SetUpdatedAt(t time.Time) *RobotstxtUpdateOne {
	ruo.mutation.SetUpdatedAt(t)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetHost(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetHost(s)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetScheme(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetScheme(s)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetResponseURL(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetResponseURL(s)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetBody(b []byte) *RobotstxtUpdateOne {
	ruo.mutation.SetBody(b)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetStatuscode(i int32) *RobotstxtUpdateOne {
	ruo.mutation.ResetStatuscode()
	ruo.mutation.SetStatuscode(i)
	return ruo
}

func (ruo *RobotstxtUpdateOne) AddStatuscode(i int32) *RobotstxtUpdateOne {
	ruo.mutation.AddStatuscode(i)
	return ruo
}

func (ruo *RobotstxtUpdateOne) SetResponseTime(i int64) *RobotstxtUpdateOne {
	ruo.mutation.ResetResponseTime()
	ruo.mutation.SetResponseTime(i)
	return ruo
}

func (ruo *RobotstxtUpdateOne) AddResponseTime(i int64) *RobotstxtUpdateOne {
	ruo.mutation.AddResponseTime(i)
	return ruo
}

func (ruo *RobotstxtUpdateOne) Save(ctx context.Context) (*Robotstxt, error) {
	if _, ok := ruo.mutation.UpdatedAt(); !ok {
		v := robotstxt.UpdateDefaultUpdatedAt()
		ruo.mutation.SetUpdatedAt(v)
	}
	var (
		err  error
		node *Robotstxt
	)
	if len(ruo.hooks) == 0 {
		node, err = ruo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*RobotstxtMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			ruo.mutation = mutation
			node, err = ruo.sqlSave(ctx)
			return node, err
		})
		for i := len(ruo.hooks) - 1; i >= 0; i-- {
			mut = ruo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ruo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

func (ruo *RobotstxtUpdateOne) SaveX(ctx context.Context) *Robotstxt {
	r, err := ruo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return r
}

func (ruo *RobotstxtUpdateOne) Exec(ctx context.Context) error {
	_, err := ruo.Save(ctx)
	return err
}

func (ruo *RobotstxtUpdateOne) ExecX(ctx context.Context) {
	if err := ruo.Exec(ctx); err != nil {
		panic(err)
	}
}

func (ruo *RobotstxtUpdateOne) sqlSave(ctx context.Context) (r *Robotstxt, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   robotstxt.Table,
			Columns: robotstxt.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: robotstxt.FieldID,
			},
		},
	}
	id, ok := ruo.mutation.ID()
	if !ok {
		return nil, fmt.Errorf("missing Robotstxt.ID for update")
	}
	_spec.Node.ID.Value = id
	if value, ok := ruo.mutation.UpdatedAt(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: robotstxt.FieldUpdatedAt,
		})
	}
	if value, ok := ruo.mutation.Host(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldHost,
		})
	}
	if value, ok := ruo.mutation.Scheme(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldScheme,
		})
	}
	if value, ok := ruo.mutation.ResponseURL(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldResponseURL,
		})
	}
	if value, ok := ruo.mutation.Body(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBytes,
			Value:  value,
			Column: robotstxt.FieldBody,
		})
	}
	if value, ok := ruo.mutation.Statuscode(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ruo.mutation.AddedStatuscode(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ruo.mutation.ResponseTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if value, ok := ruo.mutation.AddedResponseTime(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	r = &Robotstxt{config: ruo.config}
	_spec.Assign = r.assignValues
	_spec.ScanValues = r.scanValues()
	if err = sqlgraph.UpdateNode(ctx, ruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotstxt.Label}
		} else if cerr, ok := isSQLConstraintError(err); ok {
			err = cerr
		}
		return nil, err
	}
	return r, nil
}
// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"
	"time"

	"github.com/facebookincubator/ent/dialect/sql"
	"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
	"github.com/facebookincubator/ent/schema/field"
	"github.com/paulvollmer/robotstxt-datastore/server/ent/predicate"
	"github.com/paulvollmer/robotstxt-datastore/server/ent/robotstxt"
)

// RobotstxtUpdate is the builder for updating Robotstxt entities.
type RobotstxtUpdate struct {
	config
	hooks      []Hook
	mutation   *RobotstxtMutation
	predicates []predicate.Robotstxt
}

// Where adds a new predicate for the builder.
func (ru *RobotstxtUpdate) Where(ps ...predicate.Robotstxt) *RobotstxtUpdate {
	ru.predicates = append(ru.predicates, ps...)
	return ru
}

// SetUpdatedAt sets the updated_at field.
func (ru *RobotstxtUpdate) SetUpdatedAt(t time.Time) *RobotstxtUpdate {
	ru.mutation.SetUpdatedAt(t)
	return ru
}

// SetHost sets the host field.
func (ru *RobotstxtUpdate) SetHost(s string) *RobotstxtUpdate {
	ru.mutation.SetHost(s)
	return ru
}

// SetScheme sets the scheme field.
func (ru *RobotstxtUpdate) SetScheme(s string) *RobotstxtUpdate {
	ru.mutation.SetScheme(s)
	return ru
}

// SetResponseURL sets the response_url field.
func (ru *RobotstxtUpdate) SetResponseURL(s string) *RobotstxtUpdate {
	ru.mutation.SetResponseURL(s)
	return ru
}

// SetBody sets the body field.
func (ru *RobotstxtUpdate) SetBody(b []byte) *RobotstxtUpdate {
	ru.mutation.SetBody(b)
	return ru
}

// SetStatuscode sets the statuscode field.
func (ru *RobotstxtUpdate) SetStatuscode(i int32) *RobotstxtUpdate {
	ru.mutation.ResetStatuscode()
	ru.mutation.SetStatuscode(i)
	return ru
}

// AddStatuscode adds i to statuscode.
func (ru *RobotstxtUpdate) AddStatuscode(i int32) *RobotstxtUpdate {
	ru.mutation.AddStatuscode(i)
	return ru
}

// SetResponseTime sets the response_time field.
func (ru *RobotstxtUpdate) SetResponseTime(i int64) *RobotstxtUpdate {
	ru.mutation.ResetResponseTime()
	ru.mutation.SetResponseTime(i)
	return ru
}

// AddResponseTime adds i to response_time.
func (ru *RobotstxtUpdate) AddResponseTime(i int64) *RobotstxtUpdate {
	ru.mutation.AddResponseTime(i)
	return ru
}

// Save executes the query and returns the number of rows/vertices matched by this operation.
func (ru *RobotstxtUpdate) Save(ctx context.Context) (int, error) {
	if _, ok := ru.mutation.UpdatedAt(); !ok {
		v := robotstxt.UpdateDefaultUpdatedAt()
		ru.mutation.SetUpdatedAt(v)
	}
	var (
		err      error
		affected int
	)
	if len(ru.hooks) == 0 {
		affected, err = ru.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*RobotstxtMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			ru.mutation = mutation
			affected, err = ru.sqlSave(ctx)
			return affected, err
		})
		for i := len(ru.hooks) - 1; i >= 0; i-- {
			mut = ru.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ru.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

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

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

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

func (ru *RobotstxtUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   robotstxt.Table,
			Columns: robotstxt.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: robotstxt.FieldID,
			},
		},
	}
	if ps := ru.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ru.mutation.UpdatedAt(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: robotstxt.FieldUpdatedAt,
		})
	}
	if value, ok := ru.mutation.Host(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldHost,
		})
	}
	if value, ok := ru.mutation.Scheme(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldScheme,
		})
	}
	if value, ok := ru.mutation.ResponseURL(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldResponseURL,
		})
	}
	if value, ok := ru.mutation.Body(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBytes,
			Value:  value,
			Column: robotstxt.FieldBody,
		})
	}
	if value, ok := ru.mutation.Statuscode(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ru.mutation.AddedStatuscode(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ru.mutation.ResponseTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if value, ok := ru.mutation.AddedResponseTime(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotstxt.Label}
		} else if cerr, ok := isSQLConstraintError(err); ok {
			err = cerr
		}
		return 0, err
	}
	return n, nil
}

// RobotstxtUpdateOne is the builder for updating a single Robotstxt entity.
type RobotstxtUpdateOne struct {
	config
	hooks    []Hook
	mutation *RobotstxtMutation
}

// SetUpdatedAt sets the updated_at field.
func (ruo *RobotstxtUpdateOne) SetUpdatedAt(t time.Time) *RobotstxtUpdateOne {
	ruo.mutation.SetUpdatedAt(t)
	return ruo
}

// SetHost sets the host field.
func (ruo *RobotstxtUpdateOne) SetHost(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetHost(s)
	return ruo
}

// SetScheme sets the scheme field.
func (ruo *RobotstxtUpdateOne) SetScheme(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetScheme(s)
	return ruo
}

// SetResponseURL sets the response_url field.
func (ruo *RobotstxtUpdateOne) SetResponseURL(s string) *RobotstxtUpdateOne {
	ruo.mutation.SetResponseURL(s)
	return ruo
}

// SetBody sets the body field.
func (ruo *RobotstxtUpdateOne) SetBody(b []byte) *RobotstxtUpdateOne {
	ruo.mutation.SetBody(b)
	return ruo
}

// SetStatuscode sets the statuscode field.
func (ruo *RobotstxtUpdateOne) SetStatuscode(i int32) *RobotstxtUpdateOne {
	ruo.mutation.ResetStatuscode()
	ruo.mutation.SetStatuscode(i)
	return ruo
}

// AddStatuscode adds i to statuscode.
func (ruo *RobotstxtUpdateOne) AddStatuscode(i int32) *RobotstxtUpdateOne {
	ruo.mutation.AddStatuscode(i)
	return ruo
}

// SetResponseTime sets the response_time field.
func (ruo *RobotstxtUpdateOne) SetResponseTime(i int64) *RobotstxtUpdateOne {
	ruo.mutation.ResetResponseTime()
	ruo.mutation.SetResponseTime(i)
	return ruo
}

// AddResponseTime adds i to response_time.
func (ruo *RobotstxtUpdateOne) AddResponseTime(i int64) *RobotstxtUpdateOne {
	ruo.mutation.AddResponseTime(i)
	return ruo
}

// Save executes the query and returns the updated entity.
func (ruo *RobotstxtUpdateOne) Save(ctx context.Context) (*Robotstxt, error) {
	if _, ok := ruo.mutation.UpdatedAt(); !ok {
		v := robotstxt.UpdateDefaultUpdatedAt()
		ruo.mutation.SetUpdatedAt(v)
	}
	var (
		err  error
		node *Robotstxt
	)
	if len(ruo.hooks) == 0 {
		node, err = ruo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*RobotstxtMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			ruo.mutation = mutation
			node, err = ruo.sqlSave(ctx)
			return node, err
		})
		for i := len(ruo.hooks) - 1; i >= 0; i-- {
			mut = ruo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ruo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

// SaveX is like Save, but panics if an error occurs.
func (ruo *RobotstxtUpdateOne) SaveX(ctx context.Context) *Robotstxt {
	r, err := ruo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return r
}

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

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

func (ruo *RobotstxtUpdateOne) sqlSave(ctx context.Context) (r *Robotstxt, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   robotstxt.Table,
			Columns: robotstxt.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: robotstxt.FieldID,
			},
		},
	}
	id, ok := ruo.mutation.ID()
	if !ok {
		return nil, fmt.Errorf("missing Robotstxt.ID for update")
	}
	_spec.Node.ID.Value = id
	if value, ok := ruo.mutation.UpdatedAt(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: robotstxt.FieldUpdatedAt,
		})
	}
	if value, ok := ruo.mutation.Host(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldHost,
		})
	}
	if value, ok := ruo.mutation.Scheme(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldScheme,
		})
	}
	if value, ok := ruo.mutation.ResponseURL(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: robotstxt.FieldResponseURL,
		})
	}
	if value, ok := ruo.mutation.Body(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBytes,
			Value:  value,
			Column: robotstxt.FieldBody,
		})
	}
	if value, ok := ruo.mutation.Statuscode(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ruo.mutation.AddedStatuscode(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt32,
			Value:  value,
			Column: robotstxt.FieldStatuscode,
		})
	}
	if value, ok := ruo.mutation.ResponseTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	if value, ok := ruo.mutation.AddedResponseTime(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: robotstxt.FieldResponseTime,
		})
	}
	r = &Robotstxt{config: ruo.config}
	_spec.Assign = r.assignValues
	_spec.ScanValues = r.scanValues()
	if err = sqlgraph.UpdateNode(ctx, ruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotstxt.Label}
		} else if cerr, ok := isSQLConstraintError(err); ok {
			err = cerr
		}
		return nil, err
	}
	return r, nil
}
