// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/directlink"
	"github.com/anzhiyu-c/anheyu-app/ent/entity"
	"github.com/anzhiyu-c/anheyu-app/ent/file"
	"github.com/anzhiyu-c/anheyu-app/ent/fileentity"
	"github.com/anzhiyu-c/anheyu-app/ent/metadata"
	"github.com/anzhiyu-c/anheyu-app/ent/user"
)

// FileCreate is the builder for creating a File entity.
type FileCreate struct {
	config
	mutation *FileMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetDeletedAt sets the "deleted_at" field.
func (_c *FileCreate) SetDeletedAt(v time.Time) *FileCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *FileCreate) SetNillableDeletedAt(v *time.Time) *FileCreate {
	if v != nil {
		_c.SetDeletedAt(*v)
	}
	return _c
}

// SetCreatedAt sets the "created_at" field.
func (_c *FileCreate) SetCreatedAt(v time.Time) *FileCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *FileCreate) SetNillableCreatedAt(v *time.Time) *FileCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

// SetUpdatedAt sets the "updated_at" field.
func (_c *FileCreate) SetUpdatedAt(v time.Time) *FileCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *FileCreate) SetNillableUpdatedAt(v *time.Time) *FileCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

// SetType sets the "type" field.
func (_c *FileCreate) SetType(v int) *FileCreate {
	_c.mutation.SetType(v)
	return _c
}

// SetOwnerID sets the "owner_id" field.
func (_c *FileCreate) SetOwnerID(v uint) *FileCreate {
	_c.mutation.SetOwnerID(v)
	return _c
}

// SetParentID sets the "parent_id" field.
func (_c *FileCreate) SetParentID(v uint) *FileCreate {
	_c.mutation.SetParentID(v)
	return _c
}

// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (_c *FileCreate) SetNillableParentID(v *uint) *FileCreate {
	if v != nil {
		_c.SetParentID(*v)
	}
	return _c
}

// SetName sets the "name" field.
func (_c *FileCreate) SetName(v string) *FileCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetSize sets the "size" field.
func (_c *FileCreate) SetSize(v int64) *FileCreate {
	_c.mutation.SetSize(v)
	return _c
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_c *FileCreate) SetNillableSize(v *int64) *FileCreate {
	if v != nil {
		_c.SetSize(*v)
	}
	return _c
}

// SetPrimaryEntityID sets the "primary_entity_id" field.
func (_c *FileCreate) SetPrimaryEntityID(v uint) *FileCreate {
	_c.mutation.SetPrimaryEntityID(v)
	return _c
}

// SetNillablePrimaryEntityID sets the "primary_entity_id" field if the given value is not nil.
func (_c *FileCreate) SetNillablePrimaryEntityID(v *uint) *FileCreate {
	if v != nil {
		_c.SetPrimaryEntityID(*v)
	}
	return _c
}

// SetChildrenCount sets the "children_count" field.
func (_c *FileCreate) SetChildrenCount(v int64) *FileCreate {
	_c.mutation.SetChildrenCount(v)
	return _c
}

// SetNillableChildrenCount sets the "children_count" field if the given value is not nil.
func (_c *FileCreate) SetNillableChildrenCount(v *int64) *FileCreate {
	if v != nil {
		_c.SetChildrenCount(*v)
	}
	return _c
}

// SetViewConfig sets the "view_config" field.
func (_c *FileCreate) SetViewConfig(v string) *FileCreate {
	_c.mutation.SetViewConfig(v)
	return _c
}

// SetNillableViewConfig sets the "view_config" field if the given value is not nil.
func (_c *FileCreate) SetNillableViewConfig(v *string) *FileCreate {
	if v != nil {
		_c.SetViewConfig(*v)
	}
	return _c
}

// SetID sets the "id" field.
func (_c *FileCreate) SetID(v uint) *FileCreate {
	_c.mutation.SetID(v)
	return _c
}

// SetOwner sets the "owner" edge to the User entity.
func (_c *FileCreate) SetOwner(v *User) *FileCreate {
	return _c.SetOwnerID(v.ID)
}

// SetParent sets the "parent" edge to the File entity.
func (_c *FileCreate) SetParent(v *File) *FileCreate {
	return _c.SetParentID(v.ID)
}

// AddChildIDs adds the "children" edge to the File entity by IDs.
func (_c *FileCreate) AddChildIDs(ids ...uint) *FileCreate {
	_c.mutation.AddChildIDs(ids...)
	return _c
}

// AddChildren adds the "children" edges to the File entity.
func (_c *FileCreate) AddChildren(v ...*File) *FileCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddChildIDs(ids...)
}

// SetPrimaryEntity sets the "primary_entity" edge to the Entity entity.
func (_c *FileCreate) SetPrimaryEntity(v *Entity) *FileCreate {
	return _c.SetPrimaryEntityID(v.ID)
}

// AddVersionIDs adds the "versions" edge to the FileEntity entity by IDs.
func (_c *FileCreate) AddVersionIDs(ids ...uint) *FileCreate {
	_c.mutation.AddVersionIDs(ids...)
	return _c
}

// AddVersions adds the "versions" edges to the FileEntity entity.
func (_c *FileCreate) AddVersions(v ...*FileEntity) *FileCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddVersionIDs(ids...)
}

// SetDirectLinkID sets the "direct_link" edge to the DirectLink entity by ID.
func (_c *FileCreate) SetDirectLinkID(id uint) *FileCreate {
	_c.mutation.SetDirectLinkID(id)
	return _c
}

// SetNillableDirectLinkID sets the "direct_link" edge to the DirectLink entity by ID if the given value is not nil.
func (_c *FileCreate) SetNillableDirectLinkID(id *uint) *FileCreate {
	if id != nil {
		_c = _c.SetDirectLinkID(*id)
	}
	return _c
}

// SetDirectLink sets the "direct_link" edge to the DirectLink entity.
func (_c *FileCreate) SetDirectLink(v *DirectLink) *FileCreate {
	return _c.SetDirectLinkID(v.ID)
}

// AddMetadatumIDs adds the "metadata" edge to the Metadata entity by IDs.
func (_c *FileCreate) AddMetadatumIDs(ids ...uint) *FileCreate {
	_c.mutation.AddMetadatumIDs(ids...)
	return _c
}

// AddMetadata adds the "metadata" edges to the Metadata entity.
func (_c *FileCreate) AddMetadata(v ...*Metadata) *FileCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddMetadatumIDs(ids...)
}

// Mutation returns the FileMutation object of the builder.
func (_c *FileCreate) Mutation() *FileMutation {
	return _c.mutation
}

// Save creates the File in the database.
func (_c *FileCreate) Save(ctx context.Context) (*File, error) {
	if err := _c.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_c *FileCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if file.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized file.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := file.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if file.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized file.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := file.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.Size(); !ok {
		v := file.DefaultSize
		_c.mutation.SetSize(v)
	}
	if _, ok := _c.mutation.ChildrenCount(); !ok {
		v := file.DefaultChildrenCount
		_c.mutation.SetChildrenCount(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *FileCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "File.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "File.updated_at"`)}
	}
	if _, ok := _c.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "File.type"`)}
	}
	if _, ok := _c.mutation.OwnerID(); !ok {
		return &ValidationError{Name: "owner_id", err: errors.New(`ent: missing required field "File.owner_id"`)}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "File.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := file.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "File.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Size(); !ok {
		return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "File.size"`)}
	}
	if _, ok := _c.mutation.ChildrenCount(); !ok {
		return &ValidationError{Name: "children_count", err: errors.New(`ent: missing required field "File.children_count"`)}
	}
	if len(_c.mutation.OwnerIDs()) == 0 {
		return &ValidationError{Name: "owner", err: errors.New(`ent: missing required edge "File.owner"`)}
	}
	return nil
}

func (_c *FileCreate) sqlSave(ctx context.Context) (*File, 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
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *FileCreate) createSpec() (*File, *sqlgraph.CreateSpec) {
	var (
		_node = &File{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(file.Table, sqlgraph.NewFieldSpec(file.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(file.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(file.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(file.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.GetType(); ok {
		_spec.SetField(file.FieldType, field.TypeInt, value)
		_node.Type = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(file.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Size(); ok {
		_spec.SetField(file.FieldSize, field.TypeInt64, value)
		_node.Size = value
	}
	if value, ok := _c.mutation.ChildrenCount(); ok {
		_spec.SetField(file.FieldChildrenCount, field.TypeInt64, value)
		_node.ChildrenCount = value
	}
	if value, ok := _c.mutation.ViewConfig(); ok {
		_spec.SetField(file.FieldViewConfig, field.TypeString, value)
		_node.ViewConfig = &value
	}
	if nodes := _c.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   file.OwnerTable,
			Columns: []string{file.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.OwnerID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.ParentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   file.ParentTable,
			Columns: []string{file.ParentColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.ParentID = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.ChildrenIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   file.ChildrenTable,
			Columns: []string{file.ChildrenColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.PrimaryEntityIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   file.PrimaryEntityTable,
			Columns: []string{file.PrimaryEntityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.PrimaryEntityID = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.VersionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   file.VersionsTable,
			Columns: []string{file.VersionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(fileentity.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.DirectLinkIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   file.DirectLinkTable,
			Columns: []string{file.DirectLinkColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(directlink.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.MetadataIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   file.MetadataTable,
			Columns: []string{file.MetadataColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(metadata.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.File.Create().
//		SetDeletedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.FileUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *FileCreate) OnConflict(opts ...sql.ConflictOption) *FileUpsertOne {
	_c.conflict = opts
	return &FileUpsertOne{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.File.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *FileCreate) OnConflictColumns(columns ...string) *FileUpsertOne {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &FileUpsertOne{
		create: _c,
	}
}

type (
	// FileUpsertOne is the builder for "upsert"-ing
	//  one File node.
	FileUpsertOne struct {
		create *FileCreate
	}

	// FileUpsert is the "OnConflict" setter.
	FileUpsert struct {
		*sql.UpdateSet
	}
)

// SetDeletedAt sets the "deleted_at" field.
func (u *FileUpsert) SetDeletedAt(v time.Time) *FileUpsert {
	u.Set(file.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *FileUpsert) UpdateDeletedAt() *FileUpsert {
	u.SetExcluded(file.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *FileUpsert) ClearDeletedAt() *FileUpsert {
	u.SetNull(file.FieldDeletedAt)
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *FileUpsert) SetUpdatedAt(v time.Time) *FileUpsert {
	u.Set(file.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *FileUpsert) UpdateUpdatedAt() *FileUpsert {
	u.SetExcluded(file.FieldUpdatedAt)
	return u
}

// SetType sets the "type" field.
func (u *FileUpsert) SetType(v int) *FileUpsert {
	u.Set(file.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *FileUpsert) UpdateType() *FileUpsert {
	u.SetExcluded(file.FieldType)
	return u
}

// AddType adds v to the "type" field.
func (u *FileUpsert) AddType(v int) *FileUpsert {
	u.Add(file.FieldType, v)
	return u
}

// SetOwnerID sets the "owner_id" field.
func (u *FileUpsert) SetOwnerID(v uint) *FileUpsert {
	u.Set(file.FieldOwnerID, v)
	return u
}

// UpdateOwnerID sets the "owner_id" field to the value that was provided on create.
func (u *FileUpsert) UpdateOwnerID() *FileUpsert {
	u.SetExcluded(file.FieldOwnerID)
	return u
}

// SetParentID sets the "parent_id" field.
func (u *FileUpsert) SetParentID(v uint) *FileUpsert {
	u.Set(file.FieldParentID, v)
	return u
}

// UpdateParentID sets the "parent_id" field to the value that was provided on create.
func (u *FileUpsert) UpdateParentID() *FileUpsert {
	u.SetExcluded(file.FieldParentID)
	return u
}

// ClearParentID clears the value of the "parent_id" field.
func (u *FileUpsert) ClearParentID() *FileUpsert {
	u.SetNull(file.FieldParentID)
	return u
}

// SetName sets the "name" field.
func (u *FileUpsert) SetName(v string) *FileUpsert {
	u.Set(file.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *FileUpsert) UpdateName() *FileUpsert {
	u.SetExcluded(file.FieldName)
	return u
}

// SetSize sets the "size" field.
func (u *FileUpsert) SetSize(v int64) *FileUpsert {
	u.Set(file.FieldSize, v)
	return u
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *FileUpsert) UpdateSize() *FileUpsert {
	u.SetExcluded(file.FieldSize)
	return u
}

// AddSize adds v to the "size" field.
func (u *FileUpsert) AddSize(v int64) *FileUpsert {
	u.Add(file.FieldSize, v)
	return u
}

// SetPrimaryEntityID sets the "primary_entity_id" field.
func (u *FileUpsert) SetPrimaryEntityID(v uint) *FileUpsert {
	u.Set(file.FieldPrimaryEntityID, v)
	return u
}

// UpdatePrimaryEntityID sets the "primary_entity_id" field to the value that was provided on create.
func (u *FileUpsert) UpdatePrimaryEntityID() *FileUpsert {
	u.SetExcluded(file.FieldPrimaryEntityID)
	return u
}

// ClearPrimaryEntityID clears the value of the "primary_entity_id" field.
func (u *FileUpsert) ClearPrimaryEntityID() *FileUpsert {
	u.SetNull(file.FieldPrimaryEntityID)
	return u
}

// SetChildrenCount sets the "children_count" field.
func (u *FileUpsert) SetChildrenCount(v int64) *FileUpsert {
	u.Set(file.FieldChildrenCount, v)
	return u
}

// UpdateChildrenCount sets the "children_count" field to the value that was provided on create.
func (u *FileUpsert) UpdateChildrenCount() *FileUpsert {
	u.SetExcluded(file.FieldChildrenCount)
	return u
}

// AddChildrenCount adds v to the "children_count" field.
func (u *FileUpsert) AddChildrenCount(v int64) *FileUpsert {
	u.Add(file.FieldChildrenCount, v)
	return u
}

// SetViewConfig sets the "view_config" field.
func (u *FileUpsert) SetViewConfig(v string) *FileUpsert {
	u.Set(file.FieldViewConfig, v)
	return u
}

// UpdateViewConfig sets the "view_config" field to the value that was provided on create.
func (u *FileUpsert) UpdateViewConfig() *FileUpsert {
	u.SetExcluded(file.FieldViewConfig)
	return u
}

// ClearViewConfig clears the value of the "view_config" field.
func (u *FileUpsert) ClearViewConfig() *FileUpsert {
	u.SetNull(file.FieldViewConfig)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.File.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(file.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *FileUpsertOne) UpdateNewValues() *FileUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(file.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(file.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.File.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *FileUpsertOne) Ignore() *FileUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *FileUpsertOne) DoNothing() *FileUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the FileCreate.OnConflict
// documentation for more info.
func (u *FileUpsertOne) Update(set func(*FileUpsert)) *FileUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&FileUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *FileUpsertOne) SetDeletedAt(v time.Time) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateDeletedAt() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *FileUpsertOne) ClearDeletedAt() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *FileUpsertOne) SetUpdatedAt(v time.Time) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateUpdatedAt() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetType sets the "type" field.
func (u *FileUpsertOne) SetType(v int) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *FileUpsertOne) AddType(v int) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.AddType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateType() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateType()
	})
}

// SetOwnerID sets the "owner_id" field.
func (u *FileUpsertOne) SetOwnerID(v uint) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetOwnerID(v)
	})
}

// UpdateOwnerID sets the "owner_id" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateOwnerID() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateOwnerID()
	})
}

// SetParentID sets the "parent_id" field.
func (u *FileUpsertOne) SetParentID(v uint) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetParentID(v)
	})
}

// UpdateParentID sets the "parent_id" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateParentID() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateParentID()
	})
}

// ClearParentID clears the value of the "parent_id" field.
func (u *FileUpsertOne) ClearParentID() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.ClearParentID()
	})
}

// SetName sets the "name" field.
func (u *FileUpsertOne) SetName(v string) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateName() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateName()
	})
}

// SetSize sets the "size" field.
func (u *FileUpsertOne) SetSize(v int64) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetSize(v)
	})
}

// AddSize adds v to the "size" field.
func (u *FileUpsertOne) AddSize(v int64) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.AddSize(v)
	})
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateSize() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateSize()
	})
}

// SetPrimaryEntityID sets the "primary_entity_id" field.
func (u *FileUpsertOne) SetPrimaryEntityID(v uint) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetPrimaryEntityID(v)
	})
}

// UpdatePrimaryEntityID sets the "primary_entity_id" field to the value that was provided on create.
func (u *FileUpsertOne) UpdatePrimaryEntityID() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdatePrimaryEntityID()
	})
}

// ClearPrimaryEntityID clears the value of the "primary_entity_id" field.
func (u *FileUpsertOne) ClearPrimaryEntityID() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.ClearPrimaryEntityID()
	})
}

// SetChildrenCount sets the "children_count" field.
func (u *FileUpsertOne) SetChildrenCount(v int64) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetChildrenCount(v)
	})
}

// AddChildrenCount adds v to the "children_count" field.
func (u *FileUpsertOne) AddChildrenCount(v int64) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.AddChildrenCount(v)
	})
}

// UpdateChildrenCount sets the "children_count" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateChildrenCount() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateChildrenCount()
	})
}

// SetViewConfig sets the "view_config" field.
func (u *FileUpsertOne) SetViewConfig(v string) *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.SetViewConfig(v)
	})
}

// UpdateViewConfig sets the "view_config" field to the value that was provided on create.
func (u *FileUpsertOne) UpdateViewConfig() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.UpdateViewConfig()
	})
}

// ClearViewConfig clears the value of the "view_config" field.
func (u *FileUpsertOne) ClearViewConfig() *FileUpsertOne {
	return u.Update(func(s *FileUpsert) {
		s.ClearViewConfig()
	})
}

// Exec executes the query.
func (u *FileUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for FileCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *FileUpsertOne) ID(ctx context.Context) (id uint, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *FileUpsertOne) IDX(ctx context.Context) uint {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// FileCreateBulk is the builder for creating many File entities in bulk.
type FileCreateBulk struct {
	config
	err      error
	builders []*FileCreate
	conflict []sql.ConflictOption
}

// Save creates the File entities in the database.
func (_c *FileCreateBulk) Save(ctx context.Context) ([]*File, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*File, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*FileMutation)
				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}
					spec.OnConflict = _c.conflict
					// 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 && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint(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 *FileCreateBulk) SaveX(ctx context.Context) []*File {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.File.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.FileUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *FileCreateBulk) OnConflict(opts ...sql.ConflictOption) *FileUpsertBulk {
	_c.conflict = opts
	return &FileUpsertBulk{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.File.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *FileCreateBulk) OnConflictColumns(columns ...string) *FileUpsertBulk {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &FileUpsertBulk{
		create: _c,
	}
}

// FileUpsertBulk is the builder for "upsert"-ing
// a bulk of File nodes.
type FileUpsertBulk struct {
	create *FileCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.File.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(file.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *FileUpsertBulk) UpdateNewValues() *FileUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(file.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(file.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.File.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *FileUpsertBulk) Ignore() *FileUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *FileUpsertBulk) DoNothing() *FileUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the FileCreateBulk.OnConflict
// documentation for more info.
func (u *FileUpsertBulk) Update(set func(*FileUpsert)) *FileUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&FileUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *FileUpsertBulk) SetDeletedAt(v time.Time) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateDeletedAt() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *FileUpsertBulk) ClearDeletedAt() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *FileUpsertBulk) SetUpdatedAt(v time.Time) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateUpdatedAt() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetType sets the "type" field.
func (u *FileUpsertBulk) SetType(v int) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *FileUpsertBulk) AddType(v int) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.AddType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateType() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateType()
	})
}

// SetOwnerID sets the "owner_id" field.
func (u *FileUpsertBulk) SetOwnerID(v uint) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetOwnerID(v)
	})
}

// UpdateOwnerID sets the "owner_id" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateOwnerID() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateOwnerID()
	})
}

// SetParentID sets the "parent_id" field.
func (u *FileUpsertBulk) SetParentID(v uint) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetParentID(v)
	})
}

// UpdateParentID sets the "parent_id" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateParentID() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateParentID()
	})
}

// ClearParentID clears the value of the "parent_id" field.
func (u *FileUpsertBulk) ClearParentID() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.ClearParentID()
	})
}

// SetName sets the "name" field.
func (u *FileUpsertBulk) SetName(v string) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateName() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateName()
	})
}

// SetSize sets the "size" field.
func (u *FileUpsertBulk) SetSize(v int64) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetSize(v)
	})
}

// AddSize adds v to the "size" field.
func (u *FileUpsertBulk) AddSize(v int64) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.AddSize(v)
	})
}

// UpdateSize sets the "size" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateSize() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateSize()
	})
}

// SetPrimaryEntityID sets the "primary_entity_id" field.
func (u *FileUpsertBulk) SetPrimaryEntityID(v uint) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetPrimaryEntityID(v)
	})
}

// UpdatePrimaryEntityID sets the "primary_entity_id" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdatePrimaryEntityID() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdatePrimaryEntityID()
	})
}

// ClearPrimaryEntityID clears the value of the "primary_entity_id" field.
func (u *FileUpsertBulk) ClearPrimaryEntityID() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.ClearPrimaryEntityID()
	})
}

// SetChildrenCount sets the "children_count" field.
func (u *FileUpsertBulk) SetChildrenCount(v int64) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetChildrenCount(v)
	})
}

// AddChildrenCount adds v to the "children_count" field.
func (u *FileUpsertBulk) AddChildrenCount(v int64) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.AddChildrenCount(v)
	})
}

// UpdateChildrenCount sets the "children_count" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateChildrenCount() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateChildrenCount()
	})
}

// SetViewConfig sets the "view_config" field.
func (u *FileUpsertBulk) SetViewConfig(v string) *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.SetViewConfig(v)
	})
}

// UpdateViewConfig sets the "view_config" field to the value that was provided on create.
func (u *FileUpsertBulk) UpdateViewConfig() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.UpdateViewConfig()
	})
}

// ClearViewConfig clears the value of the "view_config" field.
func (u *FileUpsertBulk) ClearViewConfig() *FileUpsertBulk {
	return u.Update(func(s *FileUpsert) {
		s.ClearViewConfig()
	})
}

// Exec executes the query.
func (u *FileUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the FileCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for FileCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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