// 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" "tangled.sh/seiso.moe/aletheia.directory/ent/operation" "tangled.sh/seiso.moe/aletheia.directory/ent/predicate" "tangled.sh/seiso.moe/aletheia.directory/pkg/plc" ) // OperationUpdate is the builder for updating Operation entities. type OperationUpdate struct { config hooks []Hook mutation *OperationMutation } // Where appends a list predicates to the OperationUpdate builder. func (ou *OperationUpdate) Where(ps ...predicate.Operation) *OperationUpdate { ou.mutation.Where(ps...) return ou } // SetDid sets the "did" field. func (ou *OperationUpdate) SetDid(s string) *OperationUpdate { ou.mutation.SetDid(s) return ou } // SetNillableDid sets the "did" field if the given value is not nil. func (ou *OperationUpdate) SetNillableDid(s *string) *OperationUpdate { if s != nil { ou.SetDid(*s) } return ou } // SetOperation sets the "operation" field. func (ou *OperationUpdate) SetOperation(po plc.PLCOperation) *OperationUpdate { ou.mutation.SetOperation(po) return ou } // SetNillableOperation sets the "operation" field if the given value is not nil. func (ou *OperationUpdate) SetNillableOperation(po *plc.PLCOperation) *OperationUpdate { if po != nil { ou.SetOperation(*po) } return ou } // SetCid sets the "cid" field. func (ou *OperationUpdate) SetCid(s string) *OperationUpdate { ou.mutation.SetCid(s) return ou } // SetNillableCid sets the "cid" field if the given value is not nil. func (ou *OperationUpdate) SetNillableCid(s *string) *OperationUpdate { if s != nil { ou.SetCid(*s) } return ou } // SetNullified sets the "nullified" field. func (ou *OperationUpdate) SetNullified(b bool) *OperationUpdate { ou.mutation.SetNullified(b) return ou } // SetNillableNullified sets the "nullified" field if the given value is not nil. func (ou *OperationUpdate) SetNillableNullified(b *bool) *OperationUpdate { if b != nil { ou.SetNullified(*b) } return ou } // SetCreatedAt sets the "created_at" field. func (ou *OperationUpdate) SetCreatedAt(t time.Time) *OperationUpdate { ou.mutation.SetCreatedAt(t) return ou } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (ou *OperationUpdate) SetNillableCreatedAt(t *time.Time) *OperationUpdate { if t != nil { ou.SetCreatedAt(*t) } return ou } // Mutation returns the OperationMutation object of the builder. func (ou *OperationUpdate) Mutation() *OperationMutation { return ou.mutation } // Save executes the query and returns the number of nodes affected by the update operation. func (ou *OperationUpdate) Save(ctx context.Context) (int, error) { return withHooks(ctx, ou.sqlSave, ou.mutation, ou.hooks) } // SaveX is like Save, but panics if an error occurs. func (ou *OperationUpdate) SaveX(ctx context.Context) int { affected, err := ou.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (ou *OperationUpdate) Exec(ctx context.Context) error { _, err := ou.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (ou *OperationUpdate) ExecX(ctx context.Context) { if err := ou.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (ou *OperationUpdate) check() error { if v, ok := ou.mutation.Did(); ok { if err := operation.DidValidator(v); err != nil { return &ValidationError{Name: "did", err: fmt.Errorf(`ent: validator failed for field "Operation.did": %w`, err)} } } if v, ok := ou.mutation.Cid(); ok { if err := operation.CidValidator(v); err != nil { return &ValidationError{Name: "cid", err: fmt.Errorf(`ent: validator failed for field "Operation.cid": %w`, err)} } } return nil } func (ou *OperationUpdate) sqlSave(ctx context.Context) (n int, err error) { if err := ou.check(); err != nil { return n, err } _spec := sqlgraph.NewUpdateSpec(operation.Table, operation.Columns, sqlgraph.NewFieldSpec(operation.FieldID, field.TypeInt)) if ps := ou.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := ou.mutation.Did(); ok { _spec.SetField(operation.FieldDid, field.TypeString, value) } if value, ok := ou.mutation.Operation(); ok { _spec.SetField(operation.FieldOperation, field.TypeJSON, value) } if value, ok := ou.mutation.Cid(); ok { _spec.SetField(operation.FieldCid, field.TypeString, value) } if value, ok := ou.mutation.Nullified(); ok { _spec.SetField(operation.FieldNullified, field.TypeBool, value) } if value, ok := ou.mutation.CreatedAt(); ok { _spec.SetField(operation.FieldCreatedAt, field.TypeTime, value) } if n, err = sqlgraph.UpdateNodes(ctx, ou.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{operation.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } ou.mutation.done = true return n, nil } // OperationUpdateOne is the builder for updating a single Operation entity. type OperationUpdateOne struct { config fields []string hooks []Hook mutation *OperationMutation } // SetDid sets the "did" field. func (ouo *OperationUpdateOne) SetDid(s string) *OperationUpdateOne { ouo.mutation.SetDid(s) return ouo } // SetNillableDid sets the "did" field if the given value is not nil. func (ouo *OperationUpdateOne) SetNillableDid(s *string) *OperationUpdateOne { if s != nil { ouo.SetDid(*s) } return ouo } // SetOperation sets the "operation" field. func (ouo *OperationUpdateOne) SetOperation(po plc.PLCOperation) *OperationUpdateOne { ouo.mutation.SetOperation(po) return ouo } // SetNillableOperation sets the "operation" field if the given value is not nil. func (ouo *OperationUpdateOne) SetNillableOperation(po *plc.PLCOperation) *OperationUpdateOne { if po != nil { ouo.SetOperation(*po) } return ouo } // SetCid sets the "cid" field. func (ouo *OperationUpdateOne) SetCid(s string) *OperationUpdateOne { ouo.mutation.SetCid(s) return ouo } // SetNillableCid sets the "cid" field if the given value is not nil. func (ouo *OperationUpdateOne) SetNillableCid(s *string) *OperationUpdateOne { if s != nil { ouo.SetCid(*s) } return ouo } // SetNullified sets the "nullified" field. func (ouo *OperationUpdateOne) SetNullified(b bool) *OperationUpdateOne { ouo.mutation.SetNullified(b) return ouo } // SetNillableNullified sets the "nullified" field if the given value is not nil. func (ouo *OperationUpdateOne) SetNillableNullified(b *bool) *OperationUpdateOne { if b != nil { ouo.SetNullified(*b) } return ouo } // SetCreatedAt sets the "created_at" field. func (ouo *OperationUpdateOne) SetCreatedAt(t time.Time) *OperationUpdateOne { ouo.mutation.SetCreatedAt(t) return ouo } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (ouo *OperationUpdateOne) SetNillableCreatedAt(t *time.Time) *OperationUpdateOne { if t != nil { ouo.SetCreatedAt(*t) } return ouo } // Mutation returns the OperationMutation object of the builder. func (ouo *OperationUpdateOne) Mutation() *OperationMutation { return ouo.mutation } // Where appends a list predicates to the OperationUpdate builder. func (ouo *OperationUpdateOne) Where(ps ...predicate.Operation) *OperationUpdateOne { ouo.mutation.Where(ps...) return ouo } // Select allows selecting one or more fields (columns) of the returned entity. // The default is selecting all fields defined in the entity schema. func (ouo *OperationUpdateOne) Select(field string, fields ...string) *OperationUpdateOne { ouo.fields = append([]string{field}, fields...) return ouo } // Save executes the query and returns the updated Operation entity. func (ouo *OperationUpdateOne) Save(ctx context.Context) (*Operation, error) { return withHooks(ctx, ouo.sqlSave, ouo.mutation, ouo.hooks) } // SaveX is like Save, but panics if an error occurs. func (ouo *OperationUpdateOne) SaveX(ctx context.Context) *Operation { node, err := ouo.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (ouo *OperationUpdateOne) Exec(ctx context.Context) error { _, err := ouo.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (ouo *OperationUpdateOne) ExecX(ctx context.Context) { if err := ouo.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (ouo *OperationUpdateOne) check() error { if v, ok := ouo.mutation.Did(); ok { if err := operation.DidValidator(v); err != nil { return &ValidationError{Name: "did", err: fmt.Errorf(`ent: validator failed for field "Operation.did": %w`, err)} } } if v, ok := ouo.mutation.Cid(); ok { if err := operation.CidValidator(v); err != nil { return &ValidationError{Name: "cid", err: fmt.Errorf(`ent: validator failed for field "Operation.cid": %w`, err)} } } return nil } func (ouo *OperationUpdateOne) sqlSave(ctx context.Context) (_node *Operation, err error) { if err := ouo.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(operation.Table, operation.Columns, sqlgraph.NewFieldSpec(operation.FieldID, field.TypeInt)) id, ok := ouo.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Operation.id" for update`)} } _spec.Node.ID.Value = id if fields := ouo.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, operation.FieldID) for _, f := range fields { if !operation.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != operation.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, f) } } } if ps := ouo.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := ouo.mutation.Did(); ok { _spec.SetField(operation.FieldDid, field.TypeString, value) } if value, ok := ouo.mutation.Operation(); ok { _spec.SetField(operation.FieldOperation, field.TypeJSON, value) } if value, ok := ouo.mutation.Cid(); ok { _spec.SetField(operation.FieldCid, field.TypeString, value) } if value, ok := ouo.mutation.Nullified(); ok { _spec.SetField(operation.FieldNullified, field.TypeBool, value) } if value, ok := ouo.mutation.CreatedAt(); ok { _spec.SetField(operation.FieldCreatedAt, field.TypeTime, value) } _node = &Operation{config: ouo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues if err = sqlgraph.UpdateNode(ctx, ouo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{operation.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } ouo.mutation.done = true return _node, nil }