// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocationproblem"
	"github.com/buildbarn/bb-portal/ent/gen/ent/build"
	"github.com/buildbarn/bb-portal/ent/gen/ent/eventfile"
	"github.com/buildbarn/bb-portal/ent/gen/ent/metrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/sourcecontrol"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetpair"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testcollection"
	"github.com/buildbarn/bb-portal/pkg/summary"
	"github.com/google/uuid"
)

// BazelInvocationCreate is the builder for creating a BazelInvocation entity.
type BazelInvocationCreate struct {
	config
	mutation *BazelInvocationMutation
	hooks    []Hook
}

// SetInvocationID sets the "invocation_id" field.
func (bic *BazelInvocationCreate) SetInvocationID(u uuid.UUID) *BazelInvocationCreate {
	bic.mutation.SetInvocationID(u)
	return bic
}

// SetStartedAt sets the "started_at" field.
func (bic *BazelInvocationCreate) SetStartedAt(t time.Time) *BazelInvocationCreate {
	bic.mutation.SetStartedAt(t)
	return bic
}

// SetEndedAt sets the "ended_at" field.
func (bic *BazelInvocationCreate) SetEndedAt(t time.Time) *BazelInvocationCreate {
	bic.mutation.SetEndedAt(t)
	return bic
}

// SetNillableEndedAt sets the "ended_at" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableEndedAt(t *time.Time) *BazelInvocationCreate {
	if t != nil {
		bic.SetEndedAt(*t)
	}
	return bic
}

// SetChangeNumber sets the "change_number" field.
func (bic *BazelInvocationCreate) SetChangeNumber(i int) *BazelInvocationCreate {
	bic.mutation.SetChangeNumber(i)
	return bic
}

// SetNillableChangeNumber sets the "change_number" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableChangeNumber(i *int) *BazelInvocationCreate {
	if i != nil {
		bic.SetChangeNumber(*i)
	}
	return bic
}

// SetPatchsetNumber sets the "patchset_number" field.
func (bic *BazelInvocationCreate) SetPatchsetNumber(i int) *BazelInvocationCreate {
	bic.mutation.SetPatchsetNumber(i)
	return bic
}

// SetNillablePatchsetNumber sets the "patchset_number" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillablePatchsetNumber(i *int) *BazelInvocationCreate {
	if i != nil {
		bic.SetPatchsetNumber(*i)
	}
	return bic
}

// SetSummary sets the "summary" field.
func (bic *BazelInvocationCreate) SetSummary(ss summary.InvocationSummary) *BazelInvocationCreate {
	bic.mutation.SetSummary(ss)
	return bic
}

// SetBepCompleted sets the "bep_completed" field.
func (bic *BazelInvocationCreate) SetBepCompleted(b bool) *BazelInvocationCreate {
	bic.mutation.SetBepCompleted(b)
	return bic
}

// SetNillableBepCompleted sets the "bep_completed" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableBepCompleted(b *bool) *BazelInvocationCreate {
	if b != nil {
		bic.SetBepCompleted(*b)
	}
	return bic
}

// SetStepLabel sets the "step_label" field.
func (bic *BazelInvocationCreate) SetStepLabel(s string) *BazelInvocationCreate {
	bic.mutation.SetStepLabel(s)
	return bic
}

// SetRelatedFiles sets the "related_files" field.
func (bic *BazelInvocationCreate) SetRelatedFiles(m map[string]string) *BazelInvocationCreate {
	bic.mutation.SetRelatedFiles(m)
	return bic
}

// SetUserEmail sets the "user_email" field.
func (bic *BazelInvocationCreate) SetUserEmail(s string) *BazelInvocationCreate {
	bic.mutation.SetUserEmail(s)
	return bic
}

// SetNillableUserEmail sets the "user_email" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableUserEmail(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetUserEmail(*s)
	}
	return bic
}

// SetUserLdap sets the "user_ldap" field.
func (bic *BazelInvocationCreate) SetUserLdap(s string) *BazelInvocationCreate {
	bic.mutation.SetUserLdap(s)
	return bic
}

// SetNillableUserLdap sets the "user_ldap" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableUserLdap(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetUserLdap(*s)
	}
	return bic
}

// SetBuildLogs sets the "build_logs" field.
func (bic *BazelInvocationCreate) SetBuildLogs(s string) *BazelInvocationCreate {
	bic.mutation.SetBuildLogs(s)
	return bic
}

// SetNillableBuildLogs sets the "build_logs" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableBuildLogs(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetBuildLogs(*s)
	}
	return bic
}

// SetCPU sets the "cpu" field.
func (bic *BazelInvocationCreate) SetCPU(s string) *BazelInvocationCreate {
	bic.mutation.SetCPU(s)
	return bic
}

// SetNillableCPU sets the "cpu" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableCPU(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetCPU(*s)
	}
	return bic
}

// SetPlatformName sets the "platform_name" field.
func (bic *BazelInvocationCreate) SetPlatformName(s string) *BazelInvocationCreate {
	bic.mutation.SetPlatformName(s)
	return bic
}

// SetNillablePlatformName sets the "platform_name" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillablePlatformName(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetPlatformName(*s)
	}
	return bic
}

// SetHostname sets the "hostname" field.
func (bic *BazelInvocationCreate) SetHostname(s string) *BazelInvocationCreate {
	bic.mutation.SetHostname(s)
	return bic
}

// SetNillableHostname sets the "hostname" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableHostname(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetHostname(*s)
	}
	return bic
}

// SetIsCiWorker sets the "is_ci_worker" field.
func (bic *BazelInvocationCreate) SetIsCiWorker(b bool) *BazelInvocationCreate {
	bic.mutation.SetIsCiWorker(b)
	return bic
}

// SetNillableIsCiWorker sets the "is_ci_worker" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableIsCiWorker(b *bool) *BazelInvocationCreate {
	if b != nil {
		bic.SetIsCiWorker(*b)
	}
	return bic
}

// SetConfigurationMnemonic sets the "configuration_mnemonic" field.
func (bic *BazelInvocationCreate) SetConfigurationMnemonic(s string) *BazelInvocationCreate {
	bic.mutation.SetConfigurationMnemonic(s)
	return bic
}

// SetNillableConfigurationMnemonic sets the "configuration_mnemonic" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableConfigurationMnemonic(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetConfigurationMnemonic(*s)
	}
	return bic
}

// SetNumFetches sets the "num_fetches" field.
func (bic *BazelInvocationCreate) SetNumFetches(i int64) *BazelInvocationCreate {
	bic.mutation.SetNumFetches(i)
	return bic
}

// SetNillableNumFetches sets the "num_fetches" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableNumFetches(i *int64) *BazelInvocationCreate {
	if i != nil {
		bic.SetNumFetches(*i)
	}
	return bic
}

// SetProfileName sets the "profile_name" field.
func (bic *BazelInvocationCreate) SetProfileName(s string) *BazelInvocationCreate {
	bic.mutation.SetProfileName(s)
	return bic
}

// SetInstanceName sets the "instance_name" field.
func (bic *BazelInvocationCreate) SetInstanceName(s string) *BazelInvocationCreate {
	bic.mutation.SetInstanceName(s)
	return bic
}

// SetNillableInstanceName sets the "instance_name" field if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableInstanceName(s *string) *BazelInvocationCreate {
	if s != nil {
		bic.SetInstanceName(*s)
	}
	return bic
}

// SetEventFileID sets the "event_file" edge to the EventFile entity by ID.
func (bic *BazelInvocationCreate) SetEventFileID(id int) *BazelInvocationCreate {
	bic.mutation.SetEventFileID(id)
	return bic
}

// SetEventFile sets the "event_file" edge to the EventFile entity.
func (bic *BazelInvocationCreate) SetEventFile(e *EventFile) *BazelInvocationCreate {
	return bic.SetEventFileID(e.ID)
}

// SetBuildID sets the "build" edge to the Build entity by ID.
func (bic *BazelInvocationCreate) SetBuildID(id int) *BazelInvocationCreate {
	bic.mutation.SetBuildID(id)
	return bic
}

// SetNillableBuildID sets the "build" edge to the Build entity by ID if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableBuildID(id *int) *BazelInvocationCreate {
	if id != nil {
		bic = bic.SetBuildID(*id)
	}
	return bic
}

// SetBuild sets the "build" edge to the Build entity.
func (bic *BazelInvocationCreate) SetBuild(b *Build) *BazelInvocationCreate {
	return bic.SetBuildID(b.ID)
}

// AddProblemIDs adds the "problems" edge to the BazelInvocationProblem entity by IDs.
func (bic *BazelInvocationCreate) AddProblemIDs(ids ...int) *BazelInvocationCreate {
	bic.mutation.AddProblemIDs(ids...)
	return bic
}

// AddProblems adds the "problems" edges to the BazelInvocationProblem entity.
func (bic *BazelInvocationCreate) AddProblems(b ...*BazelInvocationProblem) *BazelInvocationCreate {
	ids := make([]int, len(b))
	for i := range b {
		ids[i] = b[i].ID
	}
	return bic.AddProblemIDs(ids...)
}

// SetMetricsID sets the "metrics" edge to the Metrics entity by ID.
func (bic *BazelInvocationCreate) SetMetricsID(id int) *BazelInvocationCreate {
	bic.mutation.SetMetricsID(id)
	return bic
}

// SetNillableMetricsID sets the "metrics" edge to the Metrics entity by ID if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableMetricsID(id *int) *BazelInvocationCreate {
	if id != nil {
		bic = bic.SetMetricsID(*id)
	}
	return bic
}

// SetMetrics sets the "metrics" edge to the Metrics entity.
func (bic *BazelInvocationCreate) SetMetrics(m *Metrics) *BazelInvocationCreate {
	return bic.SetMetricsID(m.ID)
}

// AddTestCollectionIDs adds the "test_collection" edge to the TestCollection entity by IDs.
func (bic *BazelInvocationCreate) AddTestCollectionIDs(ids ...int) *BazelInvocationCreate {
	bic.mutation.AddTestCollectionIDs(ids...)
	return bic
}

// AddTestCollection adds the "test_collection" edges to the TestCollection entity.
func (bic *BazelInvocationCreate) AddTestCollection(t ...*TestCollection) *BazelInvocationCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return bic.AddTestCollectionIDs(ids...)
}

// AddTargetIDs adds the "targets" edge to the TargetPair entity by IDs.
func (bic *BazelInvocationCreate) AddTargetIDs(ids ...int) *BazelInvocationCreate {
	bic.mutation.AddTargetIDs(ids...)
	return bic
}

// AddTargets adds the "targets" edges to the TargetPair entity.
func (bic *BazelInvocationCreate) AddTargets(t ...*TargetPair) *BazelInvocationCreate {
	ids := make([]int, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return bic.AddTargetIDs(ids...)
}

// SetSourceControlID sets the "source_control" edge to the SourceControl entity by ID.
func (bic *BazelInvocationCreate) SetSourceControlID(id int) *BazelInvocationCreate {
	bic.mutation.SetSourceControlID(id)
	return bic
}

// SetNillableSourceControlID sets the "source_control" edge to the SourceControl entity by ID if the given value is not nil.
func (bic *BazelInvocationCreate) SetNillableSourceControlID(id *int) *BazelInvocationCreate {
	if id != nil {
		bic = bic.SetSourceControlID(*id)
	}
	return bic
}

// SetSourceControl sets the "source_control" edge to the SourceControl entity.
func (bic *BazelInvocationCreate) SetSourceControl(s *SourceControl) *BazelInvocationCreate {
	return bic.SetSourceControlID(s.ID)
}

// Mutation returns the BazelInvocationMutation object of the builder.
func (bic *BazelInvocationCreate) Mutation() *BazelInvocationMutation {
	return bic.mutation
}

// Save creates the BazelInvocation in the database.
func (bic *BazelInvocationCreate) Save(ctx context.Context) (*BazelInvocation, error) {
	return withHooks(ctx, bic.sqlSave, bic.mutation, bic.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (bic *BazelInvocationCreate) SaveX(ctx context.Context) *BazelInvocation {
	v, err := bic.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (bic *BazelInvocationCreate) Exec(ctx context.Context) error {
	_, err := bic.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (bic *BazelInvocationCreate) ExecX(ctx context.Context) {
	if err := bic.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (bic *BazelInvocationCreate) check() error {
	if _, ok := bic.mutation.InvocationID(); !ok {
		return &ValidationError{Name: "invocation_id", err: errors.New(`ent: missing required field "BazelInvocation.invocation_id"`)}
	}
	if _, ok := bic.mutation.StartedAt(); !ok {
		return &ValidationError{Name: "started_at", err: errors.New(`ent: missing required field "BazelInvocation.started_at"`)}
	}
	if _, ok := bic.mutation.Summary(); !ok {
		return &ValidationError{Name: "summary", err: errors.New(`ent: missing required field "BazelInvocation.summary"`)}
	}
	if _, ok := bic.mutation.StepLabel(); !ok {
		return &ValidationError{Name: "step_label", err: errors.New(`ent: missing required field "BazelInvocation.step_label"`)}
	}
	if _, ok := bic.mutation.RelatedFiles(); !ok {
		return &ValidationError{Name: "related_files", err: errors.New(`ent: missing required field "BazelInvocation.related_files"`)}
	}
	if _, ok := bic.mutation.ProfileName(); !ok {
		return &ValidationError{Name: "profile_name", err: errors.New(`ent: missing required field "BazelInvocation.profile_name"`)}
	}
	if len(bic.mutation.EventFileIDs()) == 0 {
		return &ValidationError{Name: "event_file", err: errors.New(`ent: missing required edge "BazelInvocation.event_file"`)}
	}
	return nil
}

func (bic *BazelInvocationCreate) sqlSave(ctx context.Context) (*BazelInvocation, error) {
	if err := bic.check(); err != nil {
		return nil, err
	}
	_node, _spec := bic.createSpec()
	if err := sqlgraph.CreateNode(ctx, bic.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)
	bic.mutation.id = &_node.ID
	bic.mutation.done = true
	return _node, nil
}

func (bic *BazelInvocationCreate) createSpec() (*BazelInvocation, *sqlgraph.CreateSpec) {
	var (
		_node = &BazelInvocation{config: bic.config}
		_spec = sqlgraph.NewCreateSpec(bazelinvocation.Table, sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt))
	)
	if value, ok := bic.mutation.InvocationID(); ok {
		_spec.SetField(bazelinvocation.FieldInvocationID, field.TypeUUID, value)
		_node.InvocationID = value
	}
	if value, ok := bic.mutation.StartedAt(); ok {
		_spec.SetField(bazelinvocation.FieldStartedAt, field.TypeTime, value)
		_node.StartedAt = value
	}
	if value, ok := bic.mutation.EndedAt(); ok {
		_spec.SetField(bazelinvocation.FieldEndedAt, field.TypeTime, value)
		_node.EndedAt = value
	}
	if value, ok := bic.mutation.ChangeNumber(); ok {
		_spec.SetField(bazelinvocation.FieldChangeNumber, field.TypeInt, value)
		_node.ChangeNumber = value
	}
	if value, ok := bic.mutation.PatchsetNumber(); ok {
		_spec.SetField(bazelinvocation.FieldPatchsetNumber, field.TypeInt, value)
		_node.PatchsetNumber = value
	}
	if value, ok := bic.mutation.Summary(); ok {
		_spec.SetField(bazelinvocation.FieldSummary, field.TypeJSON, value)
		_node.Summary = value
	}
	if value, ok := bic.mutation.BepCompleted(); ok {
		_spec.SetField(bazelinvocation.FieldBepCompleted, field.TypeBool, value)
		_node.BepCompleted = value
	}
	if value, ok := bic.mutation.StepLabel(); ok {
		_spec.SetField(bazelinvocation.FieldStepLabel, field.TypeString, value)
		_node.StepLabel = value
	}
	if value, ok := bic.mutation.RelatedFiles(); ok {
		_spec.SetField(bazelinvocation.FieldRelatedFiles, field.TypeJSON, value)
		_node.RelatedFiles = value
	}
	if value, ok := bic.mutation.UserEmail(); ok {
		_spec.SetField(bazelinvocation.FieldUserEmail, field.TypeString, value)
		_node.UserEmail = value
	}
	if value, ok := bic.mutation.UserLdap(); ok {
		_spec.SetField(bazelinvocation.FieldUserLdap, field.TypeString, value)
		_node.UserLdap = value
	}
	if value, ok := bic.mutation.BuildLogs(); ok {
		_spec.SetField(bazelinvocation.FieldBuildLogs, field.TypeString, value)
		_node.BuildLogs = value
	}
	if value, ok := bic.mutation.CPU(); ok {
		_spec.SetField(bazelinvocation.FieldCPU, field.TypeString, value)
		_node.CPU = value
	}
	if value, ok := bic.mutation.PlatformName(); ok {
		_spec.SetField(bazelinvocation.FieldPlatformName, field.TypeString, value)
		_node.PlatformName = value
	}
	if value, ok := bic.mutation.Hostname(); ok {
		_spec.SetField(bazelinvocation.FieldHostname, field.TypeString, value)
		_node.Hostname = value
	}
	if value, ok := bic.mutation.IsCiWorker(); ok {
		_spec.SetField(bazelinvocation.FieldIsCiWorker, field.TypeBool, value)
		_node.IsCiWorker = value
	}
	if value, ok := bic.mutation.ConfigurationMnemonic(); ok {
		_spec.SetField(bazelinvocation.FieldConfigurationMnemonic, field.TypeString, value)
		_node.ConfigurationMnemonic = value
	}
	if value, ok := bic.mutation.NumFetches(); ok {
		_spec.SetField(bazelinvocation.FieldNumFetches, field.TypeInt64, value)
		_node.NumFetches = value
	}
	if value, ok := bic.mutation.ProfileName(); ok {
		_spec.SetField(bazelinvocation.FieldProfileName, field.TypeString, value)
		_node.ProfileName = value
	}
	if value, ok := bic.mutation.InstanceName(); ok {
		_spec.SetField(bazelinvocation.FieldInstanceName, field.TypeString, value)
		_node.InstanceName = value
	}
	if nodes := bic.mutation.EventFileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   bazelinvocation.EventFileTable,
			Columns: []string{bazelinvocation.EventFileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(eventfile.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.event_file_bazel_invocation = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.BuildIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   bazelinvocation.BuildTable,
			Columns: []string{bazelinvocation.BuildColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(build.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.build_invocations = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.ProblemsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   bazelinvocation.ProblemsTable,
			Columns: []string{bazelinvocation.ProblemsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocationproblem.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.MetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   bazelinvocation.MetricsTable,
			Columns: []string{bazelinvocation.MetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.TestCollectionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   bazelinvocation.TestCollectionTable,
			Columns: []string{bazelinvocation.TestCollectionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(testcollection.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.TargetsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   bazelinvocation.TargetsTable,
			Columns: []string{bazelinvocation.TargetsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(targetpair.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := bic.mutation.SourceControlIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   bazelinvocation.SourceControlTable,
			Columns: []string{bazelinvocation.SourceControlColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sourcecontrol.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// BazelInvocationCreateBulk is the builder for creating many BazelInvocation entities in bulk.
type BazelInvocationCreateBulk struct {
	config
	err      error
	builders []*BazelInvocationCreate
}

// Save creates the BazelInvocation entities in the database.
func (bicb *BazelInvocationCreateBulk) Save(ctx context.Context) ([]*BazelInvocation, error) {
	if bicb.err != nil {
		return nil, bicb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(bicb.builders))
	nodes := make([]*BazelInvocation, len(bicb.builders))
	mutators := make([]Mutator, len(bicb.builders))
	for i := range bicb.builders {
		func(i int, root context.Context) {
			builder := bicb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*BazelInvocationMutation)
				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, bicb.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, bicb.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, bicb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (bicb *BazelInvocationCreateBulk) SaveX(ctx context.Context) []*BazelInvocation {
	v, err := bicb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (bicb *BazelInvocationCreateBulk) Exec(ctx context.Context) error {
	_, err := bicb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (bicb *BazelInvocationCreateBulk) ExecX(ctx context.Context) {
	if err := bicb.Exec(ctx); err != nil {
		panic(err)
	}
}
