// Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "code.icod.de/postfix/manager/ent/account" "code.icod.de/postfix/manager/ent/domain" "code.icod.de/postfix/manager/ent/logentry" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // LogentryCreate is the builder for creating a Logentry entity. type LogentryCreate struct { config mutation *LogentryMutation hooks []Hook } // SetTimestamp sets the "timestamp" field. func (lc *LogentryCreate) SetTimestamp(t time.Time) *LogentryCreate { lc.mutation.SetTimestamp(t) return lc } // SetNillableTimestamp sets the "timestamp" field if the given value is not nil. func (lc *LogentryCreate) SetNillableTimestamp(t *time.Time) *LogentryCreate { if t != nil { lc.SetTimestamp(*t) } return lc } // SetAction sets the "action" field. func (lc *LogentryCreate) SetAction(s string) *LogentryCreate { lc.mutation.SetAction(s) return lc } // SetData sets the "data" field. func (lc *LogentryCreate) SetData(s string) *LogentryCreate { lc.mutation.SetData(s) return lc } // SetNillableData sets the "data" field if the given value is not nil. func (lc *LogentryCreate) SetNillableData(s *string) *LogentryCreate { if s != nil { lc.SetData(*s) } return lc } // SetAccountID sets the "account_id" field. func (lc *LogentryCreate) SetAccountID(i int64) *LogentryCreate { lc.mutation.SetAccountID(i) return lc } // SetNillableAccountID sets the "account_id" field if the given value is not nil. func (lc *LogentryCreate) SetNillableAccountID(i *int64) *LogentryCreate { if i != nil { lc.SetAccountID(*i) } return lc } // SetDomainID sets the "domain_id" field. func (lc *LogentryCreate) SetDomainID(i int64) *LogentryCreate { lc.mutation.SetDomainID(i) return lc } // SetNillableDomainID sets the "domain_id" field if the given value is not nil. func (lc *LogentryCreate) SetNillableDomainID(i *int64) *LogentryCreate { if i != nil { lc.SetDomainID(*i) } return lc } // SetID sets the "id" field. func (lc *LogentryCreate) SetID(i int64) *LogentryCreate { lc.mutation.SetID(i) return lc } // SetAccount sets the "account" edge to the Account entity. func (lc *LogentryCreate) SetAccount(a *Account) *LogentryCreate { return lc.SetAccountID(a.ID) } // SetDomain sets the "domain" edge to the Domain entity. func (lc *LogentryCreate) SetDomain(d *Domain) *LogentryCreate { return lc.SetDomainID(d.ID) } // Mutation returns the LogentryMutation object of the builder. func (lc *LogentryCreate) Mutation() *LogentryMutation { return lc.mutation } // Save creates the Logentry in the database. func (lc *LogentryCreate) Save(ctx context.Context) (*Logentry, error) { var ( err error node *Logentry ) lc.defaults() if len(lc.hooks) == 0 { if err = lc.check(); err != nil { return nil, err } node, err = lc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*LogentryMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = lc.check(); err != nil { return nil, err } lc.mutation = mutation if node, err = lc.sqlSave(ctx); err != nil { return nil, err } mutation.id = &node.ID mutation.done = true return node, err }) for i := len(lc.hooks) - 1; i >= 0; i-- { if lc.hooks[i] == nil { return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = lc.hooks[i](mut) } if _, err := mut.Mutate(ctx, lc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (lc *LogentryCreate) SaveX(ctx context.Context) *Logentry { v, err := lc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lc *LogentryCreate) Exec(ctx context.Context) error { _, err := lc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lc *LogentryCreate) ExecX(ctx context.Context) { if err := lc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (lc *LogentryCreate) defaults() { if _, ok := lc.mutation.Timestamp(); !ok { v := logentry.DefaultTimestamp() lc.mutation.SetTimestamp(v) } } // check runs all checks and user-defined validators on the builder. func (lc *LogentryCreate) check() error { if _, ok := lc.mutation.Timestamp(); !ok { return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "Logentry.timestamp"`)} } if _, ok := lc.mutation.Action(); !ok { return &ValidationError{Name: "action", err: errors.New(`ent: missing required field "Logentry.action"`)} } return nil } func (lc *LogentryCreate) sqlSave(ctx context.Context) (*Logentry, error) { _node, _spec := lc.createSpec() if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{err.Error(), err} } return nil, err } if _spec.ID.Value != _node.ID { id := _spec.ID.Value.(int64) _node.ID = int64(id) } return _node, nil } func (lc *LogentryCreate) createSpec() (*Logentry, *sqlgraph.CreateSpec) { var ( _node = &Logentry{config: lc.config} _spec = &sqlgraph.CreateSpec{ Table: logentry.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: logentry.FieldID, }, } ) if id, ok := lc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := lc.mutation.Timestamp(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: logentry.FieldTimestamp, }) _node.Timestamp = value } if value, ok := lc.mutation.Action(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: logentry.FieldAction, }) _node.Action = value } if value, ok := lc.mutation.Data(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: logentry.FieldData, }) _node.Data = &value } if nodes := lc.mutation.AccountIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: logentry.AccountTable, Columns: []string{logentry.AccountColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: account.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.AccountID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := lc.mutation.DomainIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: logentry.DomainTable, Columns: []string{logentry.DomainColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: domain.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.DomainID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // LogentryCreateBulk is the builder for creating many Logentry entities in bulk. type LogentryCreateBulk struct { config builders []*LogentryCreate } // Save creates the Logentry entities in the database. func (lcb *LogentryCreateBulk) Save(ctx context.Context) ([]*Logentry, error) { specs := make([]*sqlgraph.CreateSpec, len(lcb.builders)) nodes := make([]*Logentry, len(lcb.builders)) mutators := make([]Mutator, len(lcb.builders)) for i := range lcb.builders { func(i int, root context.Context) { builder := lcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*LogentryMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation nodes[i], specs[i] = builder.createSpec() var err error if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, lcb.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, lcb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{err.Error(), err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID mutation.done = true if specs[i].ID.Value != nil && nodes[i].ID == 0 { id := specs[i].ID.Value.(int64) nodes[i].ID = int64(id) } 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, lcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (lcb *LogentryCreateBulk) SaveX(ctx context.Context) []*Logentry { v, err := lcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lcb *LogentryCreateBulk) Exec(ctx context.Context) error { _, err := lcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lcb *LogentryCreateBulk) ExecX(ctx context.Context) { if err := lcb.Exec(ctx); err != nil { panic(err) } }