// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/common/keys"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/signingkeyshare"
	"github.com/lightsparkdev/spark/so/ent/tree"
	"github.com/lightsparkdev/spark/so/ent/treenode"
)

// TreeNodeUpdate is the builder for updating TreeNode entities.
type TreeNodeUpdate struct {
	config
	hooks    []Hook
	mutation *TreeNodeMutation
}

// Where appends a list predicates to the TreeNodeUpdate builder.
func (tnu *TreeNodeUpdate) Where(ps ...predicate.TreeNode) *TreeNodeUpdate {
	tnu.mutation.Where(ps...)
	return tnu
}

// SetUpdateTime sets the "update_time" field.
func (tnu *TreeNodeUpdate) SetUpdateTime(t time.Time) *TreeNodeUpdate {
	tnu.mutation.SetUpdateTime(t)
	return tnu
}

// SetStatus sets the "status" field.
func (tnu *TreeNodeUpdate) SetStatus(sns schematype.TreeNodeStatus) *TreeNodeUpdate {
	tnu.mutation.SetStatus(sns)
	return tnu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableStatus(sns *schematype.TreeNodeStatus) *TreeNodeUpdate {
	if sns != nil {
		tnu.SetStatus(*sns)
	}
	return tnu
}

// SetOwnerIdentityPubkey sets the "owner_identity_pubkey" field.
func (tnu *TreeNodeUpdate) SetOwnerIdentityPubkey(k keys.Public) *TreeNodeUpdate {
	tnu.mutation.SetOwnerIdentityPubkey(k)
	return tnu
}

// SetNillableOwnerIdentityPubkey sets the "owner_identity_pubkey" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableOwnerIdentityPubkey(k *keys.Public) *TreeNodeUpdate {
	if k != nil {
		tnu.SetOwnerIdentityPubkey(*k)
	}
	return tnu
}

// SetOwnerSigningPubkey sets the "owner_signing_pubkey" field.
func (tnu *TreeNodeUpdate) SetOwnerSigningPubkey(k keys.Public) *TreeNodeUpdate {
	tnu.mutation.SetOwnerSigningPubkey(k)
	return tnu
}

// SetNillableOwnerSigningPubkey sets the "owner_signing_pubkey" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableOwnerSigningPubkey(k *keys.Public) *TreeNodeUpdate {
	if k != nil {
		tnu.SetOwnerSigningPubkey(*k)
	}
	return tnu
}

// SetVout sets the "vout" field.
func (tnu *TreeNodeUpdate) SetVout(i int16) *TreeNodeUpdate {
	tnu.mutation.ResetVout()
	tnu.mutation.SetVout(i)
	return tnu
}

// SetNillableVout sets the "vout" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableVout(i *int16) *TreeNodeUpdate {
	if i != nil {
		tnu.SetVout(*i)
	}
	return tnu
}

// AddVout adds i to the "vout" field.
func (tnu *TreeNodeUpdate) AddVout(i int16) *TreeNodeUpdate {
	tnu.mutation.AddVout(i)
	return tnu
}

// SetNodeConfirmationHeight sets the "node_confirmation_height" field.
func (tnu *TreeNodeUpdate) SetNodeConfirmationHeight(u uint64) *TreeNodeUpdate {
	tnu.mutation.ResetNodeConfirmationHeight()
	tnu.mutation.SetNodeConfirmationHeight(u)
	return tnu
}

// SetNillableNodeConfirmationHeight sets the "node_confirmation_height" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableNodeConfirmationHeight(u *uint64) *TreeNodeUpdate {
	if u != nil {
		tnu.SetNodeConfirmationHeight(*u)
	}
	return tnu
}

// AddNodeConfirmationHeight adds u to the "node_confirmation_height" field.
func (tnu *TreeNodeUpdate) AddNodeConfirmationHeight(u int64) *TreeNodeUpdate {
	tnu.mutation.AddNodeConfirmationHeight(u)
	return tnu
}

// ClearNodeConfirmationHeight clears the value of the "node_confirmation_height" field.
func (tnu *TreeNodeUpdate) ClearNodeConfirmationHeight() *TreeNodeUpdate {
	tnu.mutation.ClearNodeConfirmationHeight()
	return tnu
}

// SetRefundConfirmationHeight sets the "refund_confirmation_height" field.
func (tnu *TreeNodeUpdate) SetRefundConfirmationHeight(u uint64) *TreeNodeUpdate {
	tnu.mutation.ResetRefundConfirmationHeight()
	tnu.mutation.SetRefundConfirmationHeight(u)
	return tnu
}

// SetNillableRefundConfirmationHeight sets the "refund_confirmation_height" field if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableRefundConfirmationHeight(u *uint64) *TreeNodeUpdate {
	if u != nil {
		tnu.SetRefundConfirmationHeight(*u)
	}
	return tnu
}

// AddRefundConfirmationHeight adds u to the "refund_confirmation_height" field.
func (tnu *TreeNodeUpdate) AddRefundConfirmationHeight(u int64) *TreeNodeUpdate {
	tnu.mutation.AddRefundConfirmationHeight(u)
	return tnu
}

// ClearRefundConfirmationHeight clears the value of the "refund_confirmation_height" field.
func (tnu *TreeNodeUpdate) ClearRefundConfirmationHeight() *TreeNodeUpdate {
	tnu.mutation.ClearRefundConfirmationHeight()
	return tnu
}

// SetRawTx sets the "raw_tx" field.
func (tnu *TreeNodeUpdate) SetRawTx(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetRawTx(b)
	return tnu
}

// SetDirectTx sets the "direct_tx" field.
func (tnu *TreeNodeUpdate) SetDirectTx(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectTx(b)
	return tnu
}

// ClearDirectTx clears the value of the "direct_tx" field.
func (tnu *TreeNodeUpdate) ClearDirectTx() *TreeNodeUpdate {
	tnu.mutation.ClearDirectTx()
	return tnu
}

// SetDirectFromCpfpRefundTx sets the "direct_from_cpfp_refund_tx" field.
func (tnu *TreeNodeUpdate) SetDirectFromCpfpRefundTx(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectFromCpfpRefundTx(b)
	return tnu
}

// ClearDirectFromCpfpRefundTx clears the value of the "direct_from_cpfp_refund_tx" field.
func (tnu *TreeNodeUpdate) ClearDirectFromCpfpRefundTx() *TreeNodeUpdate {
	tnu.mutation.ClearDirectFromCpfpRefundTx()
	return tnu
}

// SetRawTxid sets the "raw_txid" field.
func (tnu *TreeNodeUpdate) SetRawTxid(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetRawTxid(b)
	return tnu
}

// ClearRawTxid clears the value of the "raw_txid" field.
func (tnu *TreeNodeUpdate) ClearRawTxid() *TreeNodeUpdate {
	tnu.mutation.ClearRawTxid()
	return tnu
}

// SetDirectTxid sets the "direct_txid" field.
func (tnu *TreeNodeUpdate) SetDirectTxid(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectTxid(b)
	return tnu
}

// ClearDirectTxid clears the value of the "direct_txid" field.
func (tnu *TreeNodeUpdate) ClearDirectTxid() *TreeNodeUpdate {
	tnu.mutation.ClearDirectTxid()
	return tnu
}

// SetDirectFromCpfpRefundTxid sets the "direct_from_cpfp_refund_txid" field.
func (tnu *TreeNodeUpdate) SetDirectFromCpfpRefundTxid(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectFromCpfpRefundTxid(b)
	return tnu
}

// ClearDirectFromCpfpRefundTxid clears the value of the "direct_from_cpfp_refund_txid" field.
func (tnu *TreeNodeUpdate) ClearDirectFromCpfpRefundTxid() *TreeNodeUpdate {
	tnu.mutation.ClearDirectFromCpfpRefundTxid()
	return tnu
}

// SetRawRefundTx sets the "raw_refund_tx" field.
func (tnu *TreeNodeUpdate) SetRawRefundTx(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetRawRefundTx(b)
	return tnu
}

// ClearRawRefundTx clears the value of the "raw_refund_tx" field.
func (tnu *TreeNodeUpdate) ClearRawRefundTx() *TreeNodeUpdate {
	tnu.mutation.ClearRawRefundTx()
	return tnu
}

// SetDirectRefundTx sets the "direct_refund_tx" field.
func (tnu *TreeNodeUpdate) SetDirectRefundTx(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectRefundTx(b)
	return tnu
}

// ClearDirectRefundTx clears the value of the "direct_refund_tx" field.
func (tnu *TreeNodeUpdate) ClearDirectRefundTx() *TreeNodeUpdate {
	tnu.mutation.ClearDirectRefundTx()
	return tnu
}

// SetRawRefundTxid sets the "raw_refund_txid" field.
func (tnu *TreeNodeUpdate) SetRawRefundTxid(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetRawRefundTxid(b)
	return tnu
}

// ClearRawRefundTxid clears the value of the "raw_refund_txid" field.
func (tnu *TreeNodeUpdate) ClearRawRefundTxid() *TreeNodeUpdate {
	tnu.mutation.ClearRawRefundTxid()
	return tnu
}

// SetDirectRefundTxid sets the "direct_refund_txid" field.
func (tnu *TreeNodeUpdate) SetDirectRefundTxid(b []byte) *TreeNodeUpdate {
	tnu.mutation.SetDirectRefundTxid(b)
	return tnu
}

// ClearDirectRefundTxid clears the value of the "direct_refund_txid" field.
func (tnu *TreeNodeUpdate) ClearDirectRefundTxid() *TreeNodeUpdate {
	tnu.mutation.ClearDirectRefundTxid()
	return tnu
}

// SetTreeID sets the "tree" edge to the Tree entity by ID.
func (tnu *TreeNodeUpdate) SetTreeID(id uuid.UUID) *TreeNodeUpdate {
	tnu.mutation.SetTreeID(id)
	return tnu
}

// SetTree sets the "tree" edge to the Tree entity.
func (tnu *TreeNodeUpdate) SetTree(t *Tree) *TreeNodeUpdate {
	return tnu.SetTreeID(t.ID)
}

// SetParentID sets the "parent" edge to the TreeNode entity by ID.
func (tnu *TreeNodeUpdate) SetParentID(id uuid.UUID) *TreeNodeUpdate {
	tnu.mutation.SetParentID(id)
	return tnu
}

// SetNillableParentID sets the "parent" edge to the TreeNode entity by ID if the given value is not nil.
func (tnu *TreeNodeUpdate) SetNillableParentID(id *uuid.UUID) *TreeNodeUpdate {
	if id != nil {
		tnu = tnu.SetParentID(*id)
	}
	return tnu
}

// SetParent sets the "parent" edge to the TreeNode entity.
func (tnu *TreeNodeUpdate) SetParent(t *TreeNode) *TreeNodeUpdate {
	return tnu.SetParentID(t.ID)
}

// SetSigningKeyshareID sets the "signing_keyshare" edge to the SigningKeyshare entity by ID.
func (tnu *TreeNodeUpdate) SetSigningKeyshareID(id uuid.UUID) *TreeNodeUpdate {
	tnu.mutation.SetSigningKeyshareID(id)
	return tnu
}

// SetSigningKeyshare sets the "signing_keyshare" edge to the SigningKeyshare entity.
func (tnu *TreeNodeUpdate) SetSigningKeyshare(s *SigningKeyshare) *TreeNodeUpdate {
	return tnu.SetSigningKeyshareID(s.ID)
}

// AddChildIDs adds the "children" edge to the TreeNode entity by IDs.
func (tnu *TreeNodeUpdate) AddChildIDs(ids ...uuid.UUID) *TreeNodeUpdate {
	tnu.mutation.AddChildIDs(ids...)
	return tnu
}

// AddChildren adds the "children" edges to the TreeNode entity.
func (tnu *TreeNodeUpdate) AddChildren(t ...*TreeNode) *TreeNodeUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tnu.AddChildIDs(ids...)
}

// Mutation returns the TreeNodeMutation object of the builder.
func (tnu *TreeNodeUpdate) Mutation() *TreeNodeMutation {
	return tnu.mutation
}

// ClearTree clears the "tree" edge to the Tree entity.
func (tnu *TreeNodeUpdate) ClearTree() *TreeNodeUpdate {
	tnu.mutation.ClearTree()
	return tnu
}

// ClearParent clears the "parent" edge to the TreeNode entity.
func (tnu *TreeNodeUpdate) ClearParent() *TreeNodeUpdate {
	tnu.mutation.ClearParent()
	return tnu
}

// ClearSigningKeyshare clears the "signing_keyshare" edge to the SigningKeyshare entity.
func (tnu *TreeNodeUpdate) ClearSigningKeyshare() *TreeNodeUpdate {
	tnu.mutation.ClearSigningKeyshare()
	return tnu
}

// ClearChildren clears all "children" edges to the TreeNode entity.
func (tnu *TreeNodeUpdate) ClearChildren() *TreeNodeUpdate {
	tnu.mutation.ClearChildren()
	return tnu
}

// RemoveChildIDs removes the "children" edge to TreeNode entities by IDs.
func (tnu *TreeNodeUpdate) RemoveChildIDs(ids ...uuid.UUID) *TreeNodeUpdate {
	tnu.mutation.RemoveChildIDs(ids...)
	return tnu
}

// RemoveChildren removes "children" edges to TreeNode entities.
func (tnu *TreeNodeUpdate) RemoveChildren(t ...*TreeNode) *TreeNodeUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tnu.RemoveChildIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (tnu *TreeNodeUpdate) Save(ctx context.Context) (int, error) {
	if err := tnu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, tnu.sqlSave, tnu.mutation, tnu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tnu *TreeNodeUpdate) defaults() error {
	if _, ok := tnu.mutation.UpdateTime(); !ok {
		if treenode.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized treenode.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := treenode.UpdateDefaultUpdateTime()
		tnu.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tnu *TreeNodeUpdate) check() error {
	if v, ok := tnu.mutation.Status(); ok {
		if err := treenode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TreeNode.status": %w`, err)}
		}
	}
	if v, ok := tnu.mutation.RawTx(); ok {
		if err := treenode.RawTxValidator(v); err != nil {
			return &ValidationError{Name: "raw_tx", err: fmt.Errorf(`ent: validator failed for field "TreeNode.raw_tx": %w`, err)}
		}
	}
	if tnu.mutation.TreeCleared() && len(tnu.mutation.TreeIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TreeNode.tree"`)
	}
	if tnu.mutation.SigningKeyshareCleared() && len(tnu.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TreeNode.signing_keyshare"`)
	}
	return nil
}

func (tnu *TreeNodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tnu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(treenode.Table, treenode.Columns, sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID))
	if ps := tnu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tnu.mutation.UpdateTime(); ok {
		_spec.SetField(treenode.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tnu.mutation.Status(); ok {
		_spec.SetField(treenode.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tnu.mutation.OwnerIdentityPubkey(); ok {
		_spec.SetField(treenode.FieldOwnerIdentityPubkey, field.TypeBytes, value)
	}
	if value, ok := tnu.mutation.OwnerSigningPubkey(); ok {
		_spec.SetField(treenode.FieldOwnerSigningPubkey, field.TypeBytes, value)
	}
	if value, ok := tnu.mutation.Vout(); ok {
		_spec.SetField(treenode.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tnu.mutation.AddedVout(); ok {
		_spec.AddField(treenode.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tnu.mutation.NodeConfirmationHeight(); ok {
		_spec.SetField(treenode.FieldNodeConfirmationHeight, field.TypeUint64, value)
	}
	if value, ok := tnu.mutation.AddedNodeConfirmationHeight(); ok {
		_spec.AddField(treenode.FieldNodeConfirmationHeight, field.TypeUint64, value)
	}
	if tnu.mutation.NodeConfirmationHeightCleared() {
		_spec.ClearField(treenode.FieldNodeConfirmationHeight, field.TypeUint64)
	}
	if value, ok := tnu.mutation.RefundConfirmationHeight(); ok {
		_spec.SetField(treenode.FieldRefundConfirmationHeight, field.TypeUint64, value)
	}
	if value, ok := tnu.mutation.AddedRefundConfirmationHeight(); ok {
		_spec.AddField(treenode.FieldRefundConfirmationHeight, field.TypeUint64, value)
	}
	if tnu.mutation.RefundConfirmationHeightCleared() {
		_spec.ClearField(treenode.FieldRefundConfirmationHeight, field.TypeUint64)
	}
	if value, ok := tnu.mutation.RawTx(); ok {
		_spec.SetField(treenode.FieldRawTx, field.TypeBytes, value)
	}
	if value, ok := tnu.mutation.DirectTx(); ok {
		_spec.SetField(treenode.FieldDirectTx, field.TypeBytes, value)
	}
	if tnu.mutation.DirectTxCleared() {
		_spec.ClearField(treenode.FieldDirectTx, field.TypeBytes)
	}
	if value, ok := tnu.mutation.DirectFromCpfpRefundTx(); ok {
		_spec.SetField(treenode.FieldDirectFromCpfpRefundTx, field.TypeBytes, value)
	}
	if tnu.mutation.DirectFromCpfpRefundTxCleared() {
		_spec.ClearField(treenode.FieldDirectFromCpfpRefundTx, field.TypeBytes)
	}
	if value, ok := tnu.mutation.RawTxid(); ok {
		_spec.SetField(treenode.FieldRawTxid, field.TypeBytes, value)
	}
	if tnu.mutation.RawTxidCleared() {
		_spec.ClearField(treenode.FieldRawTxid, field.TypeBytes)
	}
	if value, ok := tnu.mutation.DirectTxid(); ok {
		_spec.SetField(treenode.FieldDirectTxid, field.TypeBytes, value)
	}
	if tnu.mutation.DirectTxidCleared() {
		_spec.ClearField(treenode.FieldDirectTxid, field.TypeBytes)
	}
	if value, ok := tnu.mutation.DirectFromCpfpRefundTxid(); ok {
		_spec.SetField(treenode.FieldDirectFromCpfpRefundTxid, field.TypeBytes, value)
	}
	if tnu.mutation.DirectFromCpfpRefundTxidCleared() {
		_spec.ClearField(treenode.FieldDirectFromCpfpRefundTxid, field.TypeBytes)
	}
	if value, ok := tnu.mutation.RawRefundTx(); ok {
		_spec.SetField(treenode.FieldRawRefundTx, field.TypeBytes, value)
	}
	if tnu.mutation.RawRefundTxCleared() {
		_spec.ClearField(treenode.FieldRawRefundTx, field.TypeBytes)
	}
	if value, ok := tnu.mutation.DirectRefundTx(); ok {
		_spec.SetField(treenode.FieldDirectRefundTx, field.TypeBytes, value)
	}
	if tnu.mutation.DirectRefundTxCleared() {
		_spec.ClearField(treenode.FieldDirectRefundTx, field.TypeBytes)
	}
	if value, ok := tnu.mutation.RawRefundTxid(); ok {
		_spec.SetField(treenode.FieldRawRefundTxid, field.TypeBytes, value)
	}
	if tnu.mutation.RawRefundTxidCleared() {
		_spec.ClearField(treenode.FieldRawRefundTxid, field.TypeBytes)
	}
	if value, ok := tnu.mutation.DirectRefundTxid(); ok {
		_spec.SetField(treenode.FieldDirectRefundTxid, field.TypeBytes, value)
	}
	if tnu.mutation.DirectRefundTxidCleared() {
		_spec.ClearField(treenode.FieldDirectRefundTxid, field.TypeBytes)
	}
	if tnu.mutation.TreeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.TreeTable,
			Columns: []string{treenode.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnu.mutation.TreeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.TreeTable,
			Columns: []string{treenode.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnu.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.ParentTable,
			Columns: []string{treenode.ParentColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnu.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.ParentTable,
			Columns: []string{treenode.ParentColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnu.mutation.SigningKeyshareCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.SigningKeyshareTable,
			Columns: []string{treenode.SigningKeyshareColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnu.mutation.SigningKeyshareIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.SigningKeyshareTable,
			Columns: []string{treenode.SigningKeyshareColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnu.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnu.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !tnu.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnu.mutation.ChildrenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		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, tnu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{treenode.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tnu.mutation.done = true
	return n, nil
}

// TreeNodeUpdateOne is the builder for updating a single TreeNode entity.
type TreeNodeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TreeNodeMutation
}

// SetUpdateTime sets the "update_time" field.
func (tnuo *TreeNodeUpdateOne) SetUpdateTime(t time.Time) *TreeNodeUpdateOne {
	tnuo.mutation.SetUpdateTime(t)
	return tnuo
}

// SetStatus sets the "status" field.
func (tnuo *TreeNodeUpdateOne) SetStatus(sns schematype.TreeNodeStatus) *TreeNodeUpdateOne {
	tnuo.mutation.SetStatus(sns)
	return tnuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableStatus(sns *schematype.TreeNodeStatus) *TreeNodeUpdateOne {
	if sns != nil {
		tnuo.SetStatus(*sns)
	}
	return tnuo
}

// SetOwnerIdentityPubkey sets the "owner_identity_pubkey" field.
func (tnuo *TreeNodeUpdateOne) SetOwnerIdentityPubkey(k keys.Public) *TreeNodeUpdateOne {
	tnuo.mutation.SetOwnerIdentityPubkey(k)
	return tnuo
}

// SetNillableOwnerIdentityPubkey sets the "owner_identity_pubkey" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableOwnerIdentityPubkey(k *keys.Public) *TreeNodeUpdateOne {
	if k != nil {
		tnuo.SetOwnerIdentityPubkey(*k)
	}
	return tnuo
}

// SetOwnerSigningPubkey sets the "owner_signing_pubkey" field.
func (tnuo *TreeNodeUpdateOne) SetOwnerSigningPubkey(k keys.Public) *TreeNodeUpdateOne {
	tnuo.mutation.SetOwnerSigningPubkey(k)
	return tnuo
}

// SetNillableOwnerSigningPubkey sets the "owner_signing_pubkey" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableOwnerSigningPubkey(k *keys.Public) *TreeNodeUpdateOne {
	if k != nil {
		tnuo.SetOwnerSigningPubkey(*k)
	}
	return tnuo
}

// SetVout sets the "vout" field.
func (tnuo *TreeNodeUpdateOne) SetVout(i int16) *TreeNodeUpdateOne {
	tnuo.mutation.ResetVout()
	tnuo.mutation.SetVout(i)
	return tnuo
}

// SetNillableVout sets the "vout" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableVout(i *int16) *TreeNodeUpdateOne {
	if i != nil {
		tnuo.SetVout(*i)
	}
	return tnuo
}

// AddVout adds i to the "vout" field.
func (tnuo *TreeNodeUpdateOne) AddVout(i int16) *TreeNodeUpdateOne {
	tnuo.mutation.AddVout(i)
	return tnuo
}

// SetNodeConfirmationHeight sets the "node_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) SetNodeConfirmationHeight(u uint64) *TreeNodeUpdateOne {
	tnuo.mutation.ResetNodeConfirmationHeight()
	tnuo.mutation.SetNodeConfirmationHeight(u)
	return tnuo
}

// SetNillableNodeConfirmationHeight sets the "node_confirmation_height" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableNodeConfirmationHeight(u *uint64) *TreeNodeUpdateOne {
	if u != nil {
		tnuo.SetNodeConfirmationHeight(*u)
	}
	return tnuo
}

// AddNodeConfirmationHeight adds u to the "node_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) AddNodeConfirmationHeight(u int64) *TreeNodeUpdateOne {
	tnuo.mutation.AddNodeConfirmationHeight(u)
	return tnuo
}

// ClearNodeConfirmationHeight clears the value of the "node_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) ClearNodeConfirmationHeight() *TreeNodeUpdateOne {
	tnuo.mutation.ClearNodeConfirmationHeight()
	return tnuo
}

// SetRefundConfirmationHeight sets the "refund_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) SetRefundConfirmationHeight(u uint64) *TreeNodeUpdateOne {
	tnuo.mutation.ResetRefundConfirmationHeight()
	tnuo.mutation.SetRefundConfirmationHeight(u)
	return tnuo
}

// SetNillableRefundConfirmationHeight sets the "refund_confirmation_height" field if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableRefundConfirmationHeight(u *uint64) *TreeNodeUpdateOne {
	if u != nil {
		tnuo.SetRefundConfirmationHeight(*u)
	}
	return tnuo
}

// AddRefundConfirmationHeight adds u to the "refund_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) AddRefundConfirmationHeight(u int64) *TreeNodeUpdateOne {
	tnuo.mutation.AddRefundConfirmationHeight(u)
	return tnuo
}

// ClearRefundConfirmationHeight clears the value of the "refund_confirmation_height" field.
func (tnuo *TreeNodeUpdateOne) ClearRefundConfirmationHeight() *TreeNodeUpdateOne {
	tnuo.mutation.ClearRefundConfirmationHeight()
	return tnuo
}

// SetRawTx sets the "raw_tx" field.
func (tnuo *TreeNodeUpdateOne) SetRawTx(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetRawTx(b)
	return tnuo
}

// SetDirectTx sets the "direct_tx" field.
func (tnuo *TreeNodeUpdateOne) SetDirectTx(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectTx(b)
	return tnuo
}

// ClearDirectTx clears the value of the "direct_tx" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectTx() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectTx()
	return tnuo
}

// SetDirectFromCpfpRefundTx sets the "direct_from_cpfp_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) SetDirectFromCpfpRefundTx(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectFromCpfpRefundTx(b)
	return tnuo
}

// ClearDirectFromCpfpRefundTx clears the value of the "direct_from_cpfp_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectFromCpfpRefundTx() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectFromCpfpRefundTx()
	return tnuo
}

// SetRawTxid sets the "raw_txid" field.
func (tnuo *TreeNodeUpdateOne) SetRawTxid(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetRawTxid(b)
	return tnuo
}

// ClearRawTxid clears the value of the "raw_txid" field.
func (tnuo *TreeNodeUpdateOne) ClearRawTxid() *TreeNodeUpdateOne {
	tnuo.mutation.ClearRawTxid()
	return tnuo
}

// SetDirectTxid sets the "direct_txid" field.
func (tnuo *TreeNodeUpdateOne) SetDirectTxid(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectTxid(b)
	return tnuo
}

// ClearDirectTxid clears the value of the "direct_txid" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectTxid() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectTxid()
	return tnuo
}

// SetDirectFromCpfpRefundTxid sets the "direct_from_cpfp_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) SetDirectFromCpfpRefundTxid(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectFromCpfpRefundTxid(b)
	return tnuo
}

// ClearDirectFromCpfpRefundTxid clears the value of the "direct_from_cpfp_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectFromCpfpRefundTxid() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectFromCpfpRefundTxid()
	return tnuo
}

// SetRawRefundTx sets the "raw_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) SetRawRefundTx(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetRawRefundTx(b)
	return tnuo
}

// ClearRawRefundTx clears the value of the "raw_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) ClearRawRefundTx() *TreeNodeUpdateOne {
	tnuo.mutation.ClearRawRefundTx()
	return tnuo
}

// SetDirectRefundTx sets the "direct_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) SetDirectRefundTx(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectRefundTx(b)
	return tnuo
}

// ClearDirectRefundTx clears the value of the "direct_refund_tx" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectRefundTx() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectRefundTx()
	return tnuo
}

// SetRawRefundTxid sets the "raw_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) SetRawRefundTxid(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetRawRefundTxid(b)
	return tnuo
}

// ClearRawRefundTxid clears the value of the "raw_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) ClearRawRefundTxid() *TreeNodeUpdateOne {
	tnuo.mutation.ClearRawRefundTxid()
	return tnuo
}

// SetDirectRefundTxid sets the "direct_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) SetDirectRefundTxid(b []byte) *TreeNodeUpdateOne {
	tnuo.mutation.SetDirectRefundTxid(b)
	return tnuo
}

// ClearDirectRefundTxid clears the value of the "direct_refund_txid" field.
func (tnuo *TreeNodeUpdateOne) ClearDirectRefundTxid() *TreeNodeUpdateOne {
	tnuo.mutation.ClearDirectRefundTxid()
	return tnuo
}

// SetTreeID sets the "tree" edge to the Tree entity by ID.
func (tnuo *TreeNodeUpdateOne) SetTreeID(id uuid.UUID) *TreeNodeUpdateOne {
	tnuo.mutation.SetTreeID(id)
	return tnuo
}

// SetTree sets the "tree" edge to the Tree entity.
func (tnuo *TreeNodeUpdateOne) SetTree(t *Tree) *TreeNodeUpdateOne {
	return tnuo.SetTreeID(t.ID)
}

// SetParentID sets the "parent" edge to the TreeNode entity by ID.
func (tnuo *TreeNodeUpdateOne) SetParentID(id uuid.UUID) *TreeNodeUpdateOne {
	tnuo.mutation.SetParentID(id)
	return tnuo
}

// SetNillableParentID sets the "parent" edge to the TreeNode entity by ID if the given value is not nil.
func (tnuo *TreeNodeUpdateOne) SetNillableParentID(id *uuid.UUID) *TreeNodeUpdateOne {
	if id != nil {
		tnuo = tnuo.SetParentID(*id)
	}
	return tnuo
}

// SetParent sets the "parent" edge to the TreeNode entity.
func (tnuo *TreeNodeUpdateOne) SetParent(t *TreeNode) *TreeNodeUpdateOne {
	return tnuo.SetParentID(t.ID)
}

// SetSigningKeyshareID sets the "signing_keyshare" edge to the SigningKeyshare entity by ID.
func (tnuo *TreeNodeUpdateOne) SetSigningKeyshareID(id uuid.UUID) *TreeNodeUpdateOne {
	tnuo.mutation.SetSigningKeyshareID(id)
	return tnuo
}

// SetSigningKeyshare sets the "signing_keyshare" edge to the SigningKeyshare entity.
func (tnuo *TreeNodeUpdateOne) SetSigningKeyshare(s *SigningKeyshare) *TreeNodeUpdateOne {
	return tnuo.SetSigningKeyshareID(s.ID)
}

// AddChildIDs adds the "children" edge to the TreeNode entity by IDs.
func (tnuo *TreeNodeUpdateOne) AddChildIDs(ids ...uuid.UUID) *TreeNodeUpdateOne {
	tnuo.mutation.AddChildIDs(ids...)
	return tnuo
}

// AddChildren adds the "children" edges to the TreeNode entity.
func (tnuo *TreeNodeUpdateOne) AddChildren(t ...*TreeNode) *TreeNodeUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tnuo.AddChildIDs(ids...)
}

// Mutation returns the TreeNodeMutation object of the builder.
func (tnuo *TreeNodeUpdateOne) Mutation() *TreeNodeMutation {
	return tnuo.mutation
}

// ClearTree clears the "tree" edge to the Tree entity.
func (tnuo *TreeNodeUpdateOne) ClearTree() *TreeNodeUpdateOne {
	tnuo.mutation.ClearTree()
	return tnuo
}

// ClearParent clears the "parent" edge to the TreeNode entity.
func (tnuo *TreeNodeUpdateOne) ClearParent() *TreeNodeUpdateOne {
	tnuo.mutation.ClearParent()
	return tnuo
}

// ClearSigningKeyshare clears the "signing_keyshare" edge to the SigningKeyshare entity.
func (tnuo *TreeNodeUpdateOne) ClearSigningKeyshare() *TreeNodeUpdateOne {
	tnuo.mutation.ClearSigningKeyshare()
	return tnuo
}

// ClearChildren clears all "children" edges to the TreeNode entity.
func (tnuo *TreeNodeUpdateOne) ClearChildren() *TreeNodeUpdateOne {
	tnuo.mutation.ClearChildren()
	return tnuo
}

// RemoveChildIDs removes the "children" edge to TreeNode entities by IDs.
func (tnuo *TreeNodeUpdateOne) RemoveChildIDs(ids ...uuid.UUID) *TreeNodeUpdateOne {
	tnuo.mutation.RemoveChildIDs(ids...)
	return tnuo
}

// RemoveChildren removes "children" edges to TreeNode entities.
func (tnuo *TreeNodeUpdateOne) RemoveChildren(t ...*TreeNode) *TreeNodeUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tnuo.RemoveChildIDs(ids...)
}

// Where appends a list predicates to the TreeNodeUpdate builder.
func (tnuo *TreeNodeUpdateOne) Where(ps ...predicate.TreeNode) *TreeNodeUpdateOne {
	tnuo.mutation.Where(ps...)
	return tnuo
}

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

// Save executes the query and returns the updated TreeNode entity.
func (tnuo *TreeNodeUpdateOne) Save(ctx context.Context) (*TreeNode, error) {
	if err := tnuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, tnuo.sqlSave, tnuo.mutation, tnuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (tnuo *TreeNodeUpdateOne) defaults() error {
	if _, ok := tnuo.mutation.UpdateTime(); !ok {
		if treenode.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized treenode.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := treenode.UpdateDefaultUpdateTime()
		tnuo.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (tnuo *TreeNodeUpdateOne) check() error {
	if v, ok := tnuo.mutation.Status(); ok {
		if err := treenode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "TreeNode.status": %w`, err)}
		}
	}
	if v, ok := tnuo.mutation.RawTx(); ok {
		if err := treenode.RawTxValidator(v); err != nil {
			return &ValidationError{Name: "raw_tx", err: fmt.Errorf(`ent: validator failed for field "TreeNode.raw_tx": %w`, err)}
		}
	}
	if tnuo.mutation.TreeCleared() && len(tnuo.mutation.TreeIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TreeNode.tree"`)
	}
	if tnuo.mutation.SigningKeyshareCleared() && len(tnuo.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "TreeNode.signing_keyshare"`)
	}
	return nil
}

func (tnuo *TreeNodeUpdateOne) sqlSave(ctx context.Context) (_node *TreeNode, err error) {
	if err := tnuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(treenode.Table, treenode.Columns, sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID))
	id, ok := tnuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "TreeNode.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tnuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, treenode.FieldID)
		for _, f := range fields {
			if !treenode.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != treenode.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tnuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tnuo.mutation.UpdateTime(); ok {
		_spec.SetField(treenode.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tnuo.mutation.Status(); ok {
		_spec.SetField(treenode.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tnuo.mutation.OwnerIdentityPubkey(); ok {
		_spec.SetField(treenode.FieldOwnerIdentityPubkey, field.TypeBytes, value)
	}
	if value, ok := tnuo.mutation.OwnerSigningPubkey(); ok {
		_spec.SetField(treenode.FieldOwnerSigningPubkey, field.TypeBytes, value)
	}
	if value, ok := tnuo.mutation.Vout(); ok {
		_spec.SetField(treenode.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tnuo.mutation.AddedVout(); ok {
		_spec.AddField(treenode.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tnuo.mutation.NodeConfirmationHeight(); ok {
		_spec.SetField(treenode.FieldNodeConfirmationHeight, field.TypeUint64, value)
	}
	if value, ok := tnuo.mutation.AddedNodeConfirmationHeight(); ok {
		_spec.AddField(treenode.FieldNodeConfirmationHeight, field.TypeUint64, value)
	}
	if tnuo.mutation.NodeConfirmationHeightCleared() {
		_spec.ClearField(treenode.FieldNodeConfirmationHeight, field.TypeUint64)
	}
	if value, ok := tnuo.mutation.RefundConfirmationHeight(); ok {
		_spec.SetField(treenode.FieldRefundConfirmationHeight, field.TypeUint64, value)
	}
	if value, ok := tnuo.mutation.AddedRefundConfirmationHeight(); ok {
		_spec.AddField(treenode.FieldRefundConfirmationHeight, field.TypeUint64, value)
	}
	if tnuo.mutation.RefundConfirmationHeightCleared() {
		_spec.ClearField(treenode.FieldRefundConfirmationHeight, field.TypeUint64)
	}
	if value, ok := tnuo.mutation.RawTx(); ok {
		_spec.SetField(treenode.FieldRawTx, field.TypeBytes, value)
	}
	if value, ok := tnuo.mutation.DirectTx(); ok {
		_spec.SetField(treenode.FieldDirectTx, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectTxCleared() {
		_spec.ClearField(treenode.FieldDirectTx, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.DirectFromCpfpRefundTx(); ok {
		_spec.SetField(treenode.FieldDirectFromCpfpRefundTx, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectFromCpfpRefundTxCleared() {
		_spec.ClearField(treenode.FieldDirectFromCpfpRefundTx, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.RawTxid(); ok {
		_spec.SetField(treenode.FieldRawTxid, field.TypeBytes, value)
	}
	if tnuo.mutation.RawTxidCleared() {
		_spec.ClearField(treenode.FieldRawTxid, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.DirectTxid(); ok {
		_spec.SetField(treenode.FieldDirectTxid, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectTxidCleared() {
		_spec.ClearField(treenode.FieldDirectTxid, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.DirectFromCpfpRefundTxid(); ok {
		_spec.SetField(treenode.FieldDirectFromCpfpRefundTxid, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectFromCpfpRefundTxidCleared() {
		_spec.ClearField(treenode.FieldDirectFromCpfpRefundTxid, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.RawRefundTx(); ok {
		_spec.SetField(treenode.FieldRawRefundTx, field.TypeBytes, value)
	}
	if tnuo.mutation.RawRefundTxCleared() {
		_spec.ClearField(treenode.FieldRawRefundTx, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.DirectRefundTx(); ok {
		_spec.SetField(treenode.FieldDirectRefundTx, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectRefundTxCleared() {
		_spec.ClearField(treenode.FieldDirectRefundTx, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.RawRefundTxid(); ok {
		_spec.SetField(treenode.FieldRawRefundTxid, field.TypeBytes, value)
	}
	if tnuo.mutation.RawRefundTxidCleared() {
		_spec.ClearField(treenode.FieldRawRefundTxid, field.TypeBytes)
	}
	if value, ok := tnuo.mutation.DirectRefundTxid(); ok {
		_spec.SetField(treenode.FieldDirectRefundTxid, field.TypeBytes, value)
	}
	if tnuo.mutation.DirectRefundTxidCleared() {
		_spec.ClearField(treenode.FieldDirectRefundTxid, field.TypeBytes)
	}
	if tnuo.mutation.TreeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.TreeTable,
			Columns: []string{treenode.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnuo.mutation.TreeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.TreeTable,
			Columns: []string{treenode.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnuo.mutation.ParentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.ParentTable,
			Columns: []string{treenode.ParentColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnuo.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.ParentTable,
			Columns: []string{treenode.ParentColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnuo.mutation.SigningKeyshareCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.SigningKeyshareTable,
			Columns: []string{treenode.SigningKeyshareColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnuo.mutation.SigningKeyshareIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   treenode.SigningKeyshareTable,
			Columns: []string{treenode.SigningKeyshareColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signingkeyshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tnuo.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnuo.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !tnuo.mutation.ChildrenCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tnuo.mutation.ChildrenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   treenode.ChildrenTable,
			Columns: []string{treenode.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &TreeNode{config: tnuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tnuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{treenode.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tnuo.mutation.done = true
	return _node, nil
}
