Files
ent/entc/integration/customid/ent/note_create.go
2021-04-17 13:45:51 +03:00

296 lines
7.4 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/customid/ent/note"
"entgo.io/ent/entc/integration/customid/ent/schema"
"entgo.io/ent/schema/field"
)
// NoteCreate is the builder for creating a Note entity.
type NoteCreate struct {
config
mutation *NoteMutation
hooks []Hook
}
// SetText sets the "text" field.
func (nc *NoteCreate) SetText(s string) *NoteCreate {
nc.mutation.SetText(s)
return nc
}
// SetNillableText sets the "text" field if the given value is not nil.
func (nc *NoteCreate) SetNillableText(s *string) *NoteCreate {
if s != nil {
nc.SetText(*s)
}
return nc
}
// SetID sets the "id" field.
func (nc *NoteCreate) SetID(si schema.NoteID) *NoteCreate {
nc.mutation.SetID(si)
return nc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (nc *NoteCreate) SetNillableID(si *schema.NoteID) *NoteCreate {
if si != nil {
nc.SetID(*si)
}
return nc
}
// SetParentID sets the "parent" edge to the Note entity by ID.
func (nc *NoteCreate) SetParentID(id schema.NoteID) *NoteCreate {
nc.mutation.SetParentID(id)
return nc
}
// SetNillableParentID sets the "parent" edge to the Note entity by ID if the given value is not nil.
func (nc *NoteCreate) SetNillableParentID(id *schema.NoteID) *NoteCreate {
if id != nil {
nc = nc.SetParentID(*id)
}
return nc
}
// SetParent sets the "parent" edge to the Note entity.
func (nc *NoteCreate) SetParent(n *Note) *NoteCreate {
return nc.SetParentID(n.ID)
}
// AddChildIDs adds the "children" edge to the Note entity by IDs.
func (nc *NoteCreate) AddChildIDs(ids ...schema.NoteID) *NoteCreate {
nc.mutation.AddChildIDs(ids...)
return nc
}
// AddChildren adds the "children" edges to the Note entity.
func (nc *NoteCreate) AddChildren(n ...*Note) *NoteCreate {
ids := make([]schema.NoteID, len(n))
for i := range n {
ids[i] = n[i].ID
}
return nc.AddChildIDs(ids...)
}
// Mutation returns the NoteMutation object of the builder.
func (nc *NoteCreate) Mutation() *NoteMutation {
return nc.mutation
}
// Save creates the Note in the database.
func (nc *NoteCreate) Save(ctx context.Context) (*Note, error) {
var (
err error
node *Note
)
nc.defaults()
if len(nc.hooks) == 0 {
if err = nc.check(); err != nil {
return nil, err
}
node, err = nc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*NoteMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = nc.check(); err != nil {
return nil, err
}
nc.mutation = mutation
node, err = nc.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(nc.hooks) - 1; i >= 0; i-- {
mut = nc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, nc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (nc *NoteCreate) SaveX(ctx context.Context) *Note {
v, err := nc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// defaults sets the default values of the builder before save.
func (nc *NoteCreate) defaults() {
if _, ok := nc.mutation.ID(); !ok {
v := note.DefaultID()
nc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (nc *NoteCreate) check() error {
if v, ok := nc.mutation.ID(); ok {
if err := note.IDValidator(string(v)); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf("ent: validator failed for field \"id\": %w", err)}
}
}
return nil
}
func (nc *NoteCreate) sqlSave(ctx context.Context) (*Note, error) {
_node, _spec := nc.createSpec()
if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
return _node, nil
}
func (nc *NoteCreate) createSpec() (*Note, *sqlgraph.CreateSpec) {
var (
_node = &Note{config: nc.config}
_spec = &sqlgraph.CreateSpec{
Table: note.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: note.FieldID,
},
}
)
if id, ok := nc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := nc.mutation.Text(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: note.FieldText,
})
_node.Text = value
}
if nodes := nc.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: note.ParentTable,
Columns: []string{note.ParentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: note.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.note_children = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := nc.mutation.ChildrenIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: note.ChildrenTable,
Columns: []string{note.ChildrenColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: note.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// NoteCreateBulk is the builder for creating many Note entities in bulk.
type NoteCreateBulk struct {
config
builders []*NoteCreate
}
// Save creates the Note entities in the database.
func (ncb *NoteCreateBulk) Save(ctx context.Context) ([]*Note, error) {
specs := make([]*sqlgraph.CreateSpec, len(ncb.builders))
nodes := make([]*Note, len(ncb.builders))
mutators := make([]Mutator, len(ncb.builders))
for i := range ncb.builders {
func(i int, root context.Context) {
builder := ncb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*NoteMutation)
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, ncb.builders[i+1].mutation)
} else {
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, ncb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
}
}
mutation.done = true
if err != nil {
return nil, err
}
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, ncb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (ncb *NoteCreateBulk) SaveX(ctx context.Context) []*Note {
v, err := ncb.Save(ctx)
if err != nil {
panic(err)
}
return v
}