170 lines
4.2 KiB
Go
170 lines
4.2 KiB
Go
|
// Code generated by ent, DO NOT EDIT.
|
||
|
|
||
|
package ent
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
||
|
"entgo.io/ent/schema/field"
|
||
|
"gitserver.in/patialtech/rano/db/ent/todo"
|
||
|
)
|
||
|
|
||
|
// TodoCreate is the builder for creating a Todo entity.
|
||
|
type TodoCreate struct {
|
||
|
config
|
||
|
mutation *TodoMutation
|
||
|
hooks []Hook
|
||
|
}
|
||
|
|
||
|
// Mutation returns the TodoMutation object of the builder.
|
||
|
func (tc *TodoCreate) Mutation() *TodoMutation {
|
||
|
return tc.mutation
|
||
|
}
|
||
|
|
||
|
// Save creates the Todo in the database.
|
||
|
func (tc *TodoCreate) Save(ctx context.Context) (*Todo, error) {
|
||
|
return withHooks(ctx, tc.sqlSave, tc.mutation, tc.hooks)
|
||
|
}
|
||
|
|
||
|
// SaveX calls Save and panics if Save returns an error.
|
||
|
func (tc *TodoCreate) SaveX(ctx context.Context) *Todo {
|
||
|
v, err := tc.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (tc *TodoCreate) Exec(ctx context.Context) error {
|
||
|
_, err := tc.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (tc *TodoCreate) ExecX(ctx context.Context) {
|
||
|
if err := tc.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check runs all checks and user-defined validators on the builder.
|
||
|
func (tc *TodoCreate) check() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (tc *TodoCreate) sqlSave(ctx context.Context) (*Todo, error) {
|
||
|
if err := tc.check(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
_node, _spec := tc.createSpec()
|
||
|
if err := sqlgraph.CreateNode(ctx, tc.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)
|
||
|
tc.mutation.id = &_node.ID
|
||
|
tc.mutation.done = true
|
||
|
return _node, nil
|
||
|
}
|
||
|
|
||
|
func (tc *TodoCreate) createSpec() (*Todo, *sqlgraph.CreateSpec) {
|
||
|
var (
|
||
|
_node = &Todo{config: tc.config}
|
||
|
_spec = sqlgraph.NewCreateSpec(todo.Table, sqlgraph.NewFieldSpec(todo.FieldID, field.TypeInt))
|
||
|
)
|
||
|
return _node, _spec
|
||
|
}
|
||
|
|
||
|
// TodoCreateBulk is the builder for creating many Todo entities in bulk.
|
||
|
type TodoCreateBulk struct {
|
||
|
config
|
||
|
err error
|
||
|
builders []*TodoCreate
|
||
|
}
|
||
|
|
||
|
// Save creates the Todo entities in the database.
|
||
|
func (tcb *TodoCreateBulk) Save(ctx context.Context) ([]*Todo, error) {
|
||
|
if tcb.err != nil {
|
||
|
return nil, tcb.err
|
||
|
}
|
||
|
specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
|
||
|
nodes := make([]*Todo, len(tcb.builders))
|
||
|
mutators := make([]Mutator, len(tcb.builders))
|
||
|
for i := range tcb.builders {
|
||
|
func(i int, root context.Context) {
|
||
|
builder := tcb.builders[i]
|
||
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||
|
mutation, ok := m.(*TodoMutation)
|
||
|
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, tcb.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, tcb.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, tcb.builders[0].mutation); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
return nodes, nil
|
||
|
}
|
||
|
|
||
|
// SaveX is like Save, but panics if an error occurs.
|
||
|
func (tcb *TodoCreateBulk) SaveX(ctx context.Context) []*Todo {
|
||
|
v, err := tcb.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (tcb *TodoCreateBulk) Exec(ctx context.Context) error {
|
||
|
_, err := tcb.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (tcb *TodoCreateBulk) ExecX(ctx context.Context) {
|
||
|
if err := tcb.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|