Files
ent/entc/integration/ent/comment_create.go

141 lines
3.5 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"
"errors"
"fmt"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/entc/integration/ent/comment"
"github.com/facebookincubator/ent/schema/field"
)
// CommentCreate is the builder for creating a Comment entity.
type CommentCreate struct {
config
mutation *CommentMutation
hooks []Hook
}
// SetUniqueInt sets the unique_int field.
func (cc *CommentCreate) SetUniqueInt(i int) *CommentCreate {
cc.mutation.SetUniqueInt(i)
return cc
}
// SetUniqueFloat sets the unique_float field.
func (cc *CommentCreate) SetUniqueFloat(f float64) *CommentCreate {
cc.mutation.SetUniqueFloat(f)
return cc
}
// SetNillableInt sets the nillable_int field.
func (cc *CommentCreate) SetNillableInt(i int) *CommentCreate {
cc.mutation.SetNillableInt(i)
return cc
}
// SetNillableNillableInt sets the nillable_int field if the given value is not nil.
func (cc *CommentCreate) SetNillableNillableInt(i *int) *CommentCreate {
if i != nil {
cc.SetNillableInt(*i)
}
return cc
}
// Save creates the Comment in the database.
func (cc *CommentCreate) Save(ctx context.Context) (*Comment, error) {
if _, ok := cc.mutation.UniqueInt(); !ok {
return nil, errors.New("ent: missing required field \"unique_int\"")
}
if _, ok := cc.mutation.UniqueFloat(); !ok {
return nil, errors.New("ent: missing required field \"unique_float\"")
}
var (
err error
node *Comment
)
if len(cc.hooks) == 0 {
node, err = cc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*CommentMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
cc.mutation = mutation
node, err = cc.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(cc.hooks) - 1; i >= 0; i-- {
mut = cc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, cc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (cc *CommentCreate) SaveX(ctx context.Context) *Comment {
v, err := cc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) {
var (
c = &Comment{config: cc.config}
_spec = &sqlgraph.CreateSpec{
Table: comment.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
}
)
if value, ok := cc.mutation.UniqueInt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: comment.FieldUniqueInt,
})
c.UniqueInt = value
}
if value, ok := cc.mutation.UniqueFloat(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeFloat64,
Value: value,
Column: comment.FieldUniqueFloat,
})
c.UniqueFloat = value
}
if value, ok := cc.mutation.NillableInt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: comment.FieldNillableInt,
})
c.NillableInt = &value
}
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
c.ID = int(id)
return c, nil
}