crowdsec/pkg/database/ent/event_create.go
2024-02-14 11:19:13 +01:00

303 lines
8.1 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/event"
)
// EventCreate is the builder for creating a Event entity.
type EventCreate struct {
config
mutation *EventMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (ec *EventCreate) SetCreatedAt(t time.Time) *EventCreate {
ec.mutation.SetCreatedAt(t)
return ec
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (ec *EventCreate) SetNillableCreatedAt(t *time.Time) *EventCreate {
if t != nil {
ec.SetCreatedAt(*t)
}
return ec
}
// SetUpdatedAt sets the "updated_at" field.
func (ec *EventCreate) SetUpdatedAt(t time.Time) *EventCreate {
ec.mutation.SetUpdatedAt(t)
return ec
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (ec *EventCreate) SetNillableUpdatedAt(t *time.Time) *EventCreate {
if t != nil {
ec.SetUpdatedAt(*t)
}
return ec
}
// SetTime sets the "time" field.
func (ec *EventCreate) SetTime(t time.Time) *EventCreate {
ec.mutation.SetTime(t)
return ec
}
// SetSerialized sets the "serialized" field.
func (ec *EventCreate) SetSerialized(s string) *EventCreate {
ec.mutation.SetSerialized(s)
return ec
}
// SetAlertEvents sets the "alert_events" field.
func (ec *EventCreate) SetAlertEvents(i int) *EventCreate {
ec.mutation.SetAlertEvents(i)
return ec
}
// SetNillableAlertEvents sets the "alert_events" field if the given value is not nil.
func (ec *EventCreate) SetNillableAlertEvents(i *int) *EventCreate {
if i != nil {
ec.SetAlertEvents(*i)
}
return ec
}
// SetOwnerID sets the "owner" edge to the Alert entity by ID.
func (ec *EventCreate) SetOwnerID(id int) *EventCreate {
ec.mutation.SetOwnerID(id)
return ec
}
// SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
func (ec *EventCreate) SetNillableOwnerID(id *int) *EventCreate {
if id != nil {
ec = ec.SetOwnerID(*id)
}
return ec
}
// SetOwner sets the "owner" edge to the Alert entity.
func (ec *EventCreate) SetOwner(a *Alert) *EventCreate {
return ec.SetOwnerID(a.ID)
}
// Mutation returns the EventMutation object of the builder.
func (ec *EventCreate) Mutation() *EventMutation {
return ec.mutation
}
// Save creates the Event in the database.
func (ec *EventCreate) Save(ctx context.Context) (*Event, error) {
ec.defaults()
return withHooks(ctx, ec.sqlSave, ec.mutation, ec.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (ec *EventCreate) SaveX(ctx context.Context) *Event {
v, err := ec.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ec *EventCreate) Exec(ctx context.Context) error {
_, err := ec.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ec *EventCreate) ExecX(ctx context.Context) {
if err := ec.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (ec *EventCreate) defaults() {
if _, ok := ec.mutation.CreatedAt(); !ok {
v := event.DefaultCreatedAt()
ec.mutation.SetCreatedAt(v)
}
if _, ok := ec.mutation.UpdatedAt(); !ok {
v := event.DefaultUpdatedAt()
ec.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (ec *EventCreate) check() error {
if _, ok := ec.mutation.Time(); !ok {
return &ValidationError{Name: "time", err: errors.New(`ent: missing required field "Event.time"`)}
}
if _, ok := ec.mutation.Serialized(); !ok {
return &ValidationError{Name: "serialized", err: errors.New(`ent: missing required field "Event.serialized"`)}
}
if v, ok := ec.mutation.Serialized(); ok {
if err := event.SerializedValidator(v); err != nil {
return &ValidationError{Name: "serialized", err: fmt.Errorf(`ent: validator failed for field "Event.serialized": %w`, err)}
}
}
return nil
}
func (ec *EventCreate) sqlSave(ctx context.Context) (*Event, error) {
if err := ec.check(); err != nil {
return nil, err
}
_node, _spec := ec.createSpec()
if err := sqlgraph.CreateNode(ctx, ec.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
ec.mutation.id = &_node.ID
ec.mutation.done = true
return _node, nil
}
func (ec *EventCreate) createSpec() (*Event, *sqlgraph.CreateSpec) {
var (
_node = &Event{config: ec.config}
_spec = sqlgraph.NewCreateSpec(event.Table, sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt))
)
if value, ok := ec.mutation.CreatedAt(); ok {
_spec.SetField(event.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = &value
}
if value, ok := ec.mutation.UpdatedAt(); ok {
_spec.SetField(event.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = &value
}
if value, ok := ec.mutation.Time(); ok {
_spec.SetField(event.FieldTime, field.TypeTime, value)
_node.Time = value
}
if value, ok := ec.mutation.Serialized(); ok {
_spec.SetField(event.FieldSerialized, field.TypeString, value)
_node.Serialized = value
}
if nodes := ec.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: event.OwnerTable,
Columns: []string{event.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.AlertEvents = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// EventCreateBulk is the builder for creating many Event entities in bulk.
type EventCreateBulk struct {
config
err error
builders []*EventCreate
}
// Save creates the Event entities in the database.
func (ecb *EventCreateBulk) Save(ctx context.Context) ([]*Event, error) {
if ecb.err != nil {
return nil, ecb.err
}
specs := make([]*sqlgraph.CreateSpec, len(ecb.builders))
nodes := make([]*Event, len(ecb.builders))
mutators := make([]Mutator, len(ecb.builders))
for i := range ecb.builders {
func(i int, root context.Context) {
builder := ecb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*EventMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
var err error
nodes[i], specs[i] = builder.createSpec()
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, ecb.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, ecb.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
}
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, ecb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (ecb *EventCreateBulk) SaveX(ctx context.Context) []*Event {
v, err := ecb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ecb *EventCreateBulk) Exec(ctx context.Context) error {
_, err := ecb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ecb *EventCreateBulk) ExecX(ctx context.Context) {
if err := ecb.Exec(ctx); err != nil {
panic(err)
}
}