mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
192 lines
4.5 KiB
Go
192 lines
4.5 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"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebookincubator/ent/entc/integration/ent/node"
|
|
"github.com/facebookincubator/ent/schema/field"
|
|
)
|
|
|
|
// NodeCreate is the builder for creating a Node entity.
|
|
type NodeCreate struct {
|
|
config
|
|
mutation *NodeMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetValue sets the value field.
|
|
func (nc *NodeCreate) SetValue(i int) *NodeCreate {
|
|
nc.mutation.SetValue(i)
|
|
return nc
|
|
}
|
|
|
|
// SetNillableValue sets the value field if the given value is not nil.
|
|
func (nc *NodeCreate) SetNillableValue(i *int) *NodeCreate {
|
|
if i != nil {
|
|
nc.SetValue(*i)
|
|
}
|
|
return nc
|
|
}
|
|
|
|
// SetPrevID sets the prev edge to Node by id.
|
|
func (nc *NodeCreate) SetPrevID(id int) *NodeCreate {
|
|
nc.mutation.SetPrevID(id)
|
|
return nc
|
|
}
|
|
|
|
// SetNillablePrevID sets the prev edge to Node by id if the given value is not nil.
|
|
func (nc *NodeCreate) SetNillablePrevID(id *int) *NodeCreate {
|
|
if id != nil {
|
|
nc = nc.SetPrevID(*id)
|
|
}
|
|
return nc
|
|
}
|
|
|
|
// SetPrev sets the prev edge to Node.
|
|
func (nc *NodeCreate) SetPrev(n *Node) *NodeCreate {
|
|
return nc.SetPrevID(n.ID)
|
|
}
|
|
|
|
// SetNextID sets the next edge to Node by id.
|
|
func (nc *NodeCreate) SetNextID(id int) *NodeCreate {
|
|
nc.mutation.SetNextID(id)
|
|
return nc
|
|
}
|
|
|
|
// SetNillableNextID sets the next edge to Node by id if the given value is not nil.
|
|
func (nc *NodeCreate) SetNillableNextID(id *int) *NodeCreate {
|
|
if id != nil {
|
|
nc = nc.SetNextID(*id)
|
|
}
|
|
return nc
|
|
}
|
|
|
|
// SetNext sets the next edge to Node.
|
|
func (nc *NodeCreate) SetNext(n *Node) *NodeCreate {
|
|
return nc.SetNextID(n.ID)
|
|
}
|
|
|
|
// Mutation returns the NodeMutation object of the builder.
|
|
func (nc *NodeCreate) Mutation() *NodeMutation {
|
|
return nc.mutation
|
|
}
|
|
|
|
// Save creates the Node in the database.
|
|
func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) {
|
|
var (
|
|
err error
|
|
node *Node
|
|
)
|
|
if len(nc.hooks) == 0 {
|
|
node, err = nc.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*NodeMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
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 *NodeCreate) SaveX(ctx context.Context) *Node {
|
|
v, err := nc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
|
|
n, _spec := nc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil {
|
|
if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
n.ID = int(id)
|
|
return n, nil
|
|
}
|
|
|
|
func (nc *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) {
|
|
var (
|
|
n = &Node{config: nc.config}
|
|
_spec = &sqlgraph.CreateSpec{
|
|
Table: node.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if value, ok := nc.mutation.Value(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: node.FieldValue,
|
|
})
|
|
n.Value = value
|
|
}
|
|
if nodes := nc.mutation.PrevIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2O,
|
|
Inverse: true,
|
|
Table: node.PrevTable,
|
|
Columns: []string{node.PrevColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
if nodes := nc.mutation.NextIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2O,
|
|
Inverse: false,
|
|
Table: node.NextTable,
|
|
Columns: []string{node.NextColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return n, _spec
|
|
}
|