230 lines
6.2 KiB
Go
230 lines
6.2 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"somegit.dev/csgowtf/csgowtfd/ent/matchplayer"
|
|
"somegit.dev/csgowtf/csgowtfd/ent/spray"
|
|
)
|
|
|
|
// SprayCreate is the builder for creating a Spray entity.
|
|
type SprayCreate struct {
|
|
config
|
|
mutation *SprayMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetWeapon sets the "weapon" field.
|
|
func (sc *SprayCreate) SetWeapon(i int) *SprayCreate {
|
|
sc.mutation.SetWeapon(i)
|
|
return sc
|
|
}
|
|
|
|
// SetSpray sets the "spray" field.
|
|
func (sc *SprayCreate) SetSpray(b []byte) *SprayCreate {
|
|
sc.mutation.SetSpray(b)
|
|
return sc
|
|
}
|
|
|
|
// SetMatchPlayersID sets the "match_players" edge to the MatchPlayer entity by ID.
|
|
func (sc *SprayCreate) SetMatchPlayersID(id int) *SprayCreate {
|
|
sc.mutation.SetMatchPlayersID(id)
|
|
return sc
|
|
}
|
|
|
|
// SetNillableMatchPlayersID sets the "match_players" edge to the MatchPlayer entity by ID if the given value is not nil.
|
|
func (sc *SprayCreate) SetNillableMatchPlayersID(id *int) *SprayCreate {
|
|
if id != nil {
|
|
sc = sc.SetMatchPlayersID(*id)
|
|
}
|
|
return sc
|
|
}
|
|
|
|
// SetMatchPlayers sets the "match_players" edge to the MatchPlayer entity.
|
|
func (sc *SprayCreate) SetMatchPlayers(m *MatchPlayer) *SprayCreate {
|
|
return sc.SetMatchPlayersID(m.ID)
|
|
}
|
|
|
|
// Mutation returns the SprayMutation object of the builder.
|
|
func (sc *SprayCreate) Mutation() *SprayMutation {
|
|
return sc.mutation
|
|
}
|
|
|
|
// Save creates the Spray in the database.
|
|
func (sc *SprayCreate) Save(ctx context.Context) (*Spray, error) {
|
|
return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (sc *SprayCreate) SaveX(ctx context.Context) *Spray {
|
|
v, err := sc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (sc *SprayCreate) Exec(ctx context.Context) error {
|
|
_, err := sc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (sc *SprayCreate) ExecX(ctx context.Context) {
|
|
if err := sc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (sc *SprayCreate) check() error {
|
|
if _, ok := sc.mutation.Weapon(); !ok {
|
|
return &ValidationError{Name: "weapon", err: errors.New(`ent: missing required field "Spray.weapon"`)}
|
|
}
|
|
if _, ok := sc.mutation.Spray(); !ok {
|
|
return &ValidationError{Name: "spray", err: errors.New(`ent: missing required field "Spray.spray"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sc *SprayCreate) sqlSave(ctx context.Context) (*Spray, error) {
|
|
if err := sc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := sc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
sc.mutation.id = &_node.ID
|
|
sc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (sc *SprayCreate) createSpec() (*Spray, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Spray{config: sc.config}
|
|
_spec = sqlgraph.NewCreateSpec(spray.Table, sqlgraph.NewFieldSpec(spray.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := sc.mutation.Weapon(); ok {
|
|
_spec.SetField(spray.FieldWeapon, field.TypeInt, value)
|
|
_node.Weapon = value
|
|
}
|
|
if value, ok := sc.mutation.Spray(); ok {
|
|
_spec.SetField(spray.FieldSpray, field.TypeBytes, value)
|
|
_node.Spray = value
|
|
}
|
|
if nodes := sc.mutation.MatchPlayersIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: spray.MatchPlayersTable,
|
|
Columns: []string{spray.MatchPlayersColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(matchplayer.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_node.match_player_spray = &nodes[0]
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// SprayCreateBulk is the builder for creating many Spray entities in bulk.
|
|
type SprayCreateBulk struct {
|
|
config
|
|
builders []*SprayCreate
|
|
}
|
|
|
|
// Save creates the Spray entities in the database.
|
|
func (scb *SprayCreateBulk) Save(ctx context.Context) ([]*Spray, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
|
|
nodes := make([]*Spray, len(scb.builders))
|
|
mutators := make([]Mutator, len(scb.builders))
|
|
for i := range scb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := scb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*SprayMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err := builder.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
builder.mutation = mutation
|
|
var err error
|
|
nodes[i], specs[i] = builder.createSpec()
|
|
if i < len(mutators)-1 {
|
|
_, err = mutators[i+1].Mutate(root, scb.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, scb.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 {
|
|
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, scb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (scb *SprayCreateBulk) SaveX(ctx context.Context) []*Spray {
|
|
v, err := scb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (scb *SprayCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := scb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (scb *SprayCreateBulk) ExecX(ctx context.Context) {
|
|
if err := scb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|