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