rano/db/ent/audit_update.go

621 lines
18 KiB
Go
Raw Permalink Normal View History

2024-11-10 09:22:33 +00:00
// 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"
"gitserver.in/patialtech/rano/db/ent/audit"
"gitserver.in/patialtech/rano/db/ent/predicate"
"gitserver.in/patialtech/rano/db/ent/user"
)
// AuditUpdate is the builder for updating Audit entities.
type AuditUpdate struct {
config
hooks []Hook
mutation *AuditMutation
}
// Where appends a list predicates to the AuditUpdate builder.
func (au *AuditUpdate) Where(ps ...predicate.Audit) *AuditUpdate {
au.mutation.Where(ps...)
return au
}
// SetEntName sets the "ent_name" field.
func (au *AuditUpdate) SetEntName(s string) *AuditUpdate {
au.mutation.SetEntName(s)
return au
}
// SetNillableEntName sets the "ent_name" field if the given value is not nil.
func (au *AuditUpdate) SetNillableEntName(s *string) *AuditUpdate {
if s != nil {
au.SetEntName(*s)
}
return au
}
// SetEntID sets the "ent_id" field.
func (au *AuditUpdate) SetEntID(i int64) *AuditUpdate {
au.mutation.ResetEntID()
au.mutation.SetEntID(i)
return au
}
// SetNillableEntID sets the "ent_id" field if the given value is not nil.
func (au *AuditUpdate) SetNillableEntID(i *int64) *AuditUpdate {
if i != nil {
au.SetEntID(*i)
}
return au
}
// AddEntID adds i to the "ent_id" field.
func (au *AuditUpdate) AddEntID(i int64) *AuditUpdate {
au.mutation.AddEntID(i)
return au
}
// SetOperation sets the "operation" field.
func (au *AuditUpdate) SetOperation(a audit.Operation) *AuditUpdate {
au.mutation.SetOperation(a)
return au
}
// SetNillableOperation sets the "operation" field if the given value is not nil.
func (au *AuditUpdate) SetNillableOperation(a *audit.Operation) *AuditUpdate {
if a != nil {
au.SetOperation(*a)
}
return au
}
// SetDescription sets the "description" field.
func (au *AuditUpdate) SetDescription(s string) *AuditUpdate {
au.mutation.SetDescription(s)
return au
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (au *AuditUpdate) SetNillableDescription(s *string) *AuditUpdate {
if s != nil {
au.SetDescription(*s)
}
return au
}
// SetIP sets the "ip" field.
func (au *AuditUpdate) SetIP(s string) *AuditUpdate {
au.mutation.SetIP(s)
return au
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (au *AuditUpdate) SetNillableIP(s *string) *AuditUpdate {
if s != nil {
au.SetIP(*s)
}
return au
}
// ClearIP clears the value of the "ip" field.
func (au *AuditUpdate) ClearIP() *AuditUpdate {
au.mutation.ClearIP()
return au
}
// SetUserName sets the "user_name" field.
func (au *AuditUpdate) SetUserName(s string) *AuditUpdate {
au.mutation.SetUserName(s)
return au
}
// SetNillableUserName sets the "user_name" field if the given value is not nil.
func (au *AuditUpdate) SetNillableUserName(s *string) *AuditUpdate {
if s != nil {
au.SetUserName(*s)
}
return au
}
// ClearUserName clears the value of the "user_name" field.
func (au *AuditUpdate) ClearUserName() *AuditUpdate {
au.mutation.ClearUserName()
return au
}
// SetUserID sets the "user" edge to the User entity by ID.
func (au *AuditUpdate) SetUserID(id int64) *AuditUpdate {
au.mutation.SetUserID(id)
return au
}
// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (au *AuditUpdate) SetNillableUserID(id *int64) *AuditUpdate {
if id != nil {
au = au.SetUserID(*id)
}
return au
}
// SetUser sets the "user" edge to the User entity.
func (au *AuditUpdate) SetUser(u *User) *AuditUpdate {
return au.SetUserID(u.ID)
}
// Mutation returns the AuditMutation object of the builder.
func (au *AuditUpdate) Mutation() *AuditMutation {
return au.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (au *AuditUpdate) ClearUser() *AuditUpdate {
au.mutation.ClearUser()
return au
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (au *AuditUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, au.sqlSave, au.mutation, au.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (au *AuditUpdate) SaveX(ctx context.Context) int {
affected, err := au.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (au *AuditUpdate) Exec(ctx context.Context) error {
_, err := au.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (au *AuditUpdate) ExecX(ctx context.Context) {
if err := au.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (au *AuditUpdate) check() error {
if v, ok := au.mutation.EntName(); ok {
if err := audit.EntNameValidator(v); err != nil {
return &ValidationError{Name: "ent_name", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_name": %w`, err)}
}
}
if v, ok := au.mutation.EntID(); ok {
if err := audit.EntIDValidator(v); err != nil {
return &ValidationError{Name: "ent_id", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_id": %w`, err)}
}
}
if v, ok := au.mutation.Operation(); ok {
if err := audit.OperationValidator(v); err != nil {
return &ValidationError{Name: "operation", err: fmt.Errorf(`ent: validator failed for field "Audit.operation": %w`, err)}
}
}
if v, ok := au.mutation.Description(); ok {
if err := audit.DescriptionValidator(v); err != nil {
return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "Audit.description": %w`, err)}
}
}
if v, ok := au.mutation.IP(); ok {
if err := audit.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "Audit.ip": %w`, err)}
}
}
if v, ok := au.mutation.UserName(); ok {
if err := audit.UserNameValidator(v); err != nil {
return &ValidationError{Name: "user_name", err: fmt.Errorf(`ent: validator failed for field "Audit.user_name": %w`, err)}
}
}
return nil
}
func (au *AuditUpdate) sqlSave(ctx context.Context) (n int, err error) {
if err := au.check(); err != nil {
return n, err
}
_spec := sqlgraph.NewUpdateSpec(audit.Table, audit.Columns, sqlgraph.NewFieldSpec(audit.FieldID, field.TypeInt64))
if ps := au.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := au.mutation.EntName(); ok {
_spec.SetField(audit.FieldEntName, field.TypeString, value)
}
if value, ok := au.mutation.EntID(); ok {
_spec.SetField(audit.FieldEntID, field.TypeInt64, value)
}
if value, ok := au.mutation.AddedEntID(); ok {
_spec.AddField(audit.FieldEntID, field.TypeInt64, value)
}
if value, ok := au.mutation.Operation(); ok {
_spec.SetField(audit.FieldOperation, field.TypeEnum, value)
}
if value, ok := au.mutation.Description(); ok {
_spec.SetField(audit.FieldDescription, field.TypeString, value)
}
if value, ok := au.mutation.IP(); ok {
_spec.SetField(audit.FieldIP, field.TypeString, value)
}
if au.mutation.IPCleared() {
_spec.ClearField(audit.FieldIP, field.TypeString)
}
if value, ok := au.mutation.UserName(); ok {
_spec.SetField(audit.FieldUserName, field.TypeString, value)
}
if au.mutation.UserNameCleared() {
_spec.ClearField(audit.FieldUserName, field.TypeString)
}
if au.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: audit.UserTable,
Columns: []string{audit.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := au.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: audit.UserTable,
Columns: []string{audit.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
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, au.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{audit.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
au.mutation.done = true
return n, nil
}
// AuditUpdateOne is the builder for updating a single Audit entity.
type AuditUpdateOne struct {
config
fields []string
hooks []Hook
mutation *AuditMutation
}
// SetEntName sets the "ent_name" field.
func (auo *AuditUpdateOne) SetEntName(s string) *AuditUpdateOne {
auo.mutation.SetEntName(s)
return auo
}
// SetNillableEntName sets the "ent_name" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableEntName(s *string) *AuditUpdateOne {
if s != nil {
auo.SetEntName(*s)
}
return auo
}
// SetEntID sets the "ent_id" field.
func (auo *AuditUpdateOne) SetEntID(i int64) *AuditUpdateOne {
auo.mutation.ResetEntID()
auo.mutation.SetEntID(i)
return auo
}
// SetNillableEntID sets the "ent_id" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableEntID(i *int64) *AuditUpdateOne {
if i != nil {
auo.SetEntID(*i)
}
return auo
}
// AddEntID adds i to the "ent_id" field.
func (auo *AuditUpdateOne) AddEntID(i int64) *AuditUpdateOne {
auo.mutation.AddEntID(i)
return auo
}
// SetOperation sets the "operation" field.
func (auo *AuditUpdateOne) SetOperation(a audit.Operation) *AuditUpdateOne {
auo.mutation.SetOperation(a)
return auo
}
// SetNillableOperation sets the "operation" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableOperation(a *audit.Operation) *AuditUpdateOne {
if a != nil {
auo.SetOperation(*a)
}
return auo
}
// SetDescription sets the "description" field.
func (auo *AuditUpdateOne) SetDescription(s string) *AuditUpdateOne {
auo.mutation.SetDescription(s)
return auo
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableDescription(s *string) *AuditUpdateOne {
if s != nil {
auo.SetDescription(*s)
}
return auo
}
// SetIP sets the "ip" field.
func (auo *AuditUpdateOne) SetIP(s string) *AuditUpdateOne {
auo.mutation.SetIP(s)
return auo
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableIP(s *string) *AuditUpdateOne {
if s != nil {
auo.SetIP(*s)
}
return auo
}
// ClearIP clears the value of the "ip" field.
func (auo *AuditUpdateOne) ClearIP() *AuditUpdateOne {
auo.mutation.ClearIP()
return auo
}
// SetUserName sets the "user_name" field.
func (auo *AuditUpdateOne) SetUserName(s string) *AuditUpdateOne {
auo.mutation.SetUserName(s)
return auo
}
// SetNillableUserName sets the "user_name" field if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableUserName(s *string) *AuditUpdateOne {
if s != nil {
auo.SetUserName(*s)
}
return auo
}
// ClearUserName clears the value of the "user_name" field.
func (auo *AuditUpdateOne) ClearUserName() *AuditUpdateOne {
auo.mutation.ClearUserName()
return auo
}
// SetUserID sets the "user" edge to the User entity by ID.
func (auo *AuditUpdateOne) SetUserID(id int64) *AuditUpdateOne {
auo.mutation.SetUserID(id)
return auo
}
// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (auo *AuditUpdateOne) SetNillableUserID(id *int64) *AuditUpdateOne {
if id != nil {
auo = auo.SetUserID(*id)
}
return auo
}
// SetUser sets the "user" edge to the User entity.
func (auo *AuditUpdateOne) SetUser(u *User) *AuditUpdateOne {
return auo.SetUserID(u.ID)
}
// Mutation returns the AuditMutation object of the builder.
func (auo *AuditUpdateOne) Mutation() *AuditMutation {
return auo.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (auo *AuditUpdateOne) ClearUser() *AuditUpdateOne {
auo.mutation.ClearUser()
return auo
}
// Where appends a list predicates to the AuditUpdate builder.
func (auo *AuditUpdateOne) Where(ps ...predicate.Audit) *AuditUpdateOne {
auo.mutation.Where(ps...)
return auo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (auo *AuditUpdateOne) Select(field string, fields ...string) *AuditUpdateOne {
auo.fields = append([]string{field}, fields...)
return auo
}
// Save executes the query and returns the updated Audit entity.
func (auo *AuditUpdateOne) Save(ctx context.Context) (*Audit, error) {
return withHooks(ctx, auo.sqlSave, auo.mutation, auo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (auo *AuditUpdateOne) SaveX(ctx context.Context) *Audit {
node, err := auo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (auo *AuditUpdateOne) Exec(ctx context.Context) error {
_, err := auo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (auo *AuditUpdateOne) ExecX(ctx context.Context) {
if err := auo.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (auo *AuditUpdateOne) check() error {
if v, ok := auo.mutation.EntName(); ok {
if err := audit.EntNameValidator(v); err != nil {
return &ValidationError{Name: "ent_name", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_name": %w`, err)}
}
}
if v, ok := auo.mutation.EntID(); ok {
if err := audit.EntIDValidator(v); err != nil {
return &ValidationError{Name: "ent_id", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_id": %w`, err)}
}
}
if v, ok := auo.mutation.Operation(); ok {
if err := audit.OperationValidator(v); err != nil {
return &ValidationError{Name: "operation", err: fmt.Errorf(`ent: validator failed for field "Audit.operation": %w`, err)}
}
}
if v, ok := auo.mutation.Description(); ok {
if err := audit.DescriptionValidator(v); err != nil {
return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "Audit.description": %w`, err)}
}
}
if v, ok := auo.mutation.IP(); ok {
if err := audit.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "Audit.ip": %w`, err)}
}
}
if v, ok := auo.mutation.UserName(); ok {
if err := audit.UserNameValidator(v); err != nil {
return &ValidationError{Name: "user_name", err: fmt.Errorf(`ent: validator failed for field "Audit.user_name": %w`, err)}
}
}
return nil
}
func (auo *AuditUpdateOne) sqlSave(ctx context.Context) (_node *Audit, err error) {
if err := auo.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(audit.Table, audit.Columns, sqlgraph.NewFieldSpec(audit.FieldID, field.TypeInt64))
id, ok := auo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Audit.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := auo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, audit.FieldID)
for _, f := range fields {
if !audit.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != audit.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := auo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := auo.mutation.EntName(); ok {
_spec.SetField(audit.FieldEntName, field.TypeString, value)
}
if value, ok := auo.mutation.EntID(); ok {
_spec.SetField(audit.FieldEntID, field.TypeInt64, value)
}
if value, ok := auo.mutation.AddedEntID(); ok {
_spec.AddField(audit.FieldEntID, field.TypeInt64, value)
}
if value, ok := auo.mutation.Operation(); ok {
_spec.SetField(audit.FieldOperation, field.TypeEnum, value)
}
if value, ok := auo.mutation.Description(); ok {
_spec.SetField(audit.FieldDescription, field.TypeString, value)
}
if value, ok := auo.mutation.IP(); ok {
_spec.SetField(audit.FieldIP, field.TypeString, value)
}
if auo.mutation.IPCleared() {
_spec.ClearField(audit.FieldIP, field.TypeString)
}
if value, ok := auo.mutation.UserName(); ok {
_spec.SetField(audit.FieldUserName, field.TypeString, value)
}
if auo.mutation.UserNameCleared() {
_spec.ClearField(audit.FieldUserName, field.TypeString)
}
if auo.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: audit.UserTable,
Columns: []string{audit.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := auo.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: audit.UserTable,
Columns: []string{audit.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &Audit{config: auo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, auo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{audit.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
auo.mutation.done = true
return _node, nil
}