295 lines
7.7 KiB
Go
295 lines
7.7 KiB
Go
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"csgowtfd/ent/matchplayer"
|
|
"csgowtfd/ent/messages"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// MessagesCreate is the builder for creating a Messages entity.
|
|
type MessagesCreate struct {
|
|
config
|
|
mutation *MessagesMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetMessage sets the "message" field.
|
|
func (mc *MessagesCreate) SetMessage(s string) *MessagesCreate {
|
|
mc.mutation.SetMessage(s)
|
|
return mc
|
|
}
|
|
|
|
// SetAllChat sets the "all_chat" field.
|
|
func (mc *MessagesCreate) SetAllChat(b bool) *MessagesCreate {
|
|
mc.mutation.SetAllChat(b)
|
|
return mc
|
|
}
|
|
|
|
// SetTick sets the "tick" field.
|
|
func (mc *MessagesCreate) SetTick(i int) *MessagesCreate {
|
|
mc.mutation.SetTick(i)
|
|
return mc
|
|
}
|
|
|
|
// SetMatchPlayerID sets the "match_player" edge to the MatchPlayer entity by ID.
|
|
func (mc *MessagesCreate) SetMatchPlayerID(id int) *MessagesCreate {
|
|
mc.mutation.SetMatchPlayerID(id)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableMatchPlayerID sets the "match_player" edge to the MatchPlayer entity by ID if the given value is not nil.
|
|
func (mc *MessagesCreate) SetNillableMatchPlayerID(id *int) *MessagesCreate {
|
|
if id != nil {
|
|
mc = mc.SetMatchPlayerID(*id)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetMatchPlayer sets the "match_player" edge to the MatchPlayer entity.
|
|
func (mc *MessagesCreate) SetMatchPlayer(m *MatchPlayer) *MessagesCreate {
|
|
return mc.SetMatchPlayerID(m.ID)
|
|
}
|
|
|
|
// Mutation returns the MessagesMutation object of the builder.
|
|
func (mc *MessagesCreate) Mutation() *MessagesMutation {
|
|
return mc.mutation
|
|
}
|
|
|
|
// Save creates the Messages in the database.
|
|
func (mc *MessagesCreate) Save(ctx context.Context) (*Messages, error) {
|
|
var (
|
|
err error
|
|
node *Messages
|
|
)
|
|
if len(mc.hooks) == 0 {
|
|
if err = mc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = mc.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*MessagesMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = mc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
mc.mutation = mutation
|
|
if node, err = mc.sqlSave(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
mutation.id = &node.ID
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(mc.hooks) - 1; i >= 0; i-- {
|
|
if mc.hooks[i] == nil {
|
|
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = mc.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, mc.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (mc *MessagesCreate) SaveX(ctx context.Context) *Messages {
|
|
v, err := mc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mc *MessagesCreate) Exec(ctx context.Context) error {
|
|
_, err := mc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mc *MessagesCreate) ExecX(ctx context.Context) {
|
|
if err := mc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (mc *MessagesCreate) check() error {
|
|
if _, ok := mc.mutation.Message(); !ok {
|
|
return &ValidationError{Name: "message", err: errors.New(`ent: missing required field "Messages.message"`)}
|
|
}
|
|
if _, ok := mc.mutation.AllChat(); !ok {
|
|
return &ValidationError{Name: "all_chat", err: errors.New(`ent: missing required field "Messages.all_chat"`)}
|
|
}
|
|
if _, ok := mc.mutation.Tick(); !ok {
|
|
return &ValidationError{Name: "tick", err: errors.New(`ent: missing required field "Messages.tick"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mc *MessagesCreate) sqlSave(ctx context.Context) (*Messages, error) {
|
|
_node, _spec := mc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{err.Error(), err}
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
return _node, nil
|
|
}
|
|
|
|
func (mc *MessagesCreate) createSpec() (*Messages, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Messages{config: mc.config}
|
|
_spec = &sqlgraph.CreateSpec{
|
|
Table: messages.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: messages.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if value, ok := mc.mutation.Message(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: value,
|
|
Column: messages.FieldMessage,
|
|
})
|
|
_node.Message = value
|
|
}
|
|
if value, ok := mc.mutation.AllChat(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeBool,
|
|
Value: value,
|
|
Column: messages.FieldAllChat,
|
|
})
|
|
_node.AllChat = value
|
|
}
|
|
if value, ok := mc.mutation.Tick(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: messages.FieldTick,
|
|
})
|
|
_node.Tick = value
|
|
}
|
|
if nodes := mc.mutation.MatchPlayerIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: messages.MatchPlayerTable,
|
|
Columns: []string{messages.MatchPlayerColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: matchplayer.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_node.match_player_messages = &nodes[0]
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// MessagesCreateBulk is the builder for creating many Messages entities in bulk.
|
|
type MessagesCreateBulk struct {
|
|
config
|
|
builders []*MessagesCreate
|
|
}
|
|
|
|
// Save creates the Messages entities in the database.
|
|
func (mcb *MessagesCreateBulk) Save(ctx context.Context) ([]*Messages, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
|
|
nodes := make([]*Messages, len(mcb.builders))
|
|
mutators := make([]Mutator, len(mcb.builders))
|
|
for i := range mcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := mcb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*MessagesMutation)
|
|
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, mcb.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, mcb.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 {
|
|
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, mcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (mcb *MessagesCreateBulk) SaveX(ctx context.Context) []*Messages {
|
|
v, err := mcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mcb *MessagesCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := mcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mcb *MessagesCreateBulk) ExecX(ctx context.Context) {
|
|
if err := mcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|