github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/permission_create.go (about)

     1  // Code generated by entc, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"time"
    10  
    11  	"entgo.io/ent/dialect/sql"
    12  	"entgo.io/ent/dialect/sql/sqlgraph"
    13  	"entgo.io/ent/schema/field"
    14  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/permission"
    15  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/role"
    16  )
    17  
    18  // PermissionCreate is the builder for creating a Permission entity.
    19  type PermissionCreate struct {
    20  	config
    21  	mutation *PermissionMutation
    22  	hooks    []Hook
    23  	conflict []sql.ConflictOption
    24  }
    25  
    26  // SetCreatedAt sets the "created_at" field.
    27  func (pc *PermissionCreate) SetCreatedAt(t time.Time) *PermissionCreate {
    28  	pc.mutation.SetCreatedAt(t)
    29  	return pc
    30  }
    31  
    32  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    33  func (pc *PermissionCreate) SetNillableCreatedAt(t *time.Time) *PermissionCreate {
    34  	if t != nil {
    35  		pc.SetCreatedAt(*t)
    36  	}
    37  	return pc
    38  }
    39  
    40  // SetUpdatedAt sets the "updated_at" field.
    41  func (pc *PermissionCreate) SetUpdatedAt(t time.Time) *PermissionCreate {
    42  	pc.mutation.SetUpdatedAt(t)
    43  	return pc
    44  }
    45  
    46  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    47  func (pc *PermissionCreate) SetNillableUpdatedAt(t *time.Time) *PermissionCreate {
    48  	if t != nil {
    49  		pc.SetUpdatedAt(*t)
    50  	}
    51  	return pc
    52  }
    53  
    54  // SetDeletedAt sets the "deleted_at" field.
    55  func (pc *PermissionCreate) SetDeletedAt(t time.Time) *PermissionCreate {
    56  	pc.mutation.SetDeletedAt(t)
    57  	return pc
    58  }
    59  
    60  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
    61  func (pc *PermissionCreate) SetNillableDeletedAt(t *time.Time) *PermissionCreate {
    62  	if t != nil {
    63  		pc.SetDeletedAt(*t)
    64  	}
    65  	return pc
    66  }
    67  
    68  // SetRoleID sets the "role_id" field.
    69  func (pc *PermissionCreate) SetRoleID(i int) *PermissionCreate {
    70  	pc.mutation.SetRoleID(i)
    71  	return pc
    72  }
    73  
    74  // SetAction sets the "action" field.
    75  func (pc *PermissionCreate) SetAction(s string) *PermissionCreate {
    76  	pc.mutation.SetAction(s)
    77  	return pc
    78  }
    79  
    80  // SetValue sets the "value" field.
    81  func (pc *PermissionCreate) SetValue(s string) *PermissionCreate {
    82  	pc.mutation.SetValue(s)
    83  	return pc
    84  }
    85  
    86  // SetRole sets the "role" edge to the Role entity.
    87  func (pc *PermissionCreate) SetRole(r *Role) *PermissionCreate {
    88  	return pc.SetRoleID(r.ID)
    89  }
    90  
    91  // Mutation returns the PermissionMutation object of the builder.
    92  func (pc *PermissionCreate) Mutation() *PermissionMutation {
    93  	return pc.mutation
    94  }
    95  
    96  // Save creates the Permission in the database.
    97  func (pc *PermissionCreate) Save(ctx context.Context) (*Permission, error) {
    98  	var (
    99  		err  error
   100  		node *Permission
   101  	)
   102  	pc.defaults()
   103  	if len(pc.hooks) == 0 {
   104  		if err = pc.check(); err != nil {
   105  			return nil, err
   106  		}
   107  		node, err = pc.sqlSave(ctx)
   108  	} else {
   109  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   110  			mutation, ok := m.(*PermissionMutation)
   111  			if !ok {
   112  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   113  			}
   114  			if err = pc.check(); err != nil {
   115  				return nil, err
   116  			}
   117  			pc.mutation = mutation
   118  			if node, err = pc.sqlSave(ctx); err != nil {
   119  				return nil, err
   120  			}
   121  			mutation.id = &node.ID
   122  			mutation.done = true
   123  			return node, err
   124  		})
   125  		for i := len(pc.hooks) - 1; i >= 0; i-- {
   126  			if pc.hooks[i] == nil {
   127  				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   128  			}
   129  			mut = pc.hooks[i](mut)
   130  		}
   131  		if _, err := mut.Mutate(ctx, pc.mutation); err != nil {
   132  			return nil, err
   133  		}
   134  	}
   135  	return node, err
   136  }
   137  
   138  // SaveX calls Save and panics if Save returns an error.
   139  func (pc *PermissionCreate) SaveX(ctx context.Context) *Permission {
   140  	v, err := pc.Save(ctx)
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  	return v
   145  }
   146  
   147  // Exec executes the query.
   148  func (pc *PermissionCreate) Exec(ctx context.Context) error {
   149  	_, err := pc.Save(ctx)
   150  	return err
   151  }
   152  
   153  // ExecX is like Exec, but panics if an error occurs.
   154  func (pc *PermissionCreate) ExecX(ctx context.Context) {
   155  	if err := pc.Exec(ctx); err != nil {
   156  		panic(err)
   157  	}
   158  }
   159  
   160  // defaults sets the default values of the builder before save.
   161  func (pc *PermissionCreate) defaults() {
   162  	if _, ok := pc.mutation.CreatedAt(); !ok {
   163  		v := permission.DefaultCreatedAt()
   164  		pc.mutation.SetCreatedAt(v)
   165  	}
   166  	if _, ok := pc.mutation.UpdatedAt(); !ok {
   167  		v := permission.DefaultUpdatedAt()
   168  		pc.mutation.SetUpdatedAt(v)
   169  	}
   170  }
   171  
   172  // check runs all checks and user-defined validators on the builder.
   173  func (pc *PermissionCreate) check() error {
   174  	if _, ok := pc.mutation.CreatedAt(); !ok {
   175  		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Permission.created_at"`)}
   176  	}
   177  	if _, ok := pc.mutation.UpdatedAt(); !ok {
   178  		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Permission.updated_at"`)}
   179  	}
   180  	if _, ok := pc.mutation.RoleID(); !ok {
   181  		return &ValidationError{Name: "role_id", err: errors.New(`ent: missing required field "Permission.role_id"`)}
   182  	}
   183  	if _, ok := pc.mutation.Action(); !ok {
   184  		return &ValidationError{Name: "action", err: errors.New(`ent: missing required field "Permission.action"`)}
   185  	}
   186  	if _, ok := pc.mutation.Value(); !ok {
   187  		return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Permission.value"`)}
   188  	}
   189  	if _, ok := pc.mutation.RoleID(); !ok {
   190  		return &ValidationError{Name: "role", err: errors.New(`ent: missing required edge "Permission.role"`)}
   191  	}
   192  	return nil
   193  }
   194  
   195  func (pc *PermissionCreate) sqlSave(ctx context.Context) (*Permission, error) {
   196  	_node, _spec := pc.createSpec()
   197  	if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil {
   198  		if sqlgraph.IsConstraintError(err) {
   199  			err = &ConstraintError{err.Error(), err}
   200  		}
   201  		return nil, err
   202  	}
   203  	id := _spec.ID.Value.(int64)
   204  	_node.ID = int(id)
   205  	return _node, nil
   206  }
   207  
   208  func (pc *PermissionCreate) createSpec() (*Permission, *sqlgraph.CreateSpec) {
   209  	var (
   210  		_node = &Permission{config: pc.config}
   211  		_spec = &sqlgraph.CreateSpec{
   212  			Table: permission.Table,
   213  			ID: &sqlgraph.FieldSpec{
   214  				Type:   field.TypeInt,
   215  				Column: permission.FieldID,
   216  			},
   217  		}
   218  	)
   219  	_spec.OnConflict = pc.conflict
   220  	if value, ok := pc.mutation.CreatedAt(); ok {
   221  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   222  			Type:   field.TypeTime,
   223  			Value:  value,
   224  			Column: permission.FieldCreatedAt,
   225  		})
   226  		_node.CreatedAt = value
   227  	}
   228  	if value, ok := pc.mutation.UpdatedAt(); ok {
   229  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   230  			Type:   field.TypeTime,
   231  			Value:  value,
   232  			Column: permission.FieldUpdatedAt,
   233  		})
   234  		_node.UpdatedAt = value
   235  	}
   236  	if value, ok := pc.mutation.DeletedAt(); ok {
   237  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   238  			Type:   field.TypeTime,
   239  			Value:  value,
   240  			Column: permission.FieldDeletedAt,
   241  		})
   242  		_node.DeletedAt = value
   243  	}
   244  	if value, ok := pc.mutation.Action(); ok {
   245  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   246  			Type:   field.TypeString,
   247  			Value:  value,
   248  			Column: permission.FieldAction,
   249  		})
   250  		_node.Action = value
   251  	}
   252  	if value, ok := pc.mutation.Value(); ok {
   253  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   254  			Type:   field.TypeString,
   255  			Value:  value,
   256  			Column: permission.FieldValue,
   257  		})
   258  		_node.Value = value
   259  	}
   260  	if nodes := pc.mutation.RoleIDs(); len(nodes) > 0 {
   261  		edge := &sqlgraph.EdgeSpec{
   262  			Rel:     sqlgraph.M2O,
   263  			Inverse: true,
   264  			Table:   permission.RoleTable,
   265  			Columns: []string{permission.RoleColumn},
   266  			Bidi:    false,
   267  			Target: &sqlgraph.EdgeTarget{
   268  				IDSpec: &sqlgraph.FieldSpec{
   269  					Type:   field.TypeInt,
   270  					Column: role.FieldID,
   271  				},
   272  			},
   273  		}
   274  		for _, k := range nodes {
   275  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   276  		}
   277  		_node.RoleID = nodes[0]
   278  		_spec.Edges = append(_spec.Edges, edge)
   279  	}
   280  	return _node, _spec
   281  }
   282  
   283  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
   284  // of the `INSERT` statement. For example:
   285  //
   286  //	client.Permission.Create().
   287  //		SetCreatedAt(v).
   288  //		OnConflict(
   289  //			// Update the row with the new values
   290  //			// the was proposed for insertion.
   291  //			sql.ResolveWithNewValues(),
   292  //		).
   293  //		// Override some of the fields with custom
   294  //		// update values.
   295  //		Update(func(u *ent.PermissionUpsert) {
   296  //			SetCreatedAt(v+v).
   297  //		}).
   298  //		Exec(ctx)
   299  //
   300  func (pc *PermissionCreate) OnConflict(opts ...sql.ConflictOption) *PermissionUpsertOne {
   301  	pc.conflict = opts
   302  	return &PermissionUpsertOne{
   303  		create: pc,
   304  	}
   305  }
   306  
   307  // OnConflictColumns calls `OnConflict` and configures the columns
   308  // as conflict target. Using this option is equivalent to using:
   309  //
   310  //	client.Permission.Create().
   311  //		OnConflict(sql.ConflictColumns(columns...)).
   312  //		Exec(ctx)
   313  //
   314  func (pc *PermissionCreate) OnConflictColumns(columns ...string) *PermissionUpsertOne {
   315  	pc.conflict = append(pc.conflict, sql.ConflictColumns(columns...))
   316  	return &PermissionUpsertOne{
   317  		create: pc,
   318  	}
   319  }
   320  
   321  type (
   322  	// PermissionUpsertOne is the builder for "upsert"-ing
   323  	//  one Permission node.
   324  	PermissionUpsertOne struct {
   325  		create *PermissionCreate
   326  	}
   327  
   328  	// PermissionUpsert is the "OnConflict" setter.
   329  	PermissionUpsert struct {
   330  		*sql.UpdateSet
   331  	}
   332  )
   333  
   334  // SetCreatedAt sets the "created_at" field.
   335  func (u *PermissionUpsert) SetCreatedAt(v time.Time) *PermissionUpsert {
   336  	u.Set(permission.FieldCreatedAt, v)
   337  	return u
   338  }
   339  
   340  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   341  func (u *PermissionUpsert) UpdateCreatedAt() *PermissionUpsert {
   342  	u.SetExcluded(permission.FieldCreatedAt)
   343  	return u
   344  }
   345  
   346  // SetUpdatedAt sets the "updated_at" field.
   347  func (u *PermissionUpsert) SetUpdatedAt(v time.Time) *PermissionUpsert {
   348  	u.Set(permission.FieldUpdatedAt, v)
   349  	return u
   350  }
   351  
   352  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
   353  func (u *PermissionUpsert) UpdateUpdatedAt() *PermissionUpsert {
   354  	u.SetExcluded(permission.FieldUpdatedAt)
   355  	return u
   356  }
   357  
   358  // SetDeletedAt sets the "deleted_at" field.
   359  func (u *PermissionUpsert) SetDeletedAt(v time.Time) *PermissionUpsert {
   360  	u.Set(permission.FieldDeletedAt, v)
   361  	return u
   362  }
   363  
   364  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
   365  func (u *PermissionUpsert) UpdateDeletedAt() *PermissionUpsert {
   366  	u.SetExcluded(permission.FieldDeletedAt)
   367  	return u
   368  }
   369  
   370  // ClearDeletedAt clears the value of the "deleted_at" field.
   371  func (u *PermissionUpsert) ClearDeletedAt() *PermissionUpsert {
   372  	u.SetNull(permission.FieldDeletedAt)
   373  	return u
   374  }
   375  
   376  // SetRoleID sets the "role_id" field.
   377  func (u *PermissionUpsert) SetRoleID(v int) *PermissionUpsert {
   378  	u.Set(permission.FieldRoleID, v)
   379  	return u
   380  }
   381  
   382  // UpdateRoleID sets the "role_id" field to the value that was provided on create.
   383  func (u *PermissionUpsert) UpdateRoleID() *PermissionUpsert {
   384  	u.SetExcluded(permission.FieldRoleID)
   385  	return u
   386  }
   387  
   388  // SetAction sets the "action" field.
   389  func (u *PermissionUpsert) SetAction(v string) *PermissionUpsert {
   390  	u.Set(permission.FieldAction, v)
   391  	return u
   392  }
   393  
   394  // UpdateAction sets the "action" field to the value that was provided on create.
   395  func (u *PermissionUpsert) UpdateAction() *PermissionUpsert {
   396  	u.SetExcluded(permission.FieldAction)
   397  	return u
   398  }
   399  
   400  // SetValue sets the "value" field.
   401  func (u *PermissionUpsert) SetValue(v string) *PermissionUpsert {
   402  	u.Set(permission.FieldValue, v)
   403  	return u
   404  }
   405  
   406  // UpdateValue sets the "value" field to the value that was provided on create.
   407  func (u *PermissionUpsert) UpdateValue() *PermissionUpsert {
   408  	u.SetExcluded(permission.FieldValue)
   409  	return u
   410  }
   411  
   412  // UpdateNewValues updates the mutable fields using the new values that were set on create.
   413  // Using this option is equivalent to using:
   414  //
   415  //	client.Permission.Create().
   416  //		OnConflict(
   417  //			sql.ResolveWithNewValues(),
   418  //		).
   419  //		Exec(ctx)
   420  //
   421  func (u *PermissionUpsertOne) UpdateNewValues() *PermissionUpsertOne {
   422  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
   423  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
   424  		if _, exists := u.create.mutation.CreatedAt(); exists {
   425  			s.SetIgnore(permission.FieldCreatedAt)
   426  		}
   427  	}))
   428  	return u
   429  }
   430  
   431  // Ignore sets each column to itself in case of conflict.
   432  // Using this option is equivalent to using:
   433  //
   434  //  client.Permission.Create().
   435  //      OnConflict(sql.ResolveWithIgnore()).
   436  //      Exec(ctx)
   437  //
   438  func (u *PermissionUpsertOne) Ignore() *PermissionUpsertOne {
   439  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
   440  	return u
   441  }
   442  
   443  // DoNothing configures the conflict_action to `DO NOTHING`.
   444  // Supported only by SQLite and PostgreSQL.
   445  func (u *PermissionUpsertOne) DoNothing() *PermissionUpsertOne {
   446  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
   447  	return u
   448  }
   449  
   450  // Update allows overriding fields `UPDATE` values. See the PermissionCreate.OnConflict
   451  // documentation for more info.
   452  func (u *PermissionUpsertOne) Update(set func(*PermissionUpsert)) *PermissionUpsertOne {
   453  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
   454  		set(&PermissionUpsert{UpdateSet: update})
   455  	}))
   456  	return u
   457  }
   458  
   459  // SetCreatedAt sets the "created_at" field.
   460  func (u *PermissionUpsertOne) SetCreatedAt(v time.Time) *PermissionUpsertOne {
   461  	return u.Update(func(s *PermissionUpsert) {
   462  		s.SetCreatedAt(v)
   463  	})
   464  }
   465  
   466  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   467  func (u *PermissionUpsertOne) UpdateCreatedAt() *PermissionUpsertOne {
   468  	return u.Update(func(s *PermissionUpsert) {
   469  		s.UpdateCreatedAt()
   470  	})
   471  }
   472  
   473  // SetUpdatedAt sets the "updated_at" field.
   474  func (u *PermissionUpsertOne) SetUpdatedAt(v time.Time) *PermissionUpsertOne {
   475  	return u.Update(func(s *PermissionUpsert) {
   476  		s.SetUpdatedAt(v)
   477  	})
   478  }
   479  
   480  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
   481  func (u *PermissionUpsertOne) UpdateUpdatedAt() *PermissionUpsertOne {
   482  	return u.Update(func(s *PermissionUpsert) {
   483  		s.UpdateUpdatedAt()
   484  	})
   485  }
   486  
   487  // SetDeletedAt sets the "deleted_at" field.
   488  func (u *PermissionUpsertOne) SetDeletedAt(v time.Time) *PermissionUpsertOne {
   489  	return u.Update(func(s *PermissionUpsert) {
   490  		s.SetDeletedAt(v)
   491  	})
   492  }
   493  
   494  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
   495  func (u *PermissionUpsertOne) UpdateDeletedAt() *PermissionUpsertOne {
   496  	return u.Update(func(s *PermissionUpsert) {
   497  		s.UpdateDeletedAt()
   498  	})
   499  }
   500  
   501  // ClearDeletedAt clears the value of the "deleted_at" field.
   502  func (u *PermissionUpsertOne) ClearDeletedAt() *PermissionUpsertOne {
   503  	return u.Update(func(s *PermissionUpsert) {
   504  		s.ClearDeletedAt()
   505  	})
   506  }
   507  
   508  // SetRoleID sets the "role_id" field.
   509  func (u *PermissionUpsertOne) SetRoleID(v int) *PermissionUpsertOne {
   510  	return u.Update(func(s *PermissionUpsert) {
   511  		s.SetRoleID(v)
   512  	})
   513  }
   514  
   515  // UpdateRoleID sets the "role_id" field to the value that was provided on create.
   516  func (u *PermissionUpsertOne) UpdateRoleID() *PermissionUpsertOne {
   517  	return u.Update(func(s *PermissionUpsert) {
   518  		s.UpdateRoleID()
   519  	})
   520  }
   521  
   522  // SetAction sets the "action" field.
   523  func (u *PermissionUpsertOne) SetAction(v string) *PermissionUpsertOne {
   524  	return u.Update(func(s *PermissionUpsert) {
   525  		s.SetAction(v)
   526  	})
   527  }
   528  
   529  // UpdateAction sets the "action" field to the value that was provided on create.
   530  func (u *PermissionUpsertOne) UpdateAction() *PermissionUpsertOne {
   531  	return u.Update(func(s *PermissionUpsert) {
   532  		s.UpdateAction()
   533  	})
   534  }
   535  
   536  // SetValue sets the "value" field.
   537  func (u *PermissionUpsertOne) SetValue(v string) *PermissionUpsertOne {
   538  	return u.Update(func(s *PermissionUpsert) {
   539  		s.SetValue(v)
   540  	})
   541  }
   542  
   543  // UpdateValue sets the "value" field to the value that was provided on create.
   544  func (u *PermissionUpsertOne) UpdateValue() *PermissionUpsertOne {
   545  	return u.Update(func(s *PermissionUpsert) {
   546  		s.UpdateValue()
   547  	})
   548  }
   549  
   550  // Exec executes the query.
   551  func (u *PermissionUpsertOne) Exec(ctx context.Context) error {
   552  	if len(u.create.conflict) == 0 {
   553  		return errors.New("ent: missing options for PermissionCreate.OnConflict")
   554  	}
   555  	return u.create.Exec(ctx)
   556  }
   557  
   558  // ExecX is like Exec, but panics if an error occurs.
   559  func (u *PermissionUpsertOne) ExecX(ctx context.Context) {
   560  	if err := u.create.Exec(ctx); err != nil {
   561  		panic(err)
   562  	}
   563  }
   564  
   565  // Exec executes the UPSERT query and returns the inserted/updated ID.
   566  func (u *PermissionUpsertOne) ID(ctx context.Context) (id int, err error) {
   567  	node, err := u.create.Save(ctx)
   568  	if err != nil {
   569  		return id, err
   570  	}
   571  	return node.ID, nil
   572  }
   573  
   574  // IDX is like ID, but panics if an error occurs.
   575  func (u *PermissionUpsertOne) IDX(ctx context.Context) int {
   576  	id, err := u.ID(ctx)
   577  	if err != nil {
   578  		panic(err)
   579  	}
   580  	return id
   581  }
   582  
   583  // PermissionCreateBulk is the builder for creating many Permission entities in bulk.
   584  type PermissionCreateBulk struct {
   585  	config
   586  	builders []*PermissionCreate
   587  	conflict []sql.ConflictOption
   588  }
   589  
   590  // Save creates the Permission entities in the database.
   591  func (pcb *PermissionCreateBulk) Save(ctx context.Context) ([]*Permission, error) {
   592  	specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
   593  	nodes := make([]*Permission, len(pcb.builders))
   594  	mutators := make([]Mutator, len(pcb.builders))
   595  	for i := range pcb.builders {
   596  		func(i int, root context.Context) {
   597  			builder := pcb.builders[i]
   598  			builder.defaults()
   599  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   600  				mutation, ok := m.(*PermissionMutation)
   601  				if !ok {
   602  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   603  				}
   604  				if err := builder.check(); err != nil {
   605  					return nil, err
   606  				}
   607  				builder.mutation = mutation
   608  				nodes[i], specs[i] = builder.createSpec()
   609  				var err error
   610  				if i < len(mutators)-1 {
   611  					_, err = mutators[i+1].Mutate(root, pcb.builders[i+1].mutation)
   612  				} else {
   613  					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
   614  					spec.OnConflict = pcb.conflict
   615  					// Invoke the actual operation on the latest mutation in the chain.
   616  					if err = sqlgraph.BatchCreate(ctx, pcb.driver, spec); err != nil {
   617  						if sqlgraph.IsConstraintError(err) {
   618  							err = &ConstraintError{err.Error(), err}
   619  						}
   620  					}
   621  				}
   622  				if err != nil {
   623  					return nil, err
   624  				}
   625  				mutation.id = &nodes[i].ID
   626  				mutation.done = true
   627  				if specs[i].ID.Value != nil {
   628  					id := specs[i].ID.Value.(int64)
   629  					nodes[i].ID = int(id)
   630  				}
   631  				return nodes[i], nil
   632  			})
   633  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   634  				mut = builder.hooks[i](mut)
   635  			}
   636  			mutators[i] = mut
   637  		}(i, ctx)
   638  	}
   639  	if len(mutators) > 0 {
   640  		if _, err := mutators[0].Mutate(ctx, pcb.builders[0].mutation); err != nil {
   641  			return nil, err
   642  		}
   643  	}
   644  	return nodes, nil
   645  }
   646  
   647  // SaveX is like Save, but panics if an error occurs.
   648  func (pcb *PermissionCreateBulk) SaveX(ctx context.Context) []*Permission {
   649  	v, err := pcb.Save(ctx)
   650  	if err != nil {
   651  		panic(err)
   652  	}
   653  	return v
   654  }
   655  
   656  // Exec executes the query.
   657  func (pcb *PermissionCreateBulk) Exec(ctx context.Context) error {
   658  	_, err := pcb.Save(ctx)
   659  	return err
   660  }
   661  
   662  // ExecX is like Exec, but panics if an error occurs.
   663  func (pcb *PermissionCreateBulk) ExecX(ctx context.Context) {
   664  	if err := pcb.Exec(ctx); err != nil {
   665  		panic(err)
   666  	}
   667  }
   668  
   669  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
   670  // of the `INSERT` statement. For example:
   671  //
   672  //	client.Permission.CreateBulk(builders...).
   673  //		OnConflict(
   674  //			// Update the row with the new values
   675  //			// the was proposed for insertion.
   676  //			sql.ResolveWithNewValues(),
   677  //		).
   678  //		// Override some of the fields with custom
   679  //		// update values.
   680  //		Update(func(u *ent.PermissionUpsert) {
   681  //			SetCreatedAt(v+v).
   682  //		}).
   683  //		Exec(ctx)
   684  //
   685  func (pcb *PermissionCreateBulk) OnConflict(opts ...sql.ConflictOption) *PermissionUpsertBulk {
   686  	pcb.conflict = opts
   687  	return &PermissionUpsertBulk{
   688  		create: pcb,
   689  	}
   690  }
   691  
   692  // OnConflictColumns calls `OnConflict` and configures the columns
   693  // as conflict target. Using this option is equivalent to using:
   694  //
   695  //	client.Permission.Create().
   696  //		OnConflict(sql.ConflictColumns(columns...)).
   697  //		Exec(ctx)
   698  //
   699  func (pcb *PermissionCreateBulk) OnConflictColumns(columns ...string) *PermissionUpsertBulk {
   700  	pcb.conflict = append(pcb.conflict, sql.ConflictColumns(columns...))
   701  	return &PermissionUpsertBulk{
   702  		create: pcb,
   703  	}
   704  }
   705  
   706  // PermissionUpsertBulk is the builder for "upsert"-ing
   707  // a bulk of Permission nodes.
   708  type PermissionUpsertBulk struct {
   709  	create *PermissionCreateBulk
   710  }
   711  
   712  // UpdateNewValues updates the mutable fields using the new values that
   713  // were set on create. Using this option is equivalent to using:
   714  //
   715  //	client.Permission.Create().
   716  //		OnConflict(
   717  //			sql.ResolveWithNewValues(),
   718  //		).
   719  //		Exec(ctx)
   720  //
   721  func (u *PermissionUpsertBulk) UpdateNewValues() *PermissionUpsertBulk {
   722  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
   723  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
   724  		for _, b := range u.create.builders {
   725  			if _, exists := b.mutation.CreatedAt(); exists {
   726  				s.SetIgnore(permission.FieldCreatedAt)
   727  			}
   728  		}
   729  	}))
   730  	return u
   731  }
   732  
   733  // Ignore sets each column to itself in case of conflict.
   734  // Using this option is equivalent to using:
   735  //
   736  //	client.Permission.Create().
   737  //		OnConflict(sql.ResolveWithIgnore()).
   738  //		Exec(ctx)
   739  //
   740  func (u *PermissionUpsertBulk) Ignore() *PermissionUpsertBulk {
   741  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
   742  	return u
   743  }
   744  
   745  // DoNothing configures the conflict_action to `DO NOTHING`.
   746  // Supported only by SQLite and PostgreSQL.
   747  func (u *PermissionUpsertBulk) DoNothing() *PermissionUpsertBulk {
   748  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
   749  	return u
   750  }
   751  
   752  // Update allows overriding fields `UPDATE` values. See the PermissionCreateBulk.OnConflict
   753  // documentation for more info.
   754  func (u *PermissionUpsertBulk) Update(set func(*PermissionUpsert)) *PermissionUpsertBulk {
   755  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
   756  		set(&PermissionUpsert{UpdateSet: update})
   757  	}))
   758  	return u
   759  }
   760  
   761  // SetCreatedAt sets the "created_at" field.
   762  func (u *PermissionUpsertBulk) SetCreatedAt(v time.Time) *PermissionUpsertBulk {
   763  	return u.Update(func(s *PermissionUpsert) {
   764  		s.SetCreatedAt(v)
   765  	})
   766  }
   767  
   768  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   769  func (u *PermissionUpsertBulk) UpdateCreatedAt() *PermissionUpsertBulk {
   770  	return u.Update(func(s *PermissionUpsert) {
   771  		s.UpdateCreatedAt()
   772  	})
   773  }
   774  
   775  // SetUpdatedAt sets the "updated_at" field.
   776  func (u *PermissionUpsertBulk) SetUpdatedAt(v time.Time) *PermissionUpsertBulk {
   777  	return u.Update(func(s *PermissionUpsert) {
   778  		s.SetUpdatedAt(v)
   779  	})
   780  }
   781  
   782  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
   783  func (u *PermissionUpsertBulk) UpdateUpdatedAt() *PermissionUpsertBulk {
   784  	return u.Update(func(s *PermissionUpsert) {
   785  		s.UpdateUpdatedAt()
   786  	})
   787  }
   788  
   789  // SetDeletedAt sets the "deleted_at" field.
   790  func (u *PermissionUpsertBulk) SetDeletedAt(v time.Time) *PermissionUpsertBulk {
   791  	return u.Update(func(s *PermissionUpsert) {
   792  		s.SetDeletedAt(v)
   793  	})
   794  }
   795  
   796  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
   797  func (u *PermissionUpsertBulk) UpdateDeletedAt() *PermissionUpsertBulk {
   798  	return u.Update(func(s *PermissionUpsert) {
   799  		s.UpdateDeletedAt()
   800  	})
   801  }
   802  
   803  // ClearDeletedAt clears the value of the "deleted_at" field.
   804  func (u *PermissionUpsertBulk) ClearDeletedAt() *PermissionUpsertBulk {
   805  	return u.Update(func(s *PermissionUpsert) {
   806  		s.ClearDeletedAt()
   807  	})
   808  }
   809  
   810  // SetRoleID sets the "role_id" field.
   811  func (u *PermissionUpsertBulk) SetRoleID(v int) *PermissionUpsertBulk {
   812  	return u.Update(func(s *PermissionUpsert) {
   813  		s.SetRoleID(v)
   814  	})
   815  }
   816  
   817  // UpdateRoleID sets the "role_id" field to the value that was provided on create.
   818  func (u *PermissionUpsertBulk) UpdateRoleID() *PermissionUpsertBulk {
   819  	return u.Update(func(s *PermissionUpsert) {
   820  		s.UpdateRoleID()
   821  	})
   822  }
   823  
   824  // SetAction sets the "action" field.
   825  func (u *PermissionUpsertBulk) SetAction(v string) *PermissionUpsertBulk {
   826  	return u.Update(func(s *PermissionUpsert) {
   827  		s.SetAction(v)
   828  	})
   829  }
   830  
   831  // UpdateAction sets the "action" field to the value that was provided on create.
   832  func (u *PermissionUpsertBulk) UpdateAction() *PermissionUpsertBulk {
   833  	return u.Update(func(s *PermissionUpsert) {
   834  		s.UpdateAction()
   835  	})
   836  }
   837  
   838  // SetValue sets the "value" field.
   839  func (u *PermissionUpsertBulk) SetValue(v string) *PermissionUpsertBulk {
   840  	return u.Update(func(s *PermissionUpsert) {
   841  		s.SetValue(v)
   842  	})
   843  }
   844  
   845  // UpdateValue sets the "value" field to the value that was provided on create.
   846  func (u *PermissionUpsertBulk) UpdateValue() *PermissionUpsertBulk {
   847  	return u.Update(func(s *PermissionUpsert) {
   848  		s.UpdateValue()
   849  	})
   850  }
   851  
   852  // Exec executes the query.
   853  func (u *PermissionUpsertBulk) Exec(ctx context.Context) error {
   854  	for i, b := range u.create.builders {
   855  		if len(b.conflict) != 0 {
   856  			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PermissionCreateBulk instead", i)
   857  		}
   858  	}
   859  	if len(u.create.conflict) == 0 {
   860  		return errors.New("ent: missing options for PermissionCreateBulk.OnConflict")
   861  	}
   862  	return u.create.Exec(ctx)
   863  }
   864  
   865  // ExecX is like Exec, but panics if an error occurs.
   866  func (u *PermissionUpsertBulk) ExecX(ctx context.Context) {
   867  	if err := u.create.Exec(ctx); err != nil {
   868  		panic(err)
   869  	}
   870  }