286 lines
7.6 KiB
Go
286 lines
7.6 KiB
Go
|
// Code generated by ent, DO NOT EDIT.
|
||
|
|
||
|
package ent
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"thesis/ent/blocks"
|
||
|
"thesis/ent/transactions"
|
||
|
"thesis/ent/validators"
|
||
|
|
||
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
||
|
"entgo.io/ent/schema/field"
|
||
|
)
|
||
|
|
||
|
// BlocksCreate is the builder for creating a Blocks entity.
|
||
|
type BlocksCreate struct {
|
||
|
config
|
||
|
mutation *BlocksMutation
|
||
|
hooks []Hook
|
||
|
}
|
||
|
|
||
|
// SetHash sets the "hash" field.
|
||
|
func (bc *BlocksCreate) SetHash(s string) *BlocksCreate {
|
||
|
bc.mutation.SetHash(s)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// SetLength sets the "length" field.
|
||
|
func (bc *BlocksCreate) SetLength(i int) *BlocksCreate {
|
||
|
bc.mutation.SetLength(i)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// SetPreviousHash sets the "previousHash" field.
|
||
|
func (bc *BlocksCreate) SetPreviousHash(s string) *BlocksCreate {
|
||
|
bc.mutation.SetPreviousHash(s)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// SetID sets the "id" field.
|
||
|
func (bc *BlocksCreate) SetID(i int) *BlocksCreate {
|
||
|
bc.mutation.SetID(i)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// AddCasterIDs adds the "Caster" edge to the Validators entity by IDs.
|
||
|
func (bc *BlocksCreate) AddCasterIDs(ids ...int) *BlocksCreate {
|
||
|
bc.mutation.AddCasterIDs(ids...)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// AddCaster adds the "Caster" edges to the Validators entity.
|
||
|
func (bc *BlocksCreate) AddCaster(v ...*Validators) *BlocksCreate {
|
||
|
ids := make([]int, len(v))
|
||
|
for i := range v {
|
||
|
ids[i] = v[i].ID
|
||
|
}
|
||
|
return bc.AddCasterIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// AddMinedTxIDs adds the "MinedTxs" edge to the Transactions entity by IDs.
|
||
|
func (bc *BlocksCreate) AddMinedTxIDs(ids ...int) *BlocksCreate {
|
||
|
bc.mutation.AddMinedTxIDs(ids...)
|
||
|
return bc
|
||
|
}
|
||
|
|
||
|
// AddMinedTxs adds the "MinedTxs" edges to the Transactions entity.
|
||
|
func (bc *BlocksCreate) AddMinedTxs(t ...*Transactions) *BlocksCreate {
|
||
|
ids := make([]int, len(t))
|
||
|
for i := range t {
|
||
|
ids[i] = t[i].ID
|
||
|
}
|
||
|
return bc.AddMinedTxIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Mutation returns the BlocksMutation object of the builder.
|
||
|
func (bc *BlocksCreate) Mutation() *BlocksMutation {
|
||
|
return bc.mutation
|
||
|
}
|
||
|
|
||
|
// Save creates the Blocks in the database.
|
||
|
func (bc *BlocksCreate) Save(ctx context.Context) (*Blocks, error) {
|
||
|
return withHooks(ctx, bc.sqlSave, bc.mutation, bc.hooks)
|
||
|
}
|
||
|
|
||
|
// SaveX calls Save and panics if Save returns an error.
|
||
|
func (bc *BlocksCreate) SaveX(ctx context.Context) *Blocks {
|
||
|
v, err := bc.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (bc *BlocksCreate) Exec(ctx context.Context) error {
|
||
|
_, err := bc.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (bc *BlocksCreate) 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 *BlocksCreate) check() error {
|
||
|
if _, ok := bc.mutation.Hash(); !ok {
|
||
|
return &ValidationError{Name: "hash", err: errors.New(`ent: missing required field "Blocks.hash"`)}
|
||
|
}
|
||
|
if _, ok := bc.mutation.Length(); !ok {
|
||
|
return &ValidationError{Name: "length", err: errors.New(`ent: missing required field "Blocks.length"`)}
|
||
|
}
|
||
|
if _, ok := bc.mutation.PreviousHash(); !ok {
|
||
|
return &ValidationError{Name: "previousHash", err: errors.New(`ent: missing required field "Blocks.previousHash"`)}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (bc *BlocksCreate) sqlSave(ctx context.Context) (*Blocks, 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 *BlocksCreate) createSpec() (*Blocks, *sqlgraph.CreateSpec) {
|
||
|
var (
|
||
|
_node = &Blocks{config: bc.config}
|
||
|
_spec = sqlgraph.NewCreateSpec(blocks.Table, sqlgraph.NewFieldSpec(blocks.FieldID, field.TypeInt))
|
||
|
)
|
||
|
if id, ok := bc.mutation.ID(); ok {
|
||
|
_node.ID = id
|
||
|
_spec.ID.Value = id
|
||
|
}
|
||
|
if value, ok := bc.mutation.Hash(); ok {
|
||
|
_spec.SetField(blocks.FieldHash, field.TypeString, value)
|
||
|
_node.Hash = value
|
||
|
}
|
||
|
if value, ok := bc.mutation.Length(); ok {
|
||
|
_spec.SetField(blocks.FieldLength, field.TypeInt, value)
|
||
|
_node.Length = value
|
||
|
}
|
||
|
if value, ok := bc.mutation.PreviousHash(); ok {
|
||
|
_spec.SetField(blocks.FieldPreviousHash, field.TypeString, value)
|
||
|
_node.PreviousHash = value
|
||
|
}
|
||
|
if nodes := bc.mutation.CasterIDs(); len(nodes) > 0 {
|
||
|
edge := &sqlgraph.EdgeSpec{
|
||
|
Rel: sqlgraph.O2M,
|
||
|
Inverse: false,
|
||
|
Table: blocks.CasterTable,
|
||
|
Columns: []string{blocks.CasterColumn},
|
||
|
Bidi: false,
|
||
|
Target: &sqlgraph.EdgeTarget{
|
||
|
IDSpec: sqlgraph.NewFieldSpec(validators.FieldID, field.TypeInt),
|
||
|
},
|
||
|
}
|
||
|
for _, k := range nodes {
|
||
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||
|
}
|
||
|
_spec.Edges = append(_spec.Edges, edge)
|
||
|
}
|
||
|
if nodes := bc.mutation.MinedTxsIDs(); len(nodes) > 0 {
|
||
|
edge := &sqlgraph.EdgeSpec{
|
||
|
Rel: sqlgraph.M2M,
|
||
|
Inverse: false,
|
||
|
Table: blocks.MinedTxsTable,
|
||
|
Columns: blocks.MinedTxsPrimaryKey,
|
||
|
Bidi: false,
|
||
|
Target: &sqlgraph.EdgeTarget{
|
||
|
IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt),
|
||
|
},
|
||
|
}
|
||
|
for _, k := range nodes {
|
||
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||
|
}
|
||
|
_spec.Edges = append(_spec.Edges, edge)
|
||
|
}
|
||
|
return _node, _spec
|
||
|
}
|
||
|
|
||
|
// BlocksCreateBulk is the builder for creating many Blocks entities in bulk.
|
||
|
type BlocksCreateBulk struct {
|
||
|
config
|
||
|
err error
|
||
|
builders []*BlocksCreate
|
||
|
}
|
||
|
|
||
|
// Save creates the Blocks entities in the database.
|
||
|
func (bcb *BlocksCreateBulk) Save(ctx context.Context) ([]*Blocks, error) {
|
||
|
if bcb.err != nil {
|
||
|
return nil, bcb.err
|
||
|
}
|
||
|
specs := make([]*sqlgraph.CreateSpec, len(bcb.builders))
|
||
|
nodes := make([]*Blocks, 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.(*BlocksMutation)
|
||
|
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, 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 *BlocksCreateBulk) SaveX(ctx context.Context) []*Blocks {
|
||
|
v, err := bcb.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (bcb *BlocksCreateBulk) Exec(ctx context.Context) error {
|
||
|
_, err := bcb.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (bcb *BlocksCreateBulk) ExecX(ctx context.Context) {
|
||
|
if err := bcb.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|