// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "thesis/ent/key" "thesis/ent/predicate" "thesis/ent/transactions" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // KeyUpdate is the builder for updating Key entities. type KeyUpdate struct { config hooks []Hook mutation *KeyMutation } // Where appends a list predicates to the KeyUpdate builder. func (ku *KeyUpdate) Where(ps ...predicate.Key) *KeyUpdate { ku.mutation.Where(ps...) return ku } // SetPublicKey sets the "publicKey" field. func (ku *KeyUpdate) SetPublicKey(s string) *KeyUpdate { ku.mutation.SetPublicKey(s) return ku } // SetNillablePublicKey sets the "publicKey" field if the given value is not nil. func (ku *KeyUpdate) SetNillablePublicKey(s *string) *KeyUpdate { if s != nil { ku.SetPublicKey(*s) } return ku } // SetOwner sets the "Owner" field. func (ku *KeyUpdate) SetOwner(s string) *KeyUpdate { ku.mutation.SetOwner(s) return ku } // SetNillableOwner sets the "Owner" field if the given value is not nil. func (ku *KeyUpdate) SetNillableOwner(s *string) *KeyUpdate { if s != nil { ku.SetOwner(*s) } return ku } // SetTrustScore sets the "trustScore" field. func (ku *KeyUpdate) SetTrustScore(f float64) *KeyUpdate { ku.mutation.ResetTrustScore() ku.mutation.SetTrustScore(f) return ku } // SetNillableTrustScore sets the "trustScore" field if the given value is not nil. func (ku *KeyUpdate) SetNillableTrustScore(f *float64) *KeyUpdate { if f != nil { ku.SetTrustScore(*f) } return ku } // AddTrustScore adds f to the "trustScore" field. func (ku *KeyUpdate) AddTrustScore(f float64) *KeyUpdate { ku.mutation.AddTrustScore(f) return ku } // AddSignedIDs adds the "Signed" edge to the Transactions entity by IDs. func (ku *KeyUpdate) AddSignedIDs(ids ...int) *KeyUpdate { ku.mutation.AddSignedIDs(ids...) return ku } // AddSigned adds the "Signed" edges to the Transactions entity. func (ku *KeyUpdate) AddSigned(t ...*Transactions) *KeyUpdate { ids := make([]int, len(t)) for i := range t { ids[i] = t[i].ID } return ku.AddSignedIDs(ids...) } // Mutation returns the KeyMutation object of the builder. func (ku *KeyUpdate) Mutation() *KeyMutation { return ku.mutation } // ClearSigned clears all "Signed" edges to the Transactions entity. func (ku *KeyUpdate) ClearSigned() *KeyUpdate { ku.mutation.ClearSigned() return ku } // RemoveSignedIDs removes the "Signed" edge to Transactions entities by IDs. func (ku *KeyUpdate) RemoveSignedIDs(ids ...int) *KeyUpdate { ku.mutation.RemoveSignedIDs(ids...) return ku } // RemoveSigned removes "Signed" edges to Transactions entities. func (ku *KeyUpdate) RemoveSigned(t ...*Transactions) *KeyUpdate { ids := make([]int, len(t)) for i := range t { ids[i] = t[i].ID } return ku.RemoveSignedIDs(ids...) } // Save executes the query and returns the number of nodes affected by the update operation. func (ku *KeyUpdate) Save(ctx context.Context) (int, error) { return withHooks(ctx, ku.sqlSave, ku.mutation, ku.hooks) } // SaveX is like Save, but panics if an error occurs. func (ku *KeyUpdate) SaveX(ctx context.Context) int { affected, err := ku.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (ku *KeyUpdate) Exec(ctx context.Context) error { _, err := ku.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (ku *KeyUpdate) ExecX(ctx context.Context) { if err := ku.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (ku *KeyUpdate) check() error { if v, ok := ku.mutation.PublicKey(); ok { if err := key.PublicKeyValidator(v); err != nil { return &ValidationError{Name: "publicKey", err: fmt.Errorf(`ent: validator failed for field "Key.publicKey": %w`, err)} } } if v, ok := ku.mutation.Owner(); ok { if err := key.OwnerValidator(v); err != nil { return &ValidationError{Name: "Owner", err: fmt.Errorf(`ent: validator failed for field "Key.Owner": %w`, err)} } } return nil } func (ku *KeyUpdate) sqlSave(ctx context.Context) (n int, err error) { if err := ku.check(); err != nil { return n, err } _spec := sqlgraph.NewUpdateSpec(key.Table, key.Columns, sqlgraph.NewFieldSpec(key.FieldID, field.TypeInt)) if ps := ku.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := ku.mutation.PublicKey(); ok { _spec.SetField(key.FieldPublicKey, field.TypeString, value) } if value, ok := ku.mutation.Owner(); ok { _spec.SetField(key.FieldOwner, field.TypeString, value) } if value, ok := ku.mutation.TrustScore(); ok { _spec.SetField(key.FieldTrustScore, field.TypeFloat64, value) } if value, ok := ku.mutation.AddedTrustScore(); ok { _spec.AddField(key.FieldTrustScore, field.TypeFloat64, value) } if ku.mutation.SignedCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := ku.mutation.RemovedSignedIDs(); len(nodes) > 0 && !ku.mutation.SignedCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := ku.mutation.SignedIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.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, ku.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{key.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } ku.mutation.done = true return n, nil } // KeyUpdateOne is the builder for updating a single Key entity. type KeyUpdateOne struct { config fields []string hooks []Hook mutation *KeyMutation } // SetPublicKey sets the "publicKey" field. func (kuo *KeyUpdateOne) SetPublicKey(s string) *KeyUpdateOne { kuo.mutation.SetPublicKey(s) return kuo } // SetNillablePublicKey sets the "publicKey" field if the given value is not nil. func (kuo *KeyUpdateOne) SetNillablePublicKey(s *string) *KeyUpdateOne { if s != nil { kuo.SetPublicKey(*s) } return kuo } // SetOwner sets the "Owner" field. func (kuo *KeyUpdateOne) SetOwner(s string) *KeyUpdateOne { kuo.mutation.SetOwner(s) return kuo } // SetNillableOwner sets the "Owner" field if the given value is not nil. func (kuo *KeyUpdateOne) SetNillableOwner(s *string) *KeyUpdateOne { if s != nil { kuo.SetOwner(*s) } return kuo } // SetTrustScore sets the "trustScore" field. func (kuo *KeyUpdateOne) SetTrustScore(f float64) *KeyUpdateOne { kuo.mutation.ResetTrustScore() kuo.mutation.SetTrustScore(f) return kuo } // SetNillableTrustScore sets the "trustScore" field if the given value is not nil. func (kuo *KeyUpdateOne) SetNillableTrustScore(f *float64) *KeyUpdateOne { if f != nil { kuo.SetTrustScore(*f) } return kuo } // AddTrustScore adds f to the "trustScore" field. func (kuo *KeyUpdateOne) AddTrustScore(f float64) *KeyUpdateOne { kuo.mutation.AddTrustScore(f) return kuo } // AddSignedIDs adds the "Signed" edge to the Transactions entity by IDs. func (kuo *KeyUpdateOne) AddSignedIDs(ids ...int) *KeyUpdateOne { kuo.mutation.AddSignedIDs(ids...) return kuo } // AddSigned adds the "Signed" edges to the Transactions entity. func (kuo *KeyUpdateOne) AddSigned(t ...*Transactions) *KeyUpdateOne { ids := make([]int, len(t)) for i := range t { ids[i] = t[i].ID } return kuo.AddSignedIDs(ids...) } // Mutation returns the KeyMutation object of the builder. func (kuo *KeyUpdateOne) Mutation() *KeyMutation { return kuo.mutation } // ClearSigned clears all "Signed" edges to the Transactions entity. func (kuo *KeyUpdateOne) ClearSigned() *KeyUpdateOne { kuo.mutation.ClearSigned() return kuo } // RemoveSignedIDs removes the "Signed" edge to Transactions entities by IDs. func (kuo *KeyUpdateOne) RemoveSignedIDs(ids ...int) *KeyUpdateOne { kuo.mutation.RemoveSignedIDs(ids...) return kuo } // RemoveSigned removes "Signed" edges to Transactions entities. func (kuo *KeyUpdateOne) RemoveSigned(t ...*Transactions) *KeyUpdateOne { ids := make([]int, len(t)) for i := range t { ids[i] = t[i].ID } return kuo.RemoveSignedIDs(ids...) } // Where appends a list predicates to the KeyUpdate builder. func (kuo *KeyUpdateOne) Where(ps ...predicate.Key) *KeyUpdateOne { kuo.mutation.Where(ps...) return kuo } // Select allows selecting one or more fields (columns) of the returned entity. // The default is selecting all fields defined in the entity schema. func (kuo *KeyUpdateOne) Select(field string, fields ...string) *KeyUpdateOne { kuo.fields = append([]string{field}, fields...) return kuo } // Save executes the query and returns the updated Key entity. func (kuo *KeyUpdateOne) Save(ctx context.Context) (*Key, error) { return withHooks(ctx, kuo.sqlSave, kuo.mutation, kuo.hooks) } // SaveX is like Save, but panics if an error occurs. func (kuo *KeyUpdateOne) SaveX(ctx context.Context) *Key { node, err := kuo.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (kuo *KeyUpdateOne) Exec(ctx context.Context) error { _, err := kuo.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (kuo *KeyUpdateOne) ExecX(ctx context.Context) { if err := kuo.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (kuo *KeyUpdateOne) check() error { if v, ok := kuo.mutation.PublicKey(); ok { if err := key.PublicKeyValidator(v); err != nil { return &ValidationError{Name: "publicKey", err: fmt.Errorf(`ent: validator failed for field "Key.publicKey": %w`, err)} } } if v, ok := kuo.mutation.Owner(); ok { if err := key.OwnerValidator(v); err != nil { return &ValidationError{Name: "Owner", err: fmt.Errorf(`ent: validator failed for field "Key.Owner": %w`, err)} } } return nil } func (kuo *KeyUpdateOne) sqlSave(ctx context.Context) (_node *Key, err error) { if err := kuo.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(key.Table, key.Columns, sqlgraph.NewFieldSpec(key.FieldID, field.TypeInt)) id, ok := kuo.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Key.id" for update`)} } _spec.Node.ID.Value = id if fields := kuo.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, key.FieldID) for _, f := range fields { if !key.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != key.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, f) } } } if ps := kuo.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := kuo.mutation.PublicKey(); ok { _spec.SetField(key.FieldPublicKey, field.TypeString, value) } if value, ok := kuo.mutation.Owner(); ok { _spec.SetField(key.FieldOwner, field.TypeString, value) } if value, ok := kuo.mutation.TrustScore(); ok { _spec.SetField(key.FieldTrustScore, field.TypeFloat64, value) } if value, ok := kuo.mutation.AddedTrustScore(); ok { _spec.AddField(key.FieldTrustScore, field.TypeFloat64, value) } if kuo.mutation.SignedCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := kuo.mutation.RemovedSignedIDs(); len(nodes) > 0 && !kuo.mutation.SignedCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := kuo.mutation.SignedIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: key.SignedTable, Columns: key.SignedPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Key{config: kuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues if err = sqlgraph.UpdateNode(ctx, kuo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{key.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } kuo.mutation.done = true return _node, nil }