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