238 lines
6.3 KiB
Go
Generated
238 lines
6.3 KiB
Go
Generated
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"git.rgst.io/homelab/klefki/internal/db/ent/machine"
|
|
)
|
|
|
|
// MachineCreate is the builder for creating a Machine entity.
|
|
type MachineCreate struct {
|
|
config
|
|
mutation *MachineMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (mc *MachineCreate) SetName(s string) *MachineCreate {
|
|
mc.mutation.SetName(s)
|
|
return mc
|
|
}
|
|
|
|
// SetPublicKey sets the "public_key" field.
|
|
func (mc *MachineCreate) SetPublicKey(b []byte) *MachineCreate {
|
|
mc.mutation.SetPublicKey(b)
|
|
return mc
|
|
}
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
func (mc *MachineCreate) SetCreatedAt(s string) *MachineCreate {
|
|
mc.mutation.SetCreatedAt(s)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
func (mc *MachineCreate) SetNillableCreatedAt(s *string) *MachineCreate {
|
|
if s != nil {
|
|
mc.SetCreatedAt(*s)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetID sets the "id" field.
|
|
func (mc *MachineCreate) SetID(s string) *MachineCreate {
|
|
mc.mutation.SetID(s)
|
|
return mc
|
|
}
|
|
|
|
// Mutation returns the MachineMutation object of the builder.
|
|
func (mc *MachineCreate) Mutation() *MachineMutation {
|
|
return mc.mutation
|
|
}
|
|
|
|
// Save creates the Machine in the database.
|
|
func (mc *MachineCreate) Save(ctx context.Context) (*Machine, error) {
|
|
mc.defaults()
|
|
return withHooks(ctx, mc.sqlSave, mc.mutation, mc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (mc *MachineCreate) SaveX(ctx context.Context) *Machine {
|
|
v, err := mc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mc *MachineCreate) Exec(ctx context.Context) error {
|
|
_, err := mc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mc *MachineCreate) ExecX(ctx context.Context) {
|
|
if err := mc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (mc *MachineCreate) defaults() {
|
|
if _, ok := mc.mutation.CreatedAt(); !ok {
|
|
v := machine.DefaultCreatedAt
|
|
mc.mutation.SetCreatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (mc *MachineCreate) check() error {
|
|
if _, ok := mc.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Machine.name"`)}
|
|
}
|
|
if _, ok := mc.mutation.PublicKey(); !ok {
|
|
return &ValidationError{Name: "public_key", err: errors.New(`ent: missing required field "Machine.public_key"`)}
|
|
}
|
|
if _, ok := mc.mutation.CreatedAt(); !ok {
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Machine.created_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mc *MachineCreate) sqlSave(ctx context.Context) (*Machine, error) {
|
|
if err := mc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := mc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
if _spec.ID.Value != nil {
|
|
if id, ok := _spec.ID.Value.(string); ok {
|
|
_node.ID = id
|
|
} else {
|
|
return nil, fmt.Errorf("unexpected Machine.ID type: %T", _spec.ID.Value)
|
|
}
|
|
}
|
|
mc.mutation.id = &_node.ID
|
|
mc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (mc *MachineCreate) createSpec() (*Machine, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Machine{config: mc.config}
|
|
_spec = sqlgraph.NewCreateSpec(machine.Table, sqlgraph.NewFieldSpec(machine.FieldID, field.TypeString))
|
|
)
|
|
if id, ok := mc.mutation.ID(); ok {
|
|
_node.ID = id
|
|
_spec.ID.Value = id
|
|
}
|
|
if value, ok := mc.mutation.Name(); ok {
|
|
_spec.SetField(machine.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if value, ok := mc.mutation.PublicKey(); ok {
|
|
_spec.SetField(machine.FieldPublicKey, field.TypeBytes, value)
|
|
_node.PublicKey = value
|
|
}
|
|
if value, ok := mc.mutation.CreatedAt(); ok {
|
|
_spec.SetField(machine.FieldCreatedAt, field.TypeString, value)
|
|
_node.CreatedAt = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// MachineCreateBulk is the builder for creating many Machine entities in bulk.
|
|
type MachineCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*MachineCreate
|
|
}
|
|
|
|
// Save creates the Machine entities in the database.
|
|
func (mcb *MachineCreateBulk) Save(ctx context.Context) ([]*Machine, error) {
|
|
if mcb.err != nil {
|
|
return nil, mcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
|
|
nodes := make([]*Machine, len(mcb.builders))
|
|
mutators := make([]Mutator, len(mcb.builders))
|
|
for i := range mcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := mcb.builders[i]
|
|
builder.defaults()
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*MachineMutation)
|
|
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, mcb.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, mcb.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
|
|
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, mcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (mcb *MachineCreateBulk) SaveX(ctx context.Context) []*Machine {
|
|
v, err := mcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mcb *MachineCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := mcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mcb *MachineCreateBulk) ExecX(ctx context.Context) {
|
|
if err := mcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|