github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/meta_update.go (about)

     1  // Code generated by ent, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"time"
    10  
    11  	"entgo.io/ent/dialect/sql"
    12  	"entgo.io/ent/dialect/sql/sqlgraph"
    13  	"entgo.io/ent/schema/field"
    14  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
    15  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
    16  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
    17  )
    18  
    19  // MetaUpdate is the builder for updating Meta entities.
    20  type MetaUpdate struct {
    21  	config
    22  	hooks    []Hook
    23  	mutation *MetaMutation
    24  }
    25  
    26  // Where appends a list predicates to the MetaUpdate builder.
    27  func (mu *MetaUpdate) Where(ps ...predicate.Meta) *MetaUpdate {
    28  	mu.mutation.Where(ps...)
    29  	return mu
    30  }
    31  
    32  // SetCreatedAt sets the "created_at" field.
    33  func (mu *MetaUpdate) SetCreatedAt(t time.Time) *MetaUpdate {
    34  	mu.mutation.SetCreatedAt(t)
    35  	return mu
    36  }
    37  
    38  // ClearCreatedAt clears the value of the "created_at" field.
    39  func (mu *MetaUpdate) ClearCreatedAt() *MetaUpdate {
    40  	mu.mutation.ClearCreatedAt()
    41  	return mu
    42  }
    43  
    44  // SetUpdatedAt sets the "updated_at" field.
    45  func (mu *MetaUpdate) SetUpdatedAt(t time.Time) *MetaUpdate {
    46  	mu.mutation.SetUpdatedAt(t)
    47  	return mu
    48  }
    49  
    50  // ClearUpdatedAt clears the value of the "updated_at" field.
    51  func (mu *MetaUpdate) ClearUpdatedAt() *MetaUpdate {
    52  	mu.mutation.ClearUpdatedAt()
    53  	return mu
    54  }
    55  
    56  // SetKey sets the "key" field.
    57  func (mu *MetaUpdate) SetKey(s string) *MetaUpdate {
    58  	mu.mutation.SetKey(s)
    59  	return mu
    60  }
    61  
    62  // SetNillableKey sets the "key" field if the given value is not nil.
    63  func (mu *MetaUpdate) SetNillableKey(s *string) *MetaUpdate {
    64  	if s != nil {
    65  		mu.SetKey(*s)
    66  	}
    67  	return mu
    68  }
    69  
    70  // SetValue sets the "value" field.
    71  func (mu *MetaUpdate) SetValue(s string) *MetaUpdate {
    72  	mu.mutation.SetValue(s)
    73  	return mu
    74  }
    75  
    76  // SetNillableValue sets the "value" field if the given value is not nil.
    77  func (mu *MetaUpdate) SetNillableValue(s *string) *MetaUpdate {
    78  	if s != nil {
    79  		mu.SetValue(*s)
    80  	}
    81  	return mu
    82  }
    83  
    84  // SetAlertMetas sets the "alert_metas" field.
    85  func (mu *MetaUpdate) SetAlertMetas(i int) *MetaUpdate {
    86  	mu.mutation.SetAlertMetas(i)
    87  	return mu
    88  }
    89  
    90  // SetNillableAlertMetas sets the "alert_metas" field if the given value is not nil.
    91  func (mu *MetaUpdate) SetNillableAlertMetas(i *int) *MetaUpdate {
    92  	if i != nil {
    93  		mu.SetAlertMetas(*i)
    94  	}
    95  	return mu
    96  }
    97  
    98  // ClearAlertMetas clears the value of the "alert_metas" field.
    99  func (mu *MetaUpdate) ClearAlertMetas() *MetaUpdate {
   100  	mu.mutation.ClearAlertMetas()
   101  	return mu
   102  }
   103  
   104  // SetOwnerID sets the "owner" edge to the Alert entity by ID.
   105  func (mu *MetaUpdate) SetOwnerID(id int) *MetaUpdate {
   106  	mu.mutation.SetOwnerID(id)
   107  	return mu
   108  }
   109  
   110  // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
   111  func (mu *MetaUpdate) SetNillableOwnerID(id *int) *MetaUpdate {
   112  	if id != nil {
   113  		mu = mu.SetOwnerID(*id)
   114  	}
   115  	return mu
   116  }
   117  
   118  // SetOwner sets the "owner" edge to the Alert entity.
   119  func (mu *MetaUpdate) SetOwner(a *Alert) *MetaUpdate {
   120  	return mu.SetOwnerID(a.ID)
   121  }
   122  
   123  // Mutation returns the MetaMutation object of the builder.
   124  func (mu *MetaUpdate) Mutation() *MetaMutation {
   125  	return mu.mutation
   126  }
   127  
   128  // ClearOwner clears the "owner" edge to the Alert entity.
   129  func (mu *MetaUpdate) ClearOwner() *MetaUpdate {
   130  	mu.mutation.ClearOwner()
   131  	return mu
   132  }
   133  
   134  // Save executes the query and returns the number of nodes affected by the update operation.
   135  func (mu *MetaUpdate) Save(ctx context.Context) (int, error) {
   136  	mu.defaults()
   137  	return withHooks(ctx, mu.sqlSave, mu.mutation, mu.hooks)
   138  }
   139  
   140  // SaveX is like Save, but panics if an error occurs.
   141  func (mu *MetaUpdate) SaveX(ctx context.Context) int {
   142  	affected, err := mu.Save(ctx)
   143  	if err != nil {
   144  		panic(err)
   145  	}
   146  	return affected
   147  }
   148  
   149  // Exec executes the query.
   150  func (mu *MetaUpdate) Exec(ctx context.Context) error {
   151  	_, err := mu.Save(ctx)
   152  	return err
   153  }
   154  
   155  // ExecX is like Exec, but panics if an error occurs.
   156  func (mu *MetaUpdate) ExecX(ctx context.Context) {
   157  	if err := mu.Exec(ctx); err != nil {
   158  		panic(err)
   159  	}
   160  }
   161  
   162  // defaults sets the default values of the builder before save.
   163  func (mu *MetaUpdate) defaults() {
   164  	if _, ok := mu.mutation.CreatedAt(); !ok && !mu.mutation.CreatedAtCleared() {
   165  		v := meta.UpdateDefaultCreatedAt()
   166  		mu.mutation.SetCreatedAt(v)
   167  	}
   168  	if _, ok := mu.mutation.UpdatedAt(); !ok && !mu.mutation.UpdatedAtCleared() {
   169  		v := meta.UpdateDefaultUpdatedAt()
   170  		mu.mutation.SetUpdatedAt(v)
   171  	}
   172  }
   173  
   174  // check runs all checks and user-defined validators on the builder.
   175  func (mu *MetaUpdate) check() error {
   176  	if v, ok := mu.mutation.Value(); ok {
   177  		if err := meta.ValueValidator(v); err != nil {
   178  			return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "Meta.value": %w`, err)}
   179  		}
   180  	}
   181  	return nil
   182  }
   183  
   184  func (mu *MetaUpdate) sqlSave(ctx context.Context) (n int, err error) {
   185  	if err := mu.check(); err != nil {
   186  		return n, err
   187  	}
   188  	_spec := sqlgraph.NewUpdateSpec(meta.Table, meta.Columns, sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt))
   189  	if ps := mu.mutation.predicates; len(ps) > 0 {
   190  		_spec.Predicate = func(selector *sql.Selector) {
   191  			for i := range ps {
   192  				ps[i](selector)
   193  			}
   194  		}
   195  	}
   196  	if value, ok := mu.mutation.CreatedAt(); ok {
   197  		_spec.SetField(meta.FieldCreatedAt, field.TypeTime, value)
   198  	}
   199  	if mu.mutation.CreatedAtCleared() {
   200  		_spec.ClearField(meta.FieldCreatedAt, field.TypeTime)
   201  	}
   202  	if value, ok := mu.mutation.UpdatedAt(); ok {
   203  		_spec.SetField(meta.FieldUpdatedAt, field.TypeTime, value)
   204  	}
   205  	if mu.mutation.UpdatedAtCleared() {
   206  		_spec.ClearField(meta.FieldUpdatedAt, field.TypeTime)
   207  	}
   208  	if value, ok := mu.mutation.Key(); ok {
   209  		_spec.SetField(meta.FieldKey, field.TypeString, value)
   210  	}
   211  	if value, ok := mu.mutation.Value(); ok {
   212  		_spec.SetField(meta.FieldValue, field.TypeString, value)
   213  	}
   214  	if mu.mutation.OwnerCleared() {
   215  		edge := &sqlgraph.EdgeSpec{
   216  			Rel:     sqlgraph.M2O,
   217  			Inverse: true,
   218  			Table:   meta.OwnerTable,
   219  			Columns: []string{meta.OwnerColumn},
   220  			Bidi:    false,
   221  			Target: &sqlgraph.EdgeTarget{
   222  				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
   223  			},
   224  		}
   225  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   226  	}
   227  	if nodes := mu.mutation.OwnerIDs(); len(nodes) > 0 {
   228  		edge := &sqlgraph.EdgeSpec{
   229  			Rel:     sqlgraph.M2O,
   230  			Inverse: true,
   231  			Table:   meta.OwnerTable,
   232  			Columns: []string{meta.OwnerColumn},
   233  			Bidi:    false,
   234  			Target: &sqlgraph.EdgeTarget{
   235  				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
   236  			},
   237  		}
   238  		for _, k := range nodes {
   239  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   240  		}
   241  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   242  	}
   243  	if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
   244  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
   245  			err = &NotFoundError{meta.Label}
   246  		} else if sqlgraph.IsConstraintError(err) {
   247  			err = &ConstraintError{msg: err.Error(), wrap: err}
   248  		}
   249  		return 0, err
   250  	}
   251  	mu.mutation.done = true
   252  	return n, nil
   253  }
   254  
   255  // MetaUpdateOne is the builder for updating a single Meta entity.
   256  type MetaUpdateOne struct {
   257  	config
   258  	fields   []string
   259  	hooks    []Hook
   260  	mutation *MetaMutation
   261  }
   262  
   263  // SetCreatedAt sets the "created_at" field.
   264  func (muo *MetaUpdateOne) SetCreatedAt(t time.Time) *MetaUpdateOne {
   265  	muo.mutation.SetCreatedAt(t)
   266  	return muo
   267  }
   268  
   269  // ClearCreatedAt clears the value of the "created_at" field.
   270  func (muo *MetaUpdateOne) ClearCreatedAt() *MetaUpdateOne {
   271  	muo.mutation.ClearCreatedAt()
   272  	return muo
   273  }
   274  
   275  // SetUpdatedAt sets the "updated_at" field.
   276  func (muo *MetaUpdateOne) SetUpdatedAt(t time.Time) *MetaUpdateOne {
   277  	muo.mutation.SetUpdatedAt(t)
   278  	return muo
   279  }
   280  
   281  // ClearUpdatedAt clears the value of the "updated_at" field.
   282  func (muo *MetaUpdateOne) ClearUpdatedAt() *MetaUpdateOne {
   283  	muo.mutation.ClearUpdatedAt()
   284  	return muo
   285  }
   286  
   287  // SetKey sets the "key" field.
   288  func (muo *MetaUpdateOne) SetKey(s string) *MetaUpdateOne {
   289  	muo.mutation.SetKey(s)
   290  	return muo
   291  }
   292  
   293  // SetNillableKey sets the "key" field if the given value is not nil.
   294  func (muo *MetaUpdateOne) SetNillableKey(s *string) *MetaUpdateOne {
   295  	if s != nil {
   296  		muo.SetKey(*s)
   297  	}
   298  	return muo
   299  }
   300  
   301  // SetValue sets the "value" field.
   302  func (muo *MetaUpdateOne) SetValue(s string) *MetaUpdateOne {
   303  	muo.mutation.SetValue(s)
   304  	return muo
   305  }
   306  
   307  // SetNillableValue sets the "value" field if the given value is not nil.
   308  func (muo *MetaUpdateOne) SetNillableValue(s *string) *MetaUpdateOne {
   309  	if s != nil {
   310  		muo.SetValue(*s)
   311  	}
   312  	return muo
   313  }
   314  
   315  // SetAlertMetas sets the "alert_metas" field.
   316  func (muo *MetaUpdateOne) SetAlertMetas(i int) *MetaUpdateOne {
   317  	muo.mutation.SetAlertMetas(i)
   318  	return muo
   319  }
   320  
   321  // SetNillableAlertMetas sets the "alert_metas" field if the given value is not nil.
   322  func (muo *MetaUpdateOne) SetNillableAlertMetas(i *int) *MetaUpdateOne {
   323  	if i != nil {
   324  		muo.SetAlertMetas(*i)
   325  	}
   326  	return muo
   327  }
   328  
   329  // ClearAlertMetas clears the value of the "alert_metas" field.
   330  func (muo *MetaUpdateOne) ClearAlertMetas() *MetaUpdateOne {
   331  	muo.mutation.ClearAlertMetas()
   332  	return muo
   333  }
   334  
   335  // SetOwnerID sets the "owner" edge to the Alert entity by ID.
   336  func (muo *MetaUpdateOne) SetOwnerID(id int) *MetaUpdateOne {
   337  	muo.mutation.SetOwnerID(id)
   338  	return muo
   339  }
   340  
   341  // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
   342  func (muo *MetaUpdateOne) SetNillableOwnerID(id *int) *MetaUpdateOne {
   343  	if id != nil {
   344  		muo = muo.SetOwnerID(*id)
   345  	}
   346  	return muo
   347  }
   348  
   349  // SetOwner sets the "owner" edge to the Alert entity.
   350  func (muo *MetaUpdateOne) SetOwner(a *Alert) *MetaUpdateOne {
   351  	return muo.SetOwnerID(a.ID)
   352  }
   353  
   354  // Mutation returns the MetaMutation object of the builder.
   355  func (muo *MetaUpdateOne) Mutation() *MetaMutation {
   356  	return muo.mutation
   357  }
   358  
   359  // ClearOwner clears the "owner" edge to the Alert entity.
   360  func (muo *MetaUpdateOne) ClearOwner() *MetaUpdateOne {
   361  	muo.mutation.ClearOwner()
   362  	return muo
   363  }
   364  
   365  // Where appends a list predicates to the MetaUpdate builder.
   366  func (muo *MetaUpdateOne) Where(ps ...predicate.Meta) *MetaUpdateOne {
   367  	muo.mutation.Where(ps...)
   368  	return muo
   369  }
   370  
   371  // Select allows selecting one or more fields (columns) of the returned entity.
   372  // The default is selecting all fields defined in the entity schema.
   373  func (muo *MetaUpdateOne) Select(field string, fields ...string) *MetaUpdateOne {
   374  	muo.fields = append([]string{field}, fields...)
   375  	return muo
   376  }
   377  
   378  // Save executes the query and returns the updated Meta entity.
   379  func (muo *MetaUpdateOne) Save(ctx context.Context) (*Meta, error) {
   380  	muo.defaults()
   381  	return withHooks(ctx, muo.sqlSave, muo.mutation, muo.hooks)
   382  }
   383  
   384  // SaveX is like Save, but panics if an error occurs.
   385  func (muo *MetaUpdateOne) SaveX(ctx context.Context) *Meta {
   386  	node, err := muo.Save(ctx)
   387  	if err != nil {
   388  		panic(err)
   389  	}
   390  	return node
   391  }
   392  
   393  // Exec executes the query on the entity.
   394  func (muo *MetaUpdateOne) Exec(ctx context.Context) error {
   395  	_, err := muo.Save(ctx)
   396  	return err
   397  }
   398  
   399  // ExecX is like Exec, but panics if an error occurs.
   400  func (muo *MetaUpdateOne) ExecX(ctx context.Context) {
   401  	if err := muo.Exec(ctx); err != nil {
   402  		panic(err)
   403  	}
   404  }
   405  
   406  // defaults sets the default values of the builder before save.
   407  func (muo *MetaUpdateOne) defaults() {
   408  	if _, ok := muo.mutation.CreatedAt(); !ok && !muo.mutation.CreatedAtCleared() {
   409  		v := meta.UpdateDefaultCreatedAt()
   410  		muo.mutation.SetCreatedAt(v)
   411  	}
   412  	if _, ok := muo.mutation.UpdatedAt(); !ok && !muo.mutation.UpdatedAtCleared() {
   413  		v := meta.UpdateDefaultUpdatedAt()
   414  		muo.mutation.SetUpdatedAt(v)
   415  	}
   416  }
   417  
   418  // check runs all checks and user-defined validators on the builder.
   419  func (muo *MetaUpdateOne) check() error {
   420  	if v, ok := muo.mutation.Value(); ok {
   421  		if err := meta.ValueValidator(v); err != nil {
   422  			return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "Meta.value": %w`, err)}
   423  		}
   424  	}
   425  	return nil
   426  }
   427  
   428  func (muo *MetaUpdateOne) sqlSave(ctx context.Context) (_node *Meta, err error) {
   429  	if err := muo.check(); err != nil {
   430  		return _node, err
   431  	}
   432  	_spec := sqlgraph.NewUpdateSpec(meta.Table, meta.Columns, sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt))
   433  	id, ok := muo.mutation.ID()
   434  	if !ok {
   435  		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Meta.id" for update`)}
   436  	}
   437  	_spec.Node.ID.Value = id
   438  	if fields := muo.fields; len(fields) > 0 {
   439  		_spec.Node.Columns = make([]string, 0, len(fields))
   440  		_spec.Node.Columns = append(_spec.Node.Columns, meta.FieldID)
   441  		for _, f := range fields {
   442  			if !meta.ValidColumn(f) {
   443  				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
   444  			}
   445  			if f != meta.FieldID {
   446  				_spec.Node.Columns = append(_spec.Node.Columns, f)
   447  			}
   448  		}
   449  	}
   450  	if ps := muo.mutation.predicates; len(ps) > 0 {
   451  		_spec.Predicate = func(selector *sql.Selector) {
   452  			for i := range ps {
   453  				ps[i](selector)
   454  			}
   455  		}
   456  	}
   457  	if value, ok := muo.mutation.CreatedAt(); ok {
   458  		_spec.SetField(meta.FieldCreatedAt, field.TypeTime, value)
   459  	}
   460  	if muo.mutation.CreatedAtCleared() {
   461  		_spec.ClearField(meta.FieldCreatedAt, field.TypeTime)
   462  	}
   463  	if value, ok := muo.mutation.UpdatedAt(); ok {
   464  		_spec.SetField(meta.FieldUpdatedAt, field.TypeTime, value)
   465  	}
   466  	if muo.mutation.UpdatedAtCleared() {
   467  		_spec.ClearField(meta.FieldUpdatedAt, field.TypeTime)
   468  	}
   469  	if value, ok := muo.mutation.Key(); ok {
   470  		_spec.SetField(meta.FieldKey, field.TypeString, value)
   471  	}
   472  	if value, ok := muo.mutation.Value(); ok {
   473  		_spec.SetField(meta.FieldValue, field.TypeString, value)
   474  	}
   475  	if muo.mutation.OwnerCleared() {
   476  		edge := &sqlgraph.EdgeSpec{
   477  			Rel:     sqlgraph.M2O,
   478  			Inverse: true,
   479  			Table:   meta.OwnerTable,
   480  			Columns: []string{meta.OwnerColumn},
   481  			Bidi:    false,
   482  			Target: &sqlgraph.EdgeTarget{
   483  				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
   484  			},
   485  		}
   486  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   487  	}
   488  	if nodes := muo.mutation.OwnerIDs(); len(nodes) > 0 {
   489  		edge := &sqlgraph.EdgeSpec{
   490  			Rel:     sqlgraph.M2O,
   491  			Inverse: true,
   492  			Table:   meta.OwnerTable,
   493  			Columns: []string{meta.OwnerColumn},
   494  			Bidi:    false,
   495  			Target: &sqlgraph.EdgeTarget{
   496  				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
   497  			},
   498  		}
   499  		for _, k := range nodes {
   500  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   501  		}
   502  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   503  	}
   504  	_node = &Meta{config: muo.config}
   505  	_spec.Assign = _node.assignValues
   506  	_spec.ScanValues = _node.scanValues
   507  	if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
   508  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
   509  			err = &NotFoundError{meta.Label}
   510  		} else if sqlgraph.IsConstraintError(err) {
   511  			err = &ConstraintError{msg: err.Error(), wrap: err}
   512  		}
   513  		return nil, err
   514  	}
   515  	muo.mutation.done = true
   516  	return _node, nil
   517  }