// 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 (_c *SprayCreate) SetWeapon(v int) *SprayCreate { _c.mutation.SetWeapon(v) return _c } // SetSpray sets the "spray" field. func (_c *SprayCreate) SetSpray(v []byte) *SprayCreate { _c.mutation.SetSpray(v) return _c } // SetMatchPlayersID sets the "match_players" edge to the MatchPlayer entity by ID. func (_c *SprayCreate) SetMatchPlayersID(id int) *SprayCreate { _c.mutation.SetMatchPlayersID(id) return _c } // SetNillableMatchPlayersID sets the "match_players" edge to the MatchPlayer entity by ID if the given value is not nil. func (_c *SprayCreate) SetNillableMatchPlayersID(id *int) *SprayCreate { if id != nil { _c = _c.SetMatchPlayersID(*id) } return _c } // SetMatchPlayers sets the "match_players" edge to the MatchPlayer entity. func (_c *SprayCreate) SetMatchPlayers(v *MatchPlayer) *SprayCreate { return _c.SetMatchPlayersID(v.ID) } // Mutation returns the SprayMutation object of the builder. func (_c *SprayCreate) Mutation() *SprayMutation { return _c.mutation } // Save creates the Spray in the database. func (_c *SprayCreate) Save(ctx context.Context) (*Spray, error) { return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *SprayCreate) SaveX(ctx context.Context) *Spray { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *SprayCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *SprayCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (_c *SprayCreate) check() error { if _, ok := _c.mutation.Weapon(); !ok { return &ValidationError{Name: "weapon", err: errors.New(`ent: missing required field "Spray.weapon"`)} } if _, ok := _c.mutation.Spray(); !ok { return &ValidationError{Name: "spray", err: errors.New(`ent: missing required field "Spray.spray"`)} } return nil } func (_c *SprayCreate) sqlSave(ctx context.Context) (*Spray, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() if err := sqlgraph.CreateNode(ctx, _c.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) _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *SprayCreate) createSpec() (*Spray, *sqlgraph.CreateSpec) { var ( _node = &Spray{config: _c.config} _spec = sqlgraph.NewCreateSpec(spray.Table, sqlgraph.NewFieldSpec(spray.FieldID, field.TypeInt)) ) if value, ok := _c.mutation.Weapon(); ok { _spec.SetField(spray.FieldWeapon, field.TypeInt, value) _node.Weapon = value } if value, ok := _c.mutation.Spray(); ok { _spec.SetField(spray.FieldSpray, field.TypeBytes, value) _node.Spray = value } if nodes := _c.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 err error builders []*SprayCreate } // Save creates the Spray entities in the database. func (_c *SprayCreateBulk) Save(ctx context.Context) ([]*Spray, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Spray, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.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, _c.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, _c.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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *SprayCreateBulk) SaveX(ctx context.Context) []*Spray { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *SprayCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *SprayCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }