// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/safedep/vet/ent/codesourcefile"
"github.com/safedep/vet/ent/depsusageevidence"
)
// CodeSourceFileCreate is the builder for creating a CodeSourceFile entity.
type CodeSourceFileCreate struct {
config
mutation *CodeSourceFileMutation
hooks []Hook
}
// SetPath sets the "path" field.
func (csfc *CodeSourceFileCreate) SetPath(s string) *CodeSourceFileCreate {
csfc.mutation.SetPath(s)
return csfc
}
// AddDepsUsageEvidenceIDs adds the "deps_usage_evidences" edge to the DepsUsageEvidence entity by IDs.
func (csfc *CodeSourceFileCreate) AddDepsUsageEvidenceIDs(ids ...int) *CodeSourceFileCreate {
csfc.mutation.AddDepsUsageEvidenceIDs(ids...)
return csfc
}
// AddDepsUsageEvidences adds the "deps_usage_evidences" edges to the DepsUsageEvidence entity.
func (csfc *CodeSourceFileCreate) AddDepsUsageEvidences(d ...*DepsUsageEvidence) *CodeSourceFileCreate {
ids := make([]int, len(d))
for i := range d {
ids[i] = d[i].ID
}
return csfc.AddDepsUsageEvidenceIDs(ids...)
}
// Mutation returns the CodeSourceFileMutation object of the builder.
func (csfc *CodeSourceFileCreate) Mutation() *CodeSourceFileMutation {
return csfc.mutation
}
// Save creates the CodeSourceFile in the database.
func (csfc *CodeSourceFileCreate) Save(ctx context.Context) (*CodeSourceFile, error) {
return withHooks(ctx, csfc.sqlSave, csfc.mutation, csfc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (csfc *CodeSourceFileCreate) SaveX(ctx context.Context) *CodeSourceFile {
v, err := csfc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (csfc *CodeSourceFileCreate) Exec(ctx context.Context) error {
_, err := csfc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (csfc *CodeSourceFileCreate) ExecX(ctx context.Context) {
if err := csfc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (csfc *CodeSourceFileCreate) check() error {
if _, ok := csfc.mutation.Path(); !ok {
return &ValidationError{Name: "path", err: errors.New(`ent: missing required field "CodeSourceFile.path"`)}
}
if v, ok := csfc.mutation.Path(); ok {
if err := codesourcefile.PathValidator(v); err != nil {
return &ValidationError{Name: "path", err: fmt.Errorf(`ent: validator failed for field "CodeSourceFile.path": %w`, err)}
}
}
return nil
}
func (csfc *CodeSourceFileCreate) sqlSave(ctx context.Context) (*CodeSourceFile, error) {
if err := csfc.check(); err != nil {
return nil, err
}
_node, _spec := csfc.createSpec()
if err := sqlgraph.CreateNode(ctx, csfc.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)
csfc.mutation.id = &_node.ID
csfc.mutation.done = true
return _node, nil
}
func (csfc *CodeSourceFileCreate) createSpec() (*CodeSourceFile, *sqlgraph.CreateSpec) {
var (
_node = &CodeSourceFile{config: csfc.config}
_spec = sqlgraph.NewCreateSpec(codesourcefile.Table, sqlgraph.NewFieldSpec(codesourcefile.FieldID, field.TypeInt))
)
if value, ok := csfc.mutation.Path(); ok {
_spec.SetField(codesourcefile.FieldPath, field.TypeString, value)
_node.Path = value
}
if nodes := csfc.mutation.DepsUsageEvidencesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: codesourcefile.DepsUsageEvidencesTable,
Columns: []string{codesourcefile.DepsUsageEvidencesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(depsusageevidence.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// CodeSourceFileCreateBulk is the builder for creating many CodeSourceFile entities in bulk.
type CodeSourceFileCreateBulk struct {
config
err error
builders []*CodeSourceFileCreate
}
// Save creates the CodeSourceFile entities in the database.
func (csfcb *CodeSourceFileCreateBulk) Save(ctx context.Context) ([]*CodeSourceFile, error) {
if csfcb.err != nil {
return nil, csfcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(csfcb.builders))
nodes := make([]*CodeSourceFile, len(csfcb.builders))
mutators := make([]Mutator, len(csfcb.builders))
for i := range csfcb.builders {
func(i int, root context.Context) {
builder := csfcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*CodeSourceFileMutation)
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, csfcb.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, csfcb.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, csfcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (csfcb *CodeSourceFileCreateBulk) SaveX(ctx context.Context) []*CodeSourceFile {
v, err := csfcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (csfcb *CodeSourceFileCreateBulk) Exec(ctx context.Context) error {
_, err := csfcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (csfcb *CodeSourceFileCreateBulk) ExecX(ctx context.Context) {
if err := csfcb.Exec(ctx); err != nil {
panic(err)
}
}