320 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			320 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Code generated by ent, 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) {
 | |
| 	lc.defaults()
 | |
| 	return withHooks(ctx, lc.sqlSave, lc.mutation, lc.hooks)
 | |
| }
 | |
| 
 | |
| // 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) {
 | |
| 	if err := lc.check(); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	_node, _spec := lc.createSpec()
 | |
| 	if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil {
 | |
| 		if sqlgraph.IsConstraintError(err) {
 | |
| 			err = &ConstraintError{msg: err.Error(), wrap: err}
 | |
| 		}
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	if _spec.ID.Value != _node.ID {
 | |
| 		id := _spec.ID.Value.(int64)
 | |
| 		_node.ID = int64(id)
 | |
| 	}
 | |
| 	lc.mutation.id = &_node.ID
 | |
| 	lc.mutation.done = true
 | |
| 	return _node, nil
 | |
| }
 | |
| 
 | |
| func (lc *LogentryCreate) createSpec() (*Logentry, *sqlgraph.CreateSpec) {
 | |
| 	var (
 | |
| 		_node = &Logentry{config: lc.config}
 | |
| 		_spec = sqlgraph.NewCreateSpec(logentry.Table, sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt64))
 | |
| 	)
 | |
| 	if id, ok := lc.mutation.ID(); ok {
 | |
| 		_node.ID = id
 | |
| 		_spec.ID.Value = id
 | |
| 	}
 | |
| 	if value, ok := lc.mutation.Timestamp(); ok {
 | |
| 		_spec.SetField(logentry.FieldTimestamp, field.TypeTime, value)
 | |
| 		_node.Timestamp = value
 | |
| 	}
 | |
| 	if value, ok := lc.mutation.Action(); ok {
 | |
| 		_spec.SetField(logentry.FieldAction, field.TypeString, value)
 | |
| 		_node.Action = value
 | |
| 	}
 | |
| 	if value, ok := lc.mutation.Data(); ok {
 | |
| 		_spec.SetField(logentry.FieldData, field.TypeString, value)
 | |
| 		_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.NewFieldSpec(account.FieldID, field.TypeInt64),
 | |
| 			},
 | |
| 		}
 | |
| 		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.NewFieldSpec(domain.FieldID, field.TypeInt64),
 | |
| 			},
 | |
| 		}
 | |
| 		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
 | |
| 	err      error
 | |
| 	builders []*LogentryCreate
 | |
| }
 | |
| 
 | |
| // Save creates the Logentry entities in the database.
 | |
| func (lcb *LogentryCreateBulk) Save(ctx context.Context) ([]*Logentry, error) {
 | |
| 	if lcb.err != nil {
 | |
| 		return nil, lcb.err
 | |
| 	}
 | |
| 	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
 | |
| 				var err error
 | |
| 				nodes[i], specs[i] = builder.createSpec()
 | |
| 				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{msg: err.Error(), wrap: err}
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 				if err != nil {
 | |
| 					return nil, err
 | |
| 				}
 | |
| 				mutation.id = &nodes[i].ID
 | |
| 				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
 | |
| 					id := specs[i].ID.Value.(int64)
 | |
| 					nodes[i].ID = int64(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, 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)
 | |
| 	}
 | |
| }
 |