// 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/oauthclient"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthgrant"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
)

// OAuthGrantCreate is the builder for creating a OAuthGrant entity.
type OAuthGrantCreate struct {
	config
	mutation *OAuthGrantMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// SetUserID sets the "user_id" field.
func (ogc *OAuthGrantCreate) SetUserID(i int) *OAuthGrantCreate {
	ogc.mutation.SetUserID(i)
	return ogc
}

// SetClientID sets the "client_id" field.
func (ogc *OAuthGrantCreate) SetClientID(i int) *OAuthGrantCreate {
	ogc.mutation.SetClientID(i)
	return ogc
}

// SetScopes sets the "scopes" field.
func (ogc *OAuthGrantCreate) SetScopes(s []string) *OAuthGrantCreate {
	ogc.mutation.SetScopes(s)
	return ogc
}

// SetLastUsedAt sets the "last_used_at" field.
func (ogc *OAuthGrantCreate) SetLastUsedAt(t time.Time) *OAuthGrantCreate {
	ogc.mutation.SetLastUsedAt(t)
	return ogc
}

// SetNillableLastUsedAt sets the "last_used_at" field if the given value is not nil.
func (ogc *OAuthGrantCreate) SetNillableLastUsedAt(t *time.Time) *OAuthGrantCreate {
	if t != nil {
		ogc.SetLastUsedAt(*t)
	}
	return ogc
}

// SetUser sets the "user" edge to the User entity.
func (ogc *OAuthGrantCreate) SetUser(u *User) *OAuthGrantCreate {
	return ogc.SetUserID(u.ID)
}

// SetClient sets the "client" edge to the OAuthClient entity.
func (ogc *OAuthGrantCreate) SetClient(o *OAuthClient) *OAuthGrantCreate {
	return ogc.SetClientID(o.ID)
}

// Mutation returns the OAuthGrantMutation object of the builder.
func (ogc *OAuthGrantCreate) Mutation() *OAuthGrantMutation {
	return ogc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (ogc *OAuthGrantCreate) defaults() error {
	if _, ok := ogc.mutation.CreatedAt(); !ok {
		if oauthgrant.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized oauthgrant.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := oauthgrant.DefaultCreatedAt()
		ogc.mutation.SetCreatedAt(v)
	}
	if _, ok := ogc.mutation.UpdatedAt(); !ok {
		if oauthgrant.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized oauthgrant.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := oauthgrant.DefaultUpdatedAt()
		ogc.mutation.SetUpdatedAt(v)
	}
	if _, ok := ogc.mutation.Scopes(); !ok {
		v := oauthgrant.DefaultScopes
		ogc.mutation.SetScopes(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (ogc *OAuthGrantCreate) check() error {
	if _, ok := ogc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "OAuthGrant.created_at"`)}
	}
	if _, ok := ogc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "OAuthGrant.updated_at"`)}
	}
	if _, ok := ogc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "OAuthGrant.user_id"`)}
	}
	if _, ok := ogc.mutation.ClientID(); !ok {
		return &ValidationError{Name: "client_id", err: errors.New(`ent: missing required field "OAuthGrant.client_id"`)}
	}
	if _, ok := ogc.mutation.Scopes(); !ok {
		return &ValidationError{Name: "scopes", err: errors.New(`ent: missing required field "OAuthGrant.scopes"`)}
	}
	if _, ok := ogc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "OAuthGrant.user"`)}
	}
	if _, ok := ogc.mutation.ClientID(); !ok {
		return &ValidationError{Name: "client", err: errors.New(`ent: missing required edge "OAuthGrant.client"`)}
	}
	return nil
}

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

func (ogc *OAuthGrantCreate) createSpec() (*OAuthGrant, *sqlgraph.CreateSpec) {
	var (
		_node = &OAuthGrant{config: ogc.config}
		_spec = sqlgraph.NewCreateSpec(oauthgrant.Table, sqlgraph.NewFieldSpec(oauthgrant.FieldID, field.TypeInt))
	)

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

	_spec.OnConflict = ogc.conflict
	if value, ok := ogc.mutation.CreatedAt(); ok {
		_spec.SetField(oauthgrant.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := ogc.mutation.UpdatedAt(); ok {
		_spec.SetField(oauthgrant.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := ogc.mutation.DeletedAt(); ok {
		_spec.SetField(oauthgrant.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := ogc.mutation.Scopes(); ok {
		_spec.SetField(oauthgrant.FieldScopes, field.TypeJSON, value)
		_node.Scopes = value
	}
	if value, ok := ogc.mutation.LastUsedAt(); ok {
		_spec.SetField(oauthgrant.FieldLastUsedAt, field.TypeTime, value)
		_node.LastUsedAt = &value
	}
	if nodes := ogc.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   oauthgrant.UserTable,
			Columns: []string{oauthgrant.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := ogc.mutation.ClientIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   oauthgrant.ClientTable,
			Columns: []string{oauthgrant.ClientColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(oauthclient.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.ClientID = 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.OAuthGrant.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.OAuthGrantUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (ogc *OAuthGrantCreate) OnConflict(opts ...sql.ConflictOption) *OAuthGrantUpsertOne {
	ogc.conflict = opts
	return &OAuthGrantUpsertOne{
		create: ogc,
	}
}

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

type (
	// OAuthGrantUpsertOne is the builder for "upsert"-ing
	//  one OAuthGrant node.
	OAuthGrantUpsertOne struct {
		create *OAuthGrantCreate
	}

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

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

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

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

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

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

// SetUserID sets the "user_id" field.
func (u *OAuthGrantUpsert) SetUserID(v int) *OAuthGrantUpsert {
	u.Set(oauthgrant.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *OAuthGrantUpsert) UpdateUserID() *OAuthGrantUpsert {
	u.SetExcluded(oauthgrant.FieldUserID)
	return u
}

// SetClientID sets the "client_id" field.
func (u *OAuthGrantUpsert) SetClientID(v int) *OAuthGrantUpsert {
	u.Set(oauthgrant.FieldClientID, v)
	return u
}

// UpdateClientID sets the "client_id" field to the value that was provided on create.
func (u *OAuthGrantUpsert) UpdateClientID() *OAuthGrantUpsert {
	u.SetExcluded(oauthgrant.FieldClientID)
	return u
}

// SetScopes sets the "scopes" field.
func (u *OAuthGrantUpsert) SetScopes(v []string) *OAuthGrantUpsert {
	u.Set(oauthgrant.FieldScopes, v)
	return u
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthGrantUpsert) UpdateScopes() *OAuthGrantUpsert {
	u.SetExcluded(oauthgrant.FieldScopes)
	return u
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *OAuthGrantUpsert) SetLastUsedAt(v time.Time) *OAuthGrantUpsert {
	u.Set(oauthgrant.FieldLastUsedAt, v)
	return u
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *OAuthGrantUpsert) UpdateLastUsedAt() *OAuthGrantUpsert {
	u.SetExcluded(oauthgrant.FieldLastUsedAt)
	return u
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *OAuthGrantUpsert) ClearLastUsedAt() *OAuthGrantUpsert {
	u.SetNull(oauthgrant.FieldLastUsedAt)
	return u
}

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

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

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

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

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

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

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

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

// SetUserID sets the "user_id" field.
func (u *OAuthGrantUpsertOne) SetUserID(v int) *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *OAuthGrantUpsertOne) UpdateUserID() *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateUserID()
	})
}

// SetClientID sets the "client_id" field.
func (u *OAuthGrantUpsertOne) SetClientID(v int) *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetClientID(v)
	})
}

// UpdateClientID sets the "client_id" field to the value that was provided on create.
func (u *OAuthGrantUpsertOne) UpdateClientID() *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateClientID()
	})
}

// SetScopes sets the "scopes" field.
func (u *OAuthGrantUpsertOne) SetScopes(v []string) *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetScopes(v)
	})
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthGrantUpsertOne) UpdateScopes() *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateScopes()
	})
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *OAuthGrantUpsertOne) SetLastUsedAt(v time.Time) *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetLastUsedAt(v)
	})
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *OAuthGrantUpsertOne) UpdateLastUsedAt() *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateLastUsedAt()
	})
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *OAuthGrantUpsertOne) ClearLastUsedAt() *OAuthGrantUpsertOne {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.ClearLastUsedAt()
	})
}

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

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

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

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

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

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

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

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

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

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

// OAuthGrantUpsertBulk is the builder for "upsert"-ing
// a bulk of OAuthGrant nodes.
type OAuthGrantUpsertBulk struct {
	create *OAuthGrantCreateBulk
}

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

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

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

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

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

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

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

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

// SetUserID sets the "user_id" field.
func (u *OAuthGrantUpsertBulk) SetUserID(v int) *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *OAuthGrantUpsertBulk) UpdateUserID() *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateUserID()
	})
}

// SetClientID sets the "client_id" field.
func (u *OAuthGrantUpsertBulk) SetClientID(v int) *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetClientID(v)
	})
}

// UpdateClientID sets the "client_id" field to the value that was provided on create.
func (u *OAuthGrantUpsertBulk) UpdateClientID() *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateClientID()
	})
}

// SetScopes sets the "scopes" field.
func (u *OAuthGrantUpsertBulk) SetScopes(v []string) *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetScopes(v)
	})
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthGrantUpsertBulk) UpdateScopes() *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateScopes()
	})
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *OAuthGrantUpsertBulk) SetLastUsedAt(v time.Time) *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.SetLastUsedAt(v)
	})
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *OAuthGrantUpsertBulk) UpdateLastUsedAt() *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.UpdateLastUsedAt()
	})
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *OAuthGrantUpsertBulk) ClearLastUsedAt() *OAuthGrantUpsertBulk {
	return u.Update(func(s *OAuthGrantUpsert) {
		s.ClearLastUsedAt()
	})
}

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

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