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