github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/alert_create.go (about) 1 // Code generated by ent, 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/sqlgraph" 12 "entgo.io/ent/schema/field" 13 "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert" 14 "github.com/crowdsecurity/crowdsec/pkg/database/ent/decision" 15 "github.com/crowdsecurity/crowdsec/pkg/database/ent/event" 16 "github.com/crowdsecurity/crowdsec/pkg/database/ent/machine" 17 "github.com/crowdsecurity/crowdsec/pkg/database/ent/meta" 18 ) 19 20 // AlertCreate is the builder for creating a Alert entity. 21 type AlertCreate struct { 22 config 23 mutation *AlertMutation 24 hooks []Hook 25 } 26 27 // SetCreatedAt sets the "created_at" field. 28 func (ac *AlertCreate) SetCreatedAt(t time.Time) *AlertCreate { 29 ac.mutation.SetCreatedAt(t) 30 return ac 31 } 32 33 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 34 func (ac *AlertCreate) SetNillableCreatedAt(t *time.Time) *AlertCreate { 35 if t != nil { 36 ac.SetCreatedAt(*t) 37 } 38 return ac 39 } 40 41 // SetUpdatedAt sets the "updated_at" field. 42 func (ac *AlertCreate) SetUpdatedAt(t time.Time) *AlertCreate { 43 ac.mutation.SetUpdatedAt(t) 44 return ac 45 } 46 47 // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. 48 func (ac *AlertCreate) SetNillableUpdatedAt(t *time.Time) *AlertCreate { 49 if t != nil { 50 ac.SetUpdatedAt(*t) 51 } 52 return ac 53 } 54 55 // SetScenario sets the "scenario" field. 56 func (ac *AlertCreate) SetScenario(s string) *AlertCreate { 57 ac.mutation.SetScenario(s) 58 return ac 59 } 60 61 // SetBucketId sets the "bucketId" field. 62 func (ac *AlertCreate) SetBucketId(s string) *AlertCreate { 63 ac.mutation.SetBucketId(s) 64 return ac 65 } 66 67 // SetNillableBucketId sets the "bucketId" field if the given value is not nil. 68 func (ac *AlertCreate) SetNillableBucketId(s *string) *AlertCreate { 69 if s != nil { 70 ac.SetBucketId(*s) 71 } 72 return ac 73 } 74 75 // SetMessage sets the "message" field. 76 func (ac *AlertCreate) SetMessage(s string) *AlertCreate { 77 ac.mutation.SetMessage(s) 78 return ac 79 } 80 81 // SetNillableMessage sets the "message" field if the given value is not nil. 82 func (ac *AlertCreate) SetNillableMessage(s *string) *AlertCreate { 83 if s != nil { 84 ac.SetMessage(*s) 85 } 86 return ac 87 } 88 89 // SetEventsCount sets the "eventsCount" field. 90 func (ac *AlertCreate) SetEventsCount(i int32) *AlertCreate { 91 ac.mutation.SetEventsCount(i) 92 return ac 93 } 94 95 // SetNillableEventsCount sets the "eventsCount" field if the given value is not nil. 96 func (ac *AlertCreate) SetNillableEventsCount(i *int32) *AlertCreate { 97 if i != nil { 98 ac.SetEventsCount(*i) 99 } 100 return ac 101 } 102 103 // SetStartedAt sets the "startedAt" field. 104 func (ac *AlertCreate) SetStartedAt(t time.Time) *AlertCreate { 105 ac.mutation.SetStartedAt(t) 106 return ac 107 } 108 109 // SetNillableStartedAt sets the "startedAt" field if the given value is not nil. 110 func (ac *AlertCreate) SetNillableStartedAt(t *time.Time) *AlertCreate { 111 if t != nil { 112 ac.SetStartedAt(*t) 113 } 114 return ac 115 } 116 117 // SetStoppedAt sets the "stoppedAt" field. 118 func (ac *AlertCreate) SetStoppedAt(t time.Time) *AlertCreate { 119 ac.mutation.SetStoppedAt(t) 120 return ac 121 } 122 123 // SetNillableStoppedAt sets the "stoppedAt" field if the given value is not nil. 124 func (ac *AlertCreate) SetNillableStoppedAt(t *time.Time) *AlertCreate { 125 if t != nil { 126 ac.SetStoppedAt(*t) 127 } 128 return ac 129 } 130 131 // SetSourceIp sets the "sourceIp" field. 132 func (ac *AlertCreate) SetSourceIp(s string) *AlertCreate { 133 ac.mutation.SetSourceIp(s) 134 return ac 135 } 136 137 // SetNillableSourceIp sets the "sourceIp" field if the given value is not nil. 138 func (ac *AlertCreate) SetNillableSourceIp(s *string) *AlertCreate { 139 if s != nil { 140 ac.SetSourceIp(*s) 141 } 142 return ac 143 } 144 145 // SetSourceRange sets the "sourceRange" field. 146 func (ac *AlertCreate) SetSourceRange(s string) *AlertCreate { 147 ac.mutation.SetSourceRange(s) 148 return ac 149 } 150 151 // SetNillableSourceRange sets the "sourceRange" field if the given value is not nil. 152 func (ac *AlertCreate) SetNillableSourceRange(s *string) *AlertCreate { 153 if s != nil { 154 ac.SetSourceRange(*s) 155 } 156 return ac 157 } 158 159 // SetSourceAsNumber sets the "sourceAsNumber" field. 160 func (ac *AlertCreate) SetSourceAsNumber(s string) *AlertCreate { 161 ac.mutation.SetSourceAsNumber(s) 162 return ac 163 } 164 165 // SetNillableSourceAsNumber sets the "sourceAsNumber" field if the given value is not nil. 166 func (ac *AlertCreate) SetNillableSourceAsNumber(s *string) *AlertCreate { 167 if s != nil { 168 ac.SetSourceAsNumber(*s) 169 } 170 return ac 171 } 172 173 // SetSourceAsName sets the "sourceAsName" field. 174 func (ac *AlertCreate) SetSourceAsName(s string) *AlertCreate { 175 ac.mutation.SetSourceAsName(s) 176 return ac 177 } 178 179 // SetNillableSourceAsName sets the "sourceAsName" field if the given value is not nil. 180 func (ac *AlertCreate) SetNillableSourceAsName(s *string) *AlertCreate { 181 if s != nil { 182 ac.SetSourceAsName(*s) 183 } 184 return ac 185 } 186 187 // SetSourceCountry sets the "sourceCountry" field. 188 func (ac *AlertCreate) SetSourceCountry(s string) *AlertCreate { 189 ac.mutation.SetSourceCountry(s) 190 return ac 191 } 192 193 // SetNillableSourceCountry sets the "sourceCountry" field if the given value is not nil. 194 func (ac *AlertCreate) SetNillableSourceCountry(s *string) *AlertCreate { 195 if s != nil { 196 ac.SetSourceCountry(*s) 197 } 198 return ac 199 } 200 201 // SetSourceLatitude sets the "sourceLatitude" field. 202 func (ac *AlertCreate) SetSourceLatitude(f float32) *AlertCreate { 203 ac.mutation.SetSourceLatitude(f) 204 return ac 205 } 206 207 // SetNillableSourceLatitude sets the "sourceLatitude" field if the given value is not nil. 208 func (ac *AlertCreate) SetNillableSourceLatitude(f *float32) *AlertCreate { 209 if f != nil { 210 ac.SetSourceLatitude(*f) 211 } 212 return ac 213 } 214 215 // SetSourceLongitude sets the "sourceLongitude" field. 216 func (ac *AlertCreate) SetSourceLongitude(f float32) *AlertCreate { 217 ac.mutation.SetSourceLongitude(f) 218 return ac 219 } 220 221 // SetNillableSourceLongitude sets the "sourceLongitude" field if the given value is not nil. 222 func (ac *AlertCreate) SetNillableSourceLongitude(f *float32) *AlertCreate { 223 if f != nil { 224 ac.SetSourceLongitude(*f) 225 } 226 return ac 227 } 228 229 // SetSourceScope sets the "sourceScope" field. 230 func (ac *AlertCreate) SetSourceScope(s string) *AlertCreate { 231 ac.mutation.SetSourceScope(s) 232 return ac 233 } 234 235 // SetNillableSourceScope sets the "sourceScope" field if the given value is not nil. 236 func (ac *AlertCreate) SetNillableSourceScope(s *string) *AlertCreate { 237 if s != nil { 238 ac.SetSourceScope(*s) 239 } 240 return ac 241 } 242 243 // SetSourceValue sets the "sourceValue" field. 244 func (ac *AlertCreate) SetSourceValue(s string) *AlertCreate { 245 ac.mutation.SetSourceValue(s) 246 return ac 247 } 248 249 // SetNillableSourceValue sets the "sourceValue" field if the given value is not nil. 250 func (ac *AlertCreate) SetNillableSourceValue(s *string) *AlertCreate { 251 if s != nil { 252 ac.SetSourceValue(*s) 253 } 254 return ac 255 } 256 257 // SetCapacity sets the "capacity" field. 258 func (ac *AlertCreate) SetCapacity(i int32) *AlertCreate { 259 ac.mutation.SetCapacity(i) 260 return ac 261 } 262 263 // SetNillableCapacity sets the "capacity" field if the given value is not nil. 264 func (ac *AlertCreate) SetNillableCapacity(i *int32) *AlertCreate { 265 if i != nil { 266 ac.SetCapacity(*i) 267 } 268 return ac 269 } 270 271 // SetLeakSpeed sets the "leakSpeed" field. 272 func (ac *AlertCreate) SetLeakSpeed(s string) *AlertCreate { 273 ac.mutation.SetLeakSpeed(s) 274 return ac 275 } 276 277 // SetNillableLeakSpeed sets the "leakSpeed" field if the given value is not nil. 278 func (ac *AlertCreate) SetNillableLeakSpeed(s *string) *AlertCreate { 279 if s != nil { 280 ac.SetLeakSpeed(*s) 281 } 282 return ac 283 } 284 285 // SetScenarioVersion sets the "scenarioVersion" field. 286 func (ac *AlertCreate) SetScenarioVersion(s string) *AlertCreate { 287 ac.mutation.SetScenarioVersion(s) 288 return ac 289 } 290 291 // SetNillableScenarioVersion sets the "scenarioVersion" field if the given value is not nil. 292 func (ac *AlertCreate) SetNillableScenarioVersion(s *string) *AlertCreate { 293 if s != nil { 294 ac.SetScenarioVersion(*s) 295 } 296 return ac 297 } 298 299 // SetScenarioHash sets the "scenarioHash" field. 300 func (ac *AlertCreate) SetScenarioHash(s string) *AlertCreate { 301 ac.mutation.SetScenarioHash(s) 302 return ac 303 } 304 305 // SetNillableScenarioHash sets the "scenarioHash" field if the given value is not nil. 306 func (ac *AlertCreate) SetNillableScenarioHash(s *string) *AlertCreate { 307 if s != nil { 308 ac.SetScenarioHash(*s) 309 } 310 return ac 311 } 312 313 // SetSimulated sets the "simulated" field. 314 func (ac *AlertCreate) SetSimulated(b bool) *AlertCreate { 315 ac.mutation.SetSimulated(b) 316 return ac 317 } 318 319 // SetNillableSimulated sets the "simulated" field if the given value is not nil. 320 func (ac *AlertCreate) SetNillableSimulated(b *bool) *AlertCreate { 321 if b != nil { 322 ac.SetSimulated(*b) 323 } 324 return ac 325 } 326 327 // SetUUID sets the "uuid" field. 328 func (ac *AlertCreate) SetUUID(s string) *AlertCreate { 329 ac.mutation.SetUUID(s) 330 return ac 331 } 332 333 // SetNillableUUID sets the "uuid" field if the given value is not nil. 334 func (ac *AlertCreate) SetNillableUUID(s *string) *AlertCreate { 335 if s != nil { 336 ac.SetUUID(*s) 337 } 338 return ac 339 } 340 341 // SetOwnerID sets the "owner" edge to the Machine entity by ID. 342 func (ac *AlertCreate) SetOwnerID(id int) *AlertCreate { 343 ac.mutation.SetOwnerID(id) 344 return ac 345 } 346 347 // SetNillableOwnerID sets the "owner" edge to the Machine entity by ID if the given value is not nil. 348 func (ac *AlertCreate) SetNillableOwnerID(id *int) *AlertCreate { 349 if id != nil { 350 ac = ac.SetOwnerID(*id) 351 } 352 return ac 353 } 354 355 // SetOwner sets the "owner" edge to the Machine entity. 356 func (ac *AlertCreate) SetOwner(m *Machine) *AlertCreate { 357 return ac.SetOwnerID(m.ID) 358 } 359 360 // AddDecisionIDs adds the "decisions" edge to the Decision entity by IDs. 361 func (ac *AlertCreate) AddDecisionIDs(ids ...int) *AlertCreate { 362 ac.mutation.AddDecisionIDs(ids...) 363 return ac 364 } 365 366 // AddDecisions adds the "decisions" edges to the Decision entity. 367 func (ac *AlertCreate) AddDecisions(d ...*Decision) *AlertCreate { 368 ids := make([]int, len(d)) 369 for i := range d { 370 ids[i] = d[i].ID 371 } 372 return ac.AddDecisionIDs(ids...) 373 } 374 375 // AddEventIDs adds the "events" edge to the Event entity by IDs. 376 func (ac *AlertCreate) AddEventIDs(ids ...int) *AlertCreate { 377 ac.mutation.AddEventIDs(ids...) 378 return ac 379 } 380 381 // AddEvents adds the "events" edges to the Event entity. 382 func (ac *AlertCreate) AddEvents(e ...*Event) *AlertCreate { 383 ids := make([]int, len(e)) 384 for i := range e { 385 ids[i] = e[i].ID 386 } 387 return ac.AddEventIDs(ids...) 388 } 389 390 // AddMetaIDs adds the "metas" edge to the Meta entity by IDs. 391 func (ac *AlertCreate) AddMetaIDs(ids ...int) *AlertCreate { 392 ac.mutation.AddMetaIDs(ids...) 393 return ac 394 } 395 396 // AddMetas adds the "metas" edges to the Meta entity. 397 func (ac *AlertCreate) AddMetas(m ...*Meta) *AlertCreate { 398 ids := make([]int, len(m)) 399 for i := range m { 400 ids[i] = m[i].ID 401 } 402 return ac.AddMetaIDs(ids...) 403 } 404 405 // Mutation returns the AlertMutation object of the builder. 406 func (ac *AlertCreate) Mutation() *AlertMutation { 407 return ac.mutation 408 } 409 410 // Save creates the Alert in the database. 411 func (ac *AlertCreate) Save(ctx context.Context) (*Alert, error) { 412 ac.defaults() 413 return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks) 414 } 415 416 // SaveX calls Save and panics if Save returns an error. 417 func (ac *AlertCreate) SaveX(ctx context.Context) *Alert { 418 v, err := ac.Save(ctx) 419 if err != nil { 420 panic(err) 421 } 422 return v 423 } 424 425 // Exec executes the query. 426 func (ac *AlertCreate) Exec(ctx context.Context) error { 427 _, err := ac.Save(ctx) 428 return err 429 } 430 431 // ExecX is like Exec, but panics if an error occurs. 432 func (ac *AlertCreate) ExecX(ctx context.Context) { 433 if err := ac.Exec(ctx); err != nil { 434 panic(err) 435 } 436 } 437 438 // defaults sets the default values of the builder before save. 439 func (ac *AlertCreate) defaults() { 440 if _, ok := ac.mutation.CreatedAt(); !ok { 441 v := alert.DefaultCreatedAt() 442 ac.mutation.SetCreatedAt(v) 443 } 444 if _, ok := ac.mutation.UpdatedAt(); !ok { 445 v := alert.DefaultUpdatedAt() 446 ac.mutation.SetUpdatedAt(v) 447 } 448 if _, ok := ac.mutation.BucketId(); !ok { 449 v := alert.DefaultBucketId 450 ac.mutation.SetBucketId(v) 451 } 452 if _, ok := ac.mutation.Message(); !ok { 453 v := alert.DefaultMessage 454 ac.mutation.SetMessage(v) 455 } 456 if _, ok := ac.mutation.EventsCount(); !ok { 457 v := alert.DefaultEventsCount 458 ac.mutation.SetEventsCount(v) 459 } 460 if _, ok := ac.mutation.StartedAt(); !ok { 461 v := alert.DefaultStartedAt() 462 ac.mutation.SetStartedAt(v) 463 } 464 if _, ok := ac.mutation.StoppedAt(); !ok { 465 v := alert.DefaultStoppedAt() 466 ac.mutation.SetStoppedAt(v) 467 } 468 if _, ok := ac.mutation.Simulated(); !ok { 469 v := alert.DefaultSimulated 470 ac.mutation.SetSimulated(v) 471 } 472 } 473 474 // check runs all checks and user-defined validators on the builder. 475 func (ac *AlertCreate) check() error { 476 if _, ok := ac.mutation.Scenario(); !ok { 477 return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "Alert.scenario"`)} 478 } 479 if _, ok := ac.mutation.Simulated(); !ok { 480 return &ValidationError{Name: "simulated", err: errors.New(`ent: missing required field "Alert.simulated"`)} 481 } 482 return nil 483 } 484 485 func (ac *AlertCreate) sqlSave(ctx context.Context) (*Alert, error) { 486 if err := ac.check(); err != nil { 487 return nil, err 488 } 489 _node, _spec := ac.createSpec() 490 if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil { 491 if sqlgraph.IsConstraintError(err) { 492 err = &ConstraintError{msg: err.Error(), wrap: err} 493 } 494 return nil, err 495 } 496 id := _spec.ID.Value.(int64) 497 _node.ID = int(id) 498 ac.mutation.id = &_node.ID 499 ac.mutation.done = true 500 return _node, nil 501 } 502 503 func (ac *AlertCreate) createSpec() (*Alert, *sqlgraph.CreateSpec) { 504 var ( 505 _node = &Alert{config: ac.config} 506 _spec = sqlgraph.NewCreateSpec(alert.Table, sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt)) 507 ) 508 if value, ok := ac.mutation.CreatedAt(); ok { 509 _spec.SetField(alert.FieldCreatedAt, field.TypeTime, value) 510 _node.CreatedAt = &value 511 } 512 if value, ok := ac.mutation.UpdatedAt(); ok { 513 _spec.SetField(alert.FieldUpdatedAt, field.TypeTime, value) 514 _node.UpdatedAt = &value 515 } 516 if value, ok := ac.mutation.Scenario(); ok { 517 _spec.SetField(alert.FieldScenario, field.TypeString, value) 518 _node.Scenario = value 519 } 520 if value, ok := ac.mutation.BucketId(); ok { 521 _spec.SetField(alert.FieldBucketId, field.TypeString, value) 522 _node.BucketId = value 523 } 524 if value, ok := ac.mutation.Message(); ok { 525 _spec.SetField(alert.FieldMessage, field.TypeString, value) 526 _node.Message = value 527 } 528 if value, ok := ac.mutation.EventsCount(); ok { 529 _spec.SetField(alert.FieldEventsCount, field.TypeInt32, value) 530 _node.EventsCount = value 531 } 532 if value, ok := ac.mutation.StartedAt(); ok { 533 _spec.SetField(alert.FieldStartedAt, field.TypeTime, value) 534 _node.StartedAt = value 535 } 536 if value, ok := ac.mutation.StoppedAt(); ok { 537 _spec.SetField(alert.FieldStoppedAt, field.TypeTime, value) 538 _node.StoppedAt = value 539 } 540 if value, ok := ac.mutation.SourceIp(); ok { 541 _spec.SetField(alert.FieldSourceIp, field.TypeString, value) 542 _node.SourceIp = value 543 } 544 if value, ok := ac.mutation.SourceRange(); ok { 545 _spec.SetField(alert.FieldSourceRange, field.TypeString, value) 546 _node.SourceRange = value 547 } 548 if value, ok := ac.mutation.SourceAsNumber(); ok { 549 _spec.SetField(alert.FieldSourceAsNumber, field.TypeString, value) 550 _node.SourceAsNumber = value 551 } 552 if value, ok := ac.mutation.SourceAsName(); ok { 553 _spec.SetField(alert.FieldSourceAsName, field.TypeString, value) 554 _node.SourceAsName = value 555 } 556 if value, ok := ac.mutation.SourceCountry(); ok { 557 _spec.SetField(alert.FieldSourceCountry, field.TypeString, value) 558 _node.SourceCountry = value 559 } 560 if value, ok := ac.mutation.SourceLatitude(); ok { 561 _spec.SetField(alert.FieldSourceLatitude, field.TypeFloat32, value) 562 _node.SourceLatitude = value 563 } 564 if value, ok := ac.mutation.SourceLongitude(); ok { 565 _spec.SetField(alert.FieldSourceLongitude, field.TypeFloat32, value) 566 _node.SourceLongitude = value 567 } 568 if value, ok := ac.mutation.SourceScope(); ok { 569 _spec.SetField(alert.FieldSourceScope, field.TypeString, value) 570 _node.SourceScope = value 571 } 572 if value, ok := ac.mutation.SourceValue(); ok { 573 _spec.SetField(alert.FieldSourceValue, field.TypeString, value) 574 _node.SourceValue = value 575 } 576 if value, ok := ac.mutation.Capacity(); ok { 577 _spec.SetField(alert.FieldCapacity, field.TypeInt32, value) 578 _node.Capacity = value 579 } 580 if value, ok := ac.mutation.LeakSpeed(); ok { 581 _spec.SetField(alert.FieldLeakSpeed, field.TypeString, value) 582 _node.LeakSpeed = value 583 } 584 if value, ok := ac.mutation.ScenarioVersion(); ok { 585 _spec.SetField(alert.FieldScenarioVersion, field.TypeString, value) 586 _node.ScenarioVersion = value 587 } 588 if value, ok := ac.mutation.ScenarioHash(); ok { 589 _spec.SetField(alert.FieldScenarioHash, field.TypeString, value) 590 _node.ScenarioHash = value 591 } 592 if value, ok := ac.mutation.Simulated(); ok { 593 _spec.SetField(alert.FieldSimulated, field.TypeBool, value) 594 _node.Simulated = value 595 } 596 if value, ok := ac.mutation.UUID(); ok { 597 _spec.SetField(alert.FieldUUID, field.TypeString, value) 598 _node.UUID = value 599 } 600 if nodes := ac.mutation.OwnerIDs(); len(nodes) > 0 { 601 edge := &sqlgraph.EdgeSpec{ 602 Rel: sqlgraph.M2O, 603 Inverse: true, 604 Table: alert.OwnerTable, 605 Columns: []string{alert.OwnerColumn}, 606 Bidi: false, 607 Target: &sqlgraph.EdgeTarget{ 608 IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt), 609 }, 610 } 611 for _, k := range nodes { 612 edge.Target.Nodes = append(edge.Target.Nodes, k) 613 } 614 _node.machine_alerts = &nodes[0] 615 _spec.Edges = append(_spec.Edges, edge) 616 } 617 if nodes := ac.mutation.DecisionsIDs(); len(nodes) > 0 { 618 edge := &sqlgraph.EdgeSpec{ 619 Rel: sqlgraph.O2M, 620 Inverse: false, 621 Table: alert.DecisionsTable, 622 Columns: []string{alert.DecisionsColumn}, 623 Bidi: false, 624 Target: &sqlgraph.EdgeTarget{ 625 IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt), 626 }, 627 } 628 for _, k := range nodes { 629 edge.Target.Nodes = append(edge.Target.Nodes, k) 630 } 631 _spec.Edges = append(_spec.Edges, edge) 632 } 633 if nodes := ac.mutation.EventsIDs(); len(nodes) > 0 { 634 edge := &sqlgraph.EdgeSpec{ 635 Rel: sqlgraph.O2M, 636 Inverse: false, 637 Table: alert.EventsTable, 638 Columns: []string{alert.EventsColumn}, 639 Bidi: false, 640 Target: &sqlgraph.EdgeTarget{ 641 IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt), 642 }, 643 } 644 for _, k := range nodes { 645 edge.Target.Nodes = append(edge.Target.Nodes, k) 646 } 647 _spec.Edges = append(_spec.Edges, edge) 648 } 649 if nodes := ac.mutation.MetasIDs(); len(nodes) > 0 { 650 edge := &sqlgraph.EdgeSpec{ 651 Rel: sqlgraph.O2M, 652 Inverse: false, 653 Table: alert.MetasTable, 654 Columns: []string{alert.MetasColumn}, 655 Bidi: false, 656 Target: &sqlgraph.EdgeTarget{ 657 IDSpec: sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt), 658 }, 659 } 660 for _, k := range nodes { 661 edge.Target.Nodes = append(edge.Target.Nodes, k) 662 } 663 _spec.Edges = append(_spec.Edges, edge) 664 } 665 return _node, _spec 666 } 667 668 // AlertCreateBulk is the builder for creating many Alert entities in bulk. 669 type AlertCreateBulk struct { 670 config 671 err error 672 builders []*AlertCreate 673 } 674 675 // Save creates the Alert entities in the database. 676 func (acb *AlertCreateBulk) Save(ctx context.Context) ([]*Alert, error) { 677 if acb.err != nil { 678 return nil, acb.err 679 } 680 specs := make([]*sqlgraph.CreateSpec, len(acb.builders)) 681 nodes := make([]*Alert, len(acb.builders)) 682 mutators := make([]Mutator, len(acb.builders)) 683 for i := range acb.builders { 684 func(i int, root context.Context) { 685 builder := acb.builders[i] 686 builder.defaults() 687 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 688 mutation, ok := m.(*AlertMutation) 689 if !ok { 690 return nil, fmt.Errorf("unexpected mutation type %T", m) 691 } 692 if err := builder.check(); err != nil { 693 return nil, err 694 } 695 builder.mutation = mutation 696 var err error 697 nodes[i], specs[i] = builder.createSpec() 698 if i < len(mutators)-1 { 699 _, err = mutators[i+1].Mutate(root, acb.builders[i+1].mutation) 700 } else { 701 spec := &sqlgraph.BatchCreateSpec{Nodes: specs} 702 // Invoke the actual operation on the latest mutation in the chain. 703 if err = sqlgraph.BatchCreate(ctx, acb.driver, spec); err != nil { 704 if sqlgraph.IsConstraintError(err) { 705 err = &ConstraintError{msg: err.Error(), wrap: err} 706 } 707 } 708 } 709 if err != nil { 710 return nil, err 711 } 712 mutation.id = &nodes[i].ID 713 if specs[i].ID.Value != nil { 714 id := specs[i].ID.Value.(int64) 715 nodes[i].ID = int(id) 716 } 717 mutation.done = true 718 return nodes[i], nil 719 }) 720 for i := len(builder.hooks) - 1; i >= 0; i-- { 721 mut = builder.hooks[i](mut) 722 } 723 mutators[i] = mut 724 }(i, ctx) 725 } 726 if len(mutators) > 0 { 727 if _, err := mutators[0].Mutate(ctx, acb.builders[0].mutation); err != nil { 728 return nil, err 729 } 730 } 731 return nodes, nil 732 } 733 734 // SaveX is like Save, but panics if an error occurs. 735 func (acb *AlertCreateBulk) SaveX(ctx context.Context) []*Alert { 736 v, err := acb.Save(ctx) 737 if err != nil { 738 panic(err) 739 } 740 return v 741 } 742 743 // Exec executes the query. 744 func (acb *AlertCreateBulk) Exec(ctx context.Context) error { 745 _, err := acb.Save(ctx) 746 return err 747 } 748 749 // ExecX is like Exec, but panics if an error occurs. 750 func (acb *AlertCreateBulk) ExecX(ctx context.Context) { 751 if err := acb.Exec(ctx); err != nil { 752 panic(err) 753 } 754 }