Save creates the Entity entities in the database.
(ctx context.Context)
| 886 | |
| 887 | // Save creates the Entity entities in the database. |
| 888 | func (ecb *EntityCreateBulk) Save(ctx context.Context) ([]*Entity, error) { |
| 889 | if ecb.err != nil { |
| 890 | return nil, ecb.err |
| 891 | } |
| 892 | specs := make([]*sqlgraph.CreateSpec, len(ecb.builders)) |
| 893 | nodes := make([]*Entity, len(ecb.builders)) |
| 894 | mutators := make([]Mutator, len(ecb.builders)) |
| 895 | for i := range ecb.builders { |
| 896 | func(i int, root context.Context) { |
| 897 | builder := ecb.builders[i] |
| 898 | builder.defaults() |
| 899 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 900 | mutation, ok := m.(*EntityMutation) |
| 901 | if !ok { |
| 902 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 903 | } |
| 904 | if err := builder.check(); err != nil { |
| 905 | return nil, err |
| 906 | } |
| 907 | builder.mutation = mutation |
| 908 | var err error |
| 909 | nodes[i], specs[i] = builder.createSpec() |
| 910 | if i < len(mutators)-1 { |
| 911 | _, err = mutators[i+1].Mutate(root, ecb.builders[i+1].mutation) |
| 912 | } else { |
| 913 | spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 914 | spec.OnConflict = ecb.conflict |
| 915 | // Invoke the actual operation on the latest mutation in the chain. |
| 916 | if err = sqlgraph.BatchCreate(ctx, ecb.driver, spec); err != nil { |
| 917 | if sqlgraph.IsConstraintError(err) { |
| 918 | err = &ConstraintError{msg: err.Error(), wrap: err} |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | if err != nil { |
| 923 | return nil, err |
| 924 | } |
| 925 | mutation.id = &nodes[i].ID |
| 926 | if specs[i].ID.Value != nil { |
| 927 | id := specs[i].ID.Value.(int64) |
| 928 | nodes[i].ID = int(id) |
| 929 | } |
| 930 | mutation.done = true |
| 931 | return nodes[i], nil |
| 932 | }) |
| 933 | for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 934 | mut = builder.hooks[i](mut) |
| 935 | } |
| 936 | mutators[i] = mut |
| 937 | }(i, ctx) |
| 938 | } |
| 939 | if len(mutators) > 0 { |
| 940 | if _, err := mutators[0].Mutate(ctx, ecb.builders[0].mutation); err != nil { |
| 941 | return nil, err |
| 942 | } |
| 943 | } |
| 944 | return nodes, nil |
| 945 | } |