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