You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
6.1 KiB
249 lines
6.1 KiB
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/go-kratos/kratos/examples/transaction/ent/internal/data/ent/card"
|
|
)
|
|
|
|
// CardCreate is the builder for creating a Card entity.
|
|
type CardCreate struct {
|
|
config
|
|
mutation *CardMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetUserID sets the "user_id" field.
|
|
func (cc *CardCreate) SetUserID(s string) *CardCreate {
|
|
cc.mutation.SetUserID(s)
|
|
return cc
|
|
}
|
|
|
|
// SetMoney sets the "money" field.
|
|
func (cc *CardCreate) SetMoney(s string) *CardCreate {
|
|
cc.mutation.SetMoney(s)
|
|
return cc
|
|
}
|
|
|
|
// SetID sets the "id" field.
|
|
func (cc *CardCreate) SetID(i int) *CardCreate {
|
|
cc.mutation.SetID(i)
|
|
return cc
|
|
}
|
|
|
|
// Mutation returns the CardMutation object of the builder.
|
|
func (cc *CardCreate) Mutation() *CardMutation {
|
|
return cc.mutation
|
|
}
|
|
|
|
// Save creates the Card in the database.
|
|
func (cc *CardCreate) Save(ctx context.Context) (*Card, error) {
|
|
var (
|
|
err error
|
|
node *Card
|
|
)
|
|
if len(cc.hooks) == 0 {
|
|
if err = cc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = cc.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*CardMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = cc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
cc.mutation = mutation
|
|
if node, err = cc.sqlSave(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
mutation.id = &node.ID
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(cc.hooks) - 1; i >= 0; i-- {
|
|
if cc.hooks[i] == nil {
|
|
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
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 *CardCreate) SaveX(ctx context.Context) *Card {
|
|
v, err := cc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (cc *CardCreate) Exec(ctx context.Context) error {
|
|
_, err := cc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (cc *CardCreate) ExecX(ctx context.Context) {
|
|
if err := cc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (cc *CardCreate) check() error {
|
|
if _, ok := cc.mutation.UserID(); !ok {
|
|
return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "user_id"`)}
|
|
}
|
|
if _, ok := cc.mutation.Money(); !ok {
|
|
return &ValidationError{Name: "money", err: errors.New(`ent: missing required field "money"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) {
|
|
_node, _spec := cc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{err.Error(), err}
|
|
}
|
|
return nil, err
|
|
}
|
|
if _node.ID == 0 {
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
}
|
|
return _node, nil
|
|
}
|
|
|
|
func (cc *CardCreate) createSpec() (*Card, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Card{config: cc.config}
|
|
_spec = &sqlgraph.CreateSpec{
|
|
Table: card.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: card.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if id, ok := cc.mutation.ID(); ok {
|
|
_node.ID = id
|
|
_spec.ID.Value = id
|
|
}
|
|
if value, ok := cc.mutation.UserID(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: value,
|
|
Column: card.FieldUserID,
|
|
})
|
|
_node.UserID = value
|
|
}
|
|
if value, ok := cc.mutation.Money(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: value,
|
|
Column: card.FieldMoney,
|
|
})
|
|
_node.Money = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// CardCreateBulk is the builder for creating many Card entities in bulk.
|
|
type CardCreateBulk struct {
|
|
config
|
|
builders []*CardCreate
|
|
}
|
|
|
|
// Save creates the Card entities in the database.
|
|
func (ccb *CardCreateBulk) Save(ctx context.Context) ([]*Card, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
|
|
nodes := make([]*Card, len(ccb.builders))
|
|
mutators := make([]Mutator, len(ccb.builders))
|
|
for i := range ccb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := ccb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*CardMutation)
|
|
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, ccb.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, ccb.driver, spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{err.Error(), err}
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mutation.id = &nodes[i].ID
|
|
mutation.done = true
|
|
if specs[i].ID.Value != nil && nodes[i].ID == 0 {
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int(id)
|
|
}
|
|
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, ccb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (ccb *CardCreateBulk) SaveX(ctx context.Context) []*Card {
|
|
v, err := ccb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (ccb *CardCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := ccb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (ccb *CardCreateBulk) ExecX(ctx context.Context) {
|
|
if err := ccb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|