MCPcopy
hub / github.com/crowdsecurity/crowdsec / Save

Method Save

pkg/database/ent/allowlist_create.go:494–551  ·  view source on GitHub ↗

Save creates the AllowList entities in the database.

(ctx context.Context)

Source from the content-addressed store, hash-verified

492
493// Save creates the AllowList entities in the database.
494func (_c *AllowListCreateBulk) Save(ctx context.Context) ([]*AllowList, error) {
495 if _c.err != nil {
496 return nil, _c.err
497 }
498 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
499 nodes := make([]*AllowList, len(_c.builders))
500 mutators := make([]Mutator, len(_c.builders))
501 for i := range _c.builders {
502 func(i int, root context.Context) {
503 builder := _c.builders[i]
504 builder.defaults()
505 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
506 mutation, ok := m.(*AllowListMutation)
507 if !ok {
508 return nil, fmt.Errorf("unexpected mutation type %T", m)
509 }
510 if err := builder.check(); err != nil {
511 return nil, err
512 }
513 builder.mutation = mutation
514 var err error
515 nodes[i], specs[i] = builder.createSpec()
516 if i < len(mutators)-1 {
517 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
518 } else {
519 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
520 spec.OnConflict = _c.conflict
521 // Invoke the actual operation on the latest mutation in the chain.
522 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
523 if sqlgraph.IsConstraintError(err) {
524 err = &ConstraintError{msg: err.Error(), wrap: err}
525 }
526 }
527 }
528 if err != nil {
529 return nil, err
530 }
531 mutation.id = &nodes[i].ID
532 if specs[i].ID.Value != nil {
533 id := specs[i].ID.Value.(int64)
534 nodes[i].ID = int(id)
535 }
536 mutation.done = true
537 return nodes[i], nil
538 })
539 for i := len(builder.hooks) - 1; i >= 0; i-- {
540 mut = builder.hooks[i](mut)
541 }
542 mutators[i] = mut
543 }(i, ctx)
544 }
545 if len(mutators) > 0 {
546 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
547 return nil, err
548 }
549 }
550 return nodes, nil
551}

Callers 2

SaveXMethod · 0.95
ExecMethod · 0.95

Calls 5

ErrorMethod · 0.65
defaultsMethod · 0.45
checkMethod · 0.45
createSpecMethod · 0.45
MutateMethod · 0.45

Tested by

no test coverage detected