Files
ent/entc/integration/ent/item_create.go

84 lines
1.9 KiB
Go

// Copyright (c) Facebook, Inc. and its affiliates. 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/item"
"github.com/facebookincubator/ent/schema/field"
)
// ItemCreate is the builder for creating a Item entity.
type ItemCreate struct {
config
mutation *ItemMutation
hooks []Hook
}
// Save creates the Item in the database.
func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) {
var (
err error
node *Item
)
if len(ic.hooks) == 0 {
node, err = ic.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ItemMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
ic.mutation = mutation
node, err = ic.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(ic.hooks) - 1; i >= 0; i-- {
mut = ic.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, ic.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (ic *ItemCreate) SaveX(ctx context.Context) *Item {
v, err := ic.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (ic *ItemCreate) sqlSave(ctx context.Context) (*Item, error) {
var (
i = &Item{config: ic.config}
_spec = &sqlgraph.CreateSpec{
Table: item.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: item.FieldID,
},
}
)
if err := sqlgraph.CreateNode(ctx, ic.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
i.ID = int(id)
return i, nil
}