bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/event_update.go (about)

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