// 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/cloudreve/Cloudreve/v4/ent/file"
	"github.com/cloudreve/Cloudreve/v4/ent/metadata"
)

// MetadataCreate is the builder for creating a Metadata entity.
type MetadataCreate struct {
	config
	mutation *MetadataMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (mc *MetadataCreate) SetCreatedAt(t time.Time) *MetadataCreate {
	mc.mutation.SetCreatedAt(t)
	return mc
}

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

// SetUpdatedAt sets the "updated_at" field.
func (mc *MetadataCreate) SetUpdatedAt(t time.Time) *MetadataCreate {
	mc.mutation.SetUpdatedAt(t)
	return mc
}

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

// SetDeletedAt sets the "deleted_at" field.
func (mc *MetadataCreate) SetDeletedAt(t time.Time) *MetadataCreate {
	mc.mutation.SetDeletedAt(t)
	return mc
}

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

// SetName sets the "name" field.
func (mc *MetadataCreate) SetName(s string) *MetadataCreate {
	mc.mutation.SetName(s)
	return mc
}

// SetValue sets the "value" field.
func (mc *MetadataCreate) SetValue(s string) *MetadataCreate {
	mc.mutation.SetValue(s)
	return mc
}

// SetFileID sets the "file_id" field.
func (mc *MetadataCreate) SetFileID(i int) *MetadataCreate {
	mc.mutation.SetFileID(i)
	return mc
}

// SetIsPublic sets the "is_public" field.
func (mc *MetadataCreate) SetIsPublic(b bool) *MetadataCreate {
	mc.mutation.SetIsPublic(b)
	return mc
}

// SetNillableIsPublic sets the "is_public" field if the given value is not nil.
func (mc *MetadataCreate) SetNillableIsPublic(b *bool) *MetadataCreate {
	if b != nil {
		mc.SetIsPublic(*b)
	}
	return mc
}

// SetFile sets the "file" edge to the File entity.
func (mc *MetadataCreate) SetFile(f *File) *MetadataCreate {
	return mc.SetFileID(f.ID)
}

// Mutation returns the MetadataMutation object of the builder.
func (mc *MetadataCreate) Mutation() *MetadataMutation {
	return mc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (mc *MetadataCreate) defaults() error {
	if _, ok := mc.mutation.CreatedAt(); !ok {
		if metadata.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized metadata.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := metadata.DefaultCreatedAt()
		mc.mutation.SetCreatedAt(v)
	}
	if _, ok := mc.mutation.UpdatedAt(); !ok {
		if metadata.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized metadata.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := metadata.DefaultUpdatedAt()
		mc.mutation.SetUpdatedAt(v)
	}
	if _, ok := mc.mutation.IsPublic(); !ok {
		v := metadata.DefaultIsPublic
		mc.mutation.SetIsPublic(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (mc *MetadataCreate) check() error {
	if _, ok := mc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Metadata.created_at"`)}
	}
	if _, ok := mc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Metadata.updated_at"`)}
	}
	if _, ok := mc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Metadata.name"`)}
	}
	if _, ok := mc.mutation.Value(); !ok {
		return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Metadata.value"`)}
	}
	if _, ok := mc.mutation.FileID(); !ok {
		return &ValidationError{Name: "file_id", err: errors.New(`ent: missing required field "Metadata.file_id"`)}
	}
	if _, ok := mc.mutation.IsPublic(); !ok {
		return &ValidationError{Name: "is_public", err: errors.New(`ent: missing required field "Metadata.is_public"`)}
	}
	if _, ok := mc.mutation.FileID(); !ok {
		return &ValidationError{Name: "file", err: errors.New(`ent: missing required edge "Metadata.file"`)}
	}
	return nil
}

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

func (mc *MetadataCreate) createSpec() (*Metadata, *sqlgraph.CreateSpec) {
	var (
		_node = &Metadata{config: mc.config}
		_spec = sqlgraph.NewCreateSpec(metadata.Table, sqlgraph.NewFieldSpec(metadata.FieldID, field.TypeInt))
	)

	if id, ok := mc.mutation.ID(); ok {
		_node.ID = id
		id64 := int64(id)
		_spec.ID.Value = id64
	}

	_spec.OnConflict = mc.conflict
	if value, ok := mc.mutation.CreatedAt(); ok {
		_spec.SetField(metadata.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := mc.mutation.UpdatedAt(); ok {
		_spec.SetField(metadata.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := mc.mutation.DeletedAt(); ok {
		_spec.SetField(metadata.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := mc.mutation.Name(); ok {
		_spec.SetField(metadata.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := mc.mutation.Value(); ok {
		_spec.SetField(metadata.FieldValue, field.TypeString, value)
		_node.Value = value
	}
	if value, ok := mc.mutation.IsPublic(); ok {
		_spec.SetField(metadata.FieldIsPublic, field.TypeBool, value)
		_node.IsPublic = value
	}
	if nodes := mc.mutation.FileIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   metadata.FileTable,
			Columns: []string{metadata.FileColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(file.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.FileID = nodes[0]
		_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.Metadata.Create().
//		SetCreatedAt(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.MetadataUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (mc *MetadataCreate) OnConflict(opts ...sql.ConflictOption) *MetadataUpsertOne {
	mc.conflict = opts
	return &MetadataUpsertOne{
		create: mc,
	}
}

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

type (
	// MetadataUpsertOne is the builder for "upsert"-ing
	//  one Metadata node.
	MetadataUpsertOne struct {
		create *MetadataCreate
	}

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

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

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

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

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

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

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

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

// SetValue sets the "value" field.
func (u *MetadataUpsert) SetValue(v string) *MetadataUpsert {
	u.Set(metadata.FieldValue, v)
	return u
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *MetadataUpsert) UpdateValue() *MetadataUpsert {
	u.SetExcluded(metadata.FieldValue)
	return u
}

// SetFileID sets the "file_id" field.
func (u *MetadataUpsert) SetFileID(v int) *MetadataUpsert {
	u.Set(metadata.FieldFileID, v)
	return u
}

// UpdateFileID sets the "file_id" field to the value that was provided on create.
func (u *MetadataUpsert) UpdateFileID() *MetadataUpsert {
	u.SetExcluded(metadata.FieldFileID)
	return u
}

// SetIsPublic sets the "is_public" field.
func (u *MetadataUpsert) SetIsPublic(v bool) *MetadataUpsert {
	u.Set(metadata.FieldIsPublic, v)
	return u
}

// UpdateIsPublic sets the "is_public" field to the value that was provided on create.
func (u *MetadataUpsert) UpdateIsPublic() *MetadataUpsert {
	u.SetExcluded(metadata.FieldIsPublic)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Metadata.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *MetadataUpsertOne) UpdateNewValues() *MetadataUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(metadata.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Metadata.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *MetadataUpsertOne) Ignore() *MetadataUpsertOne {
	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 *MetadataUpsertOne) DoNothing() *MetadataUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

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

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

// SetValue sets the "value" field.
func (u *MetadataUpsertOne) SetValue(v string) *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.SetValue(v)
	})
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *MetadataUpsertOne) UpdateValue() *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateValue()
	})
}

// SetFileID sets the "file_id" field.
func (u *MetadataUpsertOne) SetFileID(v int) *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.SetFileID(v)
	})
}

// UpdateFileID sets the "file_id" field to the value that was provided on create.
func (u *MetadataUpsertOne) UpdateFileID() *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateFileID()
	})
}

// SetIsPublic sets the "is_public" field.
func (u *MetadataUpsertOne) SetIsPublic(v bool) *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.SetIsPublic(v)
	})
}

// UpdateIsPublic sets the "is_public" field to the value that was provided on create.
func (u *MetadataUpsertOne) UpdateIsPublic() *MetadataUpsertOne {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateIsPublic()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *MetadataUpsertOne) 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 *MetadataUpsertOne) ID(ctx context.Context) (id int, 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 *MetadataUpsertOne) IDX(ctx context.Context) int {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

func (m *MetadataCreate) SetRawID(t int) *MetadataCreate {
	m.mutation.SetRawID(t)
	return m
}

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

// Save creates the Metadata entities in the database.
func (mcb *MetadataCreateBulk) Save(ctx context.Context) ([]*Metadata, error) {
	if mcb.err != nil {
		return nil, mcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
	nodes := make([]*Metadata, len(mcb.builders))
	mutators := make([]Mutator, len(mcb.builders))
	for i := range mcb.builders {
		func(i int, root context.Context) {
			builder := mcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*MetadataMutation)
				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, mcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = mcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, mcb.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, mcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Metadata.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.MetadataUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (mcb *MetadataCreateBulk) OnConflict(opts ...sql.ConflictOption) *MetadataUpsertBulk {
	mcb.conflict = opts
	return &MetadataUpsertBulk{
		create: mcb,
	}
}

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

// MetadataUpsertBulk is the builder for "upsert"-ing
// a bulk of Metadata nodes.
type MetadataUpsertBulk struct {
	create *MetadataCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Metadata.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *MetadataUpsertBulk) UpdateNewValues() *MetadataUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(metadata.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Metadata.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *MetadataUpsertBulk) Ignore() *MetadataUpsertBulk {
	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 *MetadataUpsertBulk) DoNothing() *MetadataUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

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

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

// SetValue sets the "value" field.
func (u *MetadataUpsertBulk) SetValue(v string) *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.SetValue(v)
	})
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *MetadataUpsertBulk) UpdateValue() *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateValue()
	})
}

// SetFileID sets the "file_id" field.
func (u *MetadataUpsertBulk) SetFileID(v int) *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.SetFileID(v)
	})
}

// UpdateFileID sets the "file_id" field to the value that was provided on create.
func (u *MetadataUpsertBulk) UpdateFileID() *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateFileID()
	})
}

// SetIsPublic sets the "is_public" field.
func (u *MetadataUpsertBulk) SetIsPublic(v bool) *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.SetIsPublic(v)
	})
}

// UpdateIsPublic sets the "is_public" field to the value that was provided on create.
func (u *MetadataUpsertBulk) UpdateIsPublic() *MetadataUpsertBulk {
	return u.Update(func(s *MetadataUpsert) {
		s.UpdateIsPublic()
	})
}

// Exec executes the query.
func (u *MetadataUpsertBulk) 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 MetadataCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for MetadataCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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