github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/event_create.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/sqlgraph"
    12  	"entgo.io/ent/schema/field"
    13  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
    14  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/event"
    15  )
    16  
    17  // EventCreate is the builder for creating a Event entity.
    18  type EventCreate struct {
    19  	config
    20  	mutation *EventMutation
    21  	hooks    []Hook
    22  }
    23  
    24  // SetCreatedAt sets the "created_at" field.
    25  func (ec *EventCreate) SetCreatedAt(t time.Time) *EventCreate {
    26  	ec.mutation.SetCreatedAt(t)
    27  	return ec
    28  }
    29  
    30  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    31  func (ec *EventCreate) SetNillableCreatedAt(t *time.Time) *EventCreate {
    32  	if t != nil {
    33  		ec.SetCreatedAt(*t)
    34  	}
    35  	return ec
    36  }
    37  
    38  // SetUpdatedAt sets the "updated_at" field.
    39  func (ec *EventCreate) SetUpdatedAt(t time.Time) *EventCreate {
    40  	ec.mutation.SetUpdatedAt(t)
    41  	return ec
    42  }
    43  
    44  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    45  func (ec *EventCreate) SetNillableUpdatedAt(t *time.Time) *EventCreate {
    46  	if t != nil {
    47  		ec.SetUpdatedAt(*t)
    48  	}
    49  	return ec
    50  }
    51  
    52  // SetTime sets the "time" field.
    53  func (ec *EventCreate) SetTime(t time.Time) *EventCreate {
    54  	ec.mutation.SetTime(t)
    55  	return ec
    56  }
    57  
    58  // SetSerialized sets the "serialized" field.
    59  func (ec *EventCreate) SetSerialized(s string) *EventCreate {
    60  	ec.mutation.SetSerialized(s)
    61  	return ec
    62  }
    63  
    64  // SetAlertEvents sets the "alert_events" field.
    65  func (ec *EventCreate) SetAlertEvents(i int) *EventCreate {
    66  	ec.mutation.SetAlertEvents(i)
    67  	return ec
    68  }
    69  
    70  // SetNillableAlertEvents sets the "alert_events" field if the given value is not nil.
    71  func (ec *EventCreate) SetNillableAlertEvents(i *int) *EventCreate {
    72  	if i != nil {
    73  		ec.SetAlertEvents(*i)
    74  	}
    75  	return ec
    76  }
    77  
    78  // SetOwnerID sets the "owner" edge to the Alert entity by ID.
    79  func (ec *EventCreate) SetOwnerID(id int) *EventCreate {
    80  	ec.mutation.SetOwnerID(id)
    81  	return ec
    82  }
    83  
    84  // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
    85  func (ec *EventCreate) SetNillableOwnerID(id *int) *EventCreate {
    86  	if id != nil {
    87  		ec = ec.SetOwnerID(*id)
    88  	}
    89  	return ec
    90  }
    91  
    92  // SetOwner sets the "owner" edge to the Alert entity.
    93  func (ec *EventCreate) SetOwner(a *Alert) *EventCreate {
    94  	return ec.SetOwnerID(a.ID)
    95  }
    96  
    97  // Mutation returns the EventMutation object of the builder.
    98  func (ec *EventCreate) Mutation() *EventMutation {
    99  	return ec.mutation
   100  }
   101  
   102  // Save creates the Event in the database.
   103  func (ec *EventCreate) Save(ctx context.Context) (*Event, error) {
   104  	ec.defaults()
   105  	return withHooks(ctx, ec.sqlSave, ec.mutation, ec.hooks)
   106  }
   107  
   108  // SaveX calls Save and panics if Save returns an error.
   109  func (ec *EventCreate) SaveX(ctx context.Context) *Event {
   110  	v, err := ec.Save(ctx)
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  	return v
   115  }
   116  
   117  // Exec executes the query.
   118  func (ec *EventCreate) Exec(ctx context.Context) error {
   119  	_, err := ec.Save(ctx)
   120  	return err
   121  }
   122  
   123  // ExecX is like Exec, but panics if an error occurs.
   124  func (ec *EventCreate) ExecX(ctx context.Context) {
   125  	if err := ec.Exec(ctx); err != nil {
   126  		panic(err)
   127  	}
   128  }
   129  
   130  // defaults sets the default values of the builder before save.
   131  func (ec *EventCreate) defaults() {
   132  	if _, ok := ec.mutation.CreatedAt(); !ok {
   133  		v := event.DefaultCreatedAt()
   134  		ec.mutation.SetCreatedAt(v)
   135  	}
   136  	if _, ok := ec.mutation.UpdatedAt(); !ok {
   137  		v := event.DefaultUpdatedAt()
   138  		ec.mutation.SetUpdatedAt(v)
   139  	}
   140  }
   141  
   142  // check runs all checks and user-defined validators on the builder.
   143  func (ec *EventCreate) check() error {
   144  	if _, ok := ec.mutation.Time(); !ok {
   145  		return &ValidationError{Name: "time", err: errors.New(`ent: missing required field "Event.time"`)}
   146  	}
   147  	if _, ok := ec.mutation.Serialized(); !ok {
   148  		return &ValidationError{Name: "serialized", err: errors.New(`ent: missing required field "Event.serialized"`)}
   149  	}
   150  	if v, ok := ec.mutation.Serialized(); ok {
   151  		if err := event.SerializedValidator(v); err != nil {
   152  			return &ValidationError{Name: "serialized", err: fmt.Errorf(`ent: validator failed for field "Event.serialized": %w`, err)}
   153  		}
   154  	}
   155  	return nil
   156  }
   157  
   158  func (ec *EventCreate) sqlSave(ctx context.Context) (*Event, error) {
   159  	if err := ec.check(); err != nil {
   160  		return nil, err
   161  	}
   162  	_node, _spec := ec.createSpec()
   163  	if err := sqlgraph.CreateNode(ctx, ec.driver, _spec); err != nil {
   164  		if sqlgraph.IsConstraintError(err) {
   165  			err = &ConstraintError{msg: err.Error(), wrap: err}
   166  		}
   167  		return nil, err
   168  	}
   169  	id := _spec.ID.Value.(int64)
   170  	_node.ID = int(id)
   171  	ec.mutation.id = &_node.ID
   172  	ec.mutation.done = true
   173  	return _node, nil
   174  }
   175  
   176  func (ec *EventCreate) createSpec() (*Event, *sqlgraph.CreateSpec) {
   177  	var (
   178  		_node = &Event{config: ec.config}
   179  		_spec = sqlgraph.NewCreateSpec(event.Table, sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt))
   180  	)
   181  	if value, ok := ec.mutation.CreatedAt(); ok {
   182  		_spec.SetField(event.FieldCreatedAt, field.TypeTime, value)
   183  		_node.CreatedAt = &value
   184  	}
   185  	if value, ok := ec.mutation.UpdatedAt(); ok {
   186  		_spec.SetField(event.FieldUpdatedAt, field.TypeTime, value)
   187  		_node.UpdatedAt = &value
   188  	}
   189  	if value, ok := ec.mutation.Time(); ok {
   190  		_spec.SetField(event.FieldTime, field.TypeTime, value)
   191  		_node.Time = value
   192  	}
   193  	if value, ok := ec.mutation.Serialized(); ok {
   194  		_spec.SetField(event.FieldSerialized, field.TypeString, value)
   195  		_node.Serialized = value
   196  	}
   197  	if nodes := ec.mutation.OwnerIDs(); len(nodes) > 0 {
   198  		edge := &sqlgraph.EdgeSpec{
   199  			Rel:     sqlgraph.M2O,
   200  			Inverse: true,
   201  			Table:   event.OwnerTable,
   202  			Columns: []string{event.OwnerColumn},
   203  			Bidi:    false,
   204  			Target: &sqlgraph.EdgeTarget{
   205  				IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
   206  			},
   207  		}
   208  		for _, k := range nodes {
   209  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   210  		}
   211  		_node.AlertEvents = nodes[0]
   212  		_spec.Edges = append(_spec.Edges, edge)
   213  	}
   214  	return _node, _spec
   215  }
   216  
   217  // EventCreateBulk is the builder for creating many Event entities in bulk.
   218  type EventCreateBulk struct {
   219  	config
   220  	err      error
   221  	builders []*EventCreate
   222  }
   223  
   224  // Save creates the Event entities in the database.
   225  func (ecb *EventCreateBulk) Save(ctx context.Context) ([]*Event, error) {
   226  	if ecb.err != nil {
   227  		return nil, ecb.err
   228  	}
   229  	specs := make([]*sqlgraph.CreateSpec, len(ecb.builders))
   230  	nodes := make([]*Event, len(ecb.builders))
   231  	mutators := make([]Mutator, len(ecb.builders))
   232  	for i := range ecb.builders {
   233  		func(i int, root context.Context) {
   234  			builder := ecb.builders[i]
   235  			builder.defaults()
   236  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   237  				mutation, ok := m.(*EventMutation)
   238  				if !ok {
   239  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   240  				}
   241  				if err := builder.check(); err != nil {
   242  					return nil, err
   243  				}
   244  				builder.mutation = mutation
   245  				var err error
   246  				nodes[i], specs[i] = builder.createSpec()
   247  				if i < len(mutators)-1 {
   248  					_, err = mutators[i+1].Mutate(root, ecb.builders[i+1].mutation)
   249  				} else {
   250  					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
   251  					// Invoke the actual operation on the latest mutation in the chain.
   252  					if err = sqlgraph.BatchCreate(ctx, ecb.driver, spec); err != nil {
   253  						if sqlgraph.IsConstraintError(err) {
   254  							err = &ConstraintError{msg: err.Error(), wrap: err}
   255  						}
   256  					}
   257  				}
   258  				if err != nil {
   259  					return nil, err
   260  				}
   261  				mutation.id = &nodes[i].ID
   262  				if specs[i].ID.Value != nil {
   263  					id := specs[i].ID.Value.(int64)
   264  					nodes[i].ID = int(id)
   265  				}
   266  				mutation.done = true
   267  				return nodes[i], nil
   268  			})
   269  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   270  				mut = builder.hooks[i](mut)
   271  			}
   272  			mutators[i] = mut
   273  		}(i, ctx)
   274  	}
   275  	if len(mutators) > 0 {
   276  		if _, err := mutators[0].Mutate(ctx, ecb.builders[0].mutation); err != nil {
   277  			return nil, err
   278  		}
   279  	}
   280  	return nodes, nil
   281  }
   282  
   283  // SaveX is like Save, but panics if an error occurs.
   284  func (ecb *EventCreateBulk) SaveX(ctx context.Context) []*Event {
   285  	v, err := ecb.Save(ctx)
   286  	if err != nil {
   287  		panic(err)
   288  	}
   289  	return v
   290  }
   291  
   292  // Exec executes the query.
   293  func (ecb *EventCreateBulk) Exec(ctx context.Context) error {
   294  	_, err := ecb.Save(ctx)
   295  	return err
   296  }
   297  
   298  // ExecX is like Exec, but panics if an error occurs.
   299  func (ecb *EventCreateBulk) ExecX(ctx context.Context) {
   300  	if err := ecb.Exec(ctx); err != nil {
   301  		panic(err)
   302  	}
   303  }