Save creates the Meta entities in the database.
(ctx context.Context)
| 431 | |
| 432 | // Save creates the Meta entities in the database. |
| 433 | func (_c *MetaCreateBulk) Save(ctx context.Context) ([]*Meta, error) { |
| 434 | if _c.err != nil { |
| 435 | return nil, _c.err |
| 436 | } |
| 437 | specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
| 438 | nodes := make([]*Meta, len(_c.builders)) |
| 439 | mutators := make([]Mutator, len(_c.builders)) |
| 440 | for i := range _c.builders { |
| 441 | func(i int, root context.Context) { |
| 442 | builder := _c.builders[i] |
| 443 | builder.defaults() |
| 444 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 445 | mutation, ok := m.(*MetaMutation) |
| 446 | if !ok { |
| 447 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 448 | } |
| 449 | if err := builder.check(); err != nil { |
| 450 | return nil, err |
| 451 | } |
| 452 | builder.mutation = mutation |
| 453 | var err error |
| 454 | nodes[i], specs[i] = builder.createSpec() |
| 455 | if i < len(mutators)-1 { |
| 456 | _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) |
| 457 | } else { |
| 458 | spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 459 | spec.OnConflict = _c.conflict |
| 460 | // Invoke the actual operation on the latest mutation in the chain. |
| 461 | if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { |
| 462 | if sqlgraph.IsConstraintError(err) { |
| 463 | err = &ConstraintError{msg: err.Error(), wrap: err} |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | if err != nil { |
| 468 | return nil, err |
| 469 | } |
| 470 | mutation.id = &nodes[i].ID |
| 471 | if specs[i].ID.Value != nil { |
| 472 | id := specs[i].ID.Value.(int64) |
| 473 | nodes[i].ID = int(id) |
| 474 | } |
| 475 | mutation.done = true |
| 476 | return nodes[i], nil |
| 477 | }) |
| 478 | for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 479 | mut = builder.hooks[i](mut) |
| 480 | } |
| 481 | mutators[i] = mut |
| 482 | }(i, ctx) |
| 483 | } |
| 484 | if len(mutators) > 0 { |
| 485 | if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { |
| 486 | return nil, err |
| 487 | } |
| 488 | } |
| 489 | return nodes, nil |
| 490 | } |