mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
241 lines
6.0 KiB
Go
241 lines
6.0 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 ent, DO NOT EDIT.
|
|
|
|
package entv2
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/entc/integration/migrate/entv2/blog"
|
|
"entgo.io/ent/entc/integration/migrate/entv2/user"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// BlogCreate is the builder for creating a Blog entity.
|
|
type BlogCreate struct {
|
|
config
|
|
mutation *BlogMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetOid sets the "oid" field.
|
|
func (bc *BlogCreate) SetOid(i int) *BlogCreate {
|
|
bc.mutation.SetOid(i)
|
|
return bc
|
|
}
|
|
|
|
// SetID sets the "id" field.
|
|
func (bc *BlogCreate) SetID(i int) *BlogCreate {
|
|
bc.mutation.SetID(i)
|
|
return bc
|
|
}
|
|
|
|
// AddAdminIDs adds the "admins" edge to the User entity by IDs.
|
|
func (bc *BlogCreate) AddAdminIDs(ids ...int) *BlogCreate {
|
|
bc.mutation.AddAdminIDs(ids...)
|
|
return bc
|
|
}
|
|
|
|
// AddAdmins adds the "admins" edges to the User entity.
|
|
func (bc *BlogCreate) AddAdmins(u ...*User) *BlogCreate {
|
|
ids := make([]int, len(u))
|
|
for i := range u {
|
|
ids[i] = u[i].ID
|
|
}
|
|
return bc.AddAdminIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the BlogMutation object of the builder.
|
|
func (bc *BlogCreate) Mutation() *BlogMutation {
|
|
return bc.mutation
|
|
}
|
|
|
|
// Save creates the Blog in the database.
|
|
func (bc *BlogCreate) Save(ctx context.Context) (*Blog, error) {
|
|
return withHooks[*Blog, BlogMutation](ctx, bc.sqlSave, bc.mutation, bc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (bc *BlogCreate) SaveX(ctx context.Context) *Blog {
|
|
v, err := bc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (bc *BlogCreate) Exec(ctx context.Context) error {
|
|
_, err := bc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (bc *BlogCreate) ExecX(ctx context.Context) {
|
|
if err := bc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (bc *BlogCreate) check() error {
|
|
switch bc.driver.Dialect() {
|
|
case dialect.MySQL, dialect.SQLite:
|
|
if _, ok := bc.mutation.Oid(); !ok {
|
|
return &ValidationError{Name: "oid", err: errors.New(`entv2: missing required field "Blog.oid"`)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (bc *BlogCreate) sqlSave(ctx context.Context) (*Blog, error) {
|
|
if err := bc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := bc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, bc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
if _spec.ID.Value != _node.ID {
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
}
|
|
bc.mutation.id = &_node.ID
|
|
bc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (bc *BlogCreate) createSpec() (*Blog, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Blog{config: bc.config}
|
|
_spec = &sqlgraph.CreateSpec{
|
|
Table: blog.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: blog.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if id, ok := bc.mutation.ID(); ok {
|
|
_node.ID = id
|
|
_spec.ID.Value = id
|
|
}
|
|
if value, ok := bc.mutation.Oid(); ok {
|
|
_spec.SetField(blog.FieldOid, field.TypeInt, value)
|
|
_node.Oid = value
|
|
}
|
|
if nodes := bc.mutation.AdminsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// BlogCreateBulk is the builder for creating many Blog entities in bulk.
|
|
type BlogCreateBulk struct {
|
|
config
|
|
builders []*BlogCreate
|
|
}
|
|
|
|
// Save creates the Blog entities in the database.
|
|
func (bcb *BlogCreateBulk) Save(ctx context.Context) ([]*Blog, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(bcb.builders))
|
|
nodes := make([]*Blog, len(bcb.builders))
|
|
mutators := make([]Mutator, len(bcb.builders))
|
|
for i := range bcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := bcb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*BlogMutation)
|
|
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, bcb.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, bcb.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 && nodes[i].ID == 0 {
|
|
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, bcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (bcb *BlogCreateBulk) SaveX(ctx context.Context) []*Blog {
|
|
v, err := bcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (bcb *BlogCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := bcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (bcb *BlogCreateBulk) ExecX(ctx context.Context) {
|
|
if err := bcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|