Save creates the Machine entities in the database.
(ctx context.Context)
| 1096 | |
| 1097 | // Save creates the Machine entities in the database. |
| 1098 | func (_c *MachineCreateBulk) Save(ctx context.Context) ([]*Machine, error) { |
| 1099 | if _c.err != nil { |
| 1100 | return nil, _c.err |
| 1101 | } |
| 1102 | specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
| 1103 | nodes := make([]*Machine, len(_c.builders)) |
| 1104 | mutators := make([]Mutator, len(_c.builders)) |
| 1105 | for i := range _c.builders { |
| 1106 | func(i int, root context.Context) { |
| 1107 | builder := _c.builders[i] |
| 1108 | builder.defaults() |
| 1109 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 1110 | mutation, ok := m.(*MachineMutation) |
| 1111 | if !ok { |
| 1112 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 1113 | } |
| 1114 | if err := builder.check(); err != nil { |
| 1115 | return nil, err |
| 1116 | } |
| 1117 | builder.mutation = mutation |
| 1118 | var err error |
| 1119 | nodes[i], specs[i] = builder.createSpec() |
| 1120 | if i < len(mutators)-1 { |
| 1121 | _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) |
| 1122 | } else { |
| 1123 | spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 1124 | spec.OnConflict = _c.conflict |
| 1125 | // Invoke the actual operation on the latest mutation in the chain. |
| 1126 | if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { |
| 1127 | if sqlgraph.IsConstraintError(err) { |
| 1128 | err = &ConstraintError{msg: err.Error(), wrap: err} |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | if err != nil { |
| 1133 | return nil, err |
| 1134 | } |
| 1135 | mutation.id = &nodes[i].ID |
| 1136 | if specs[i].ID.Value != nil { |
| 1137 | id := specs[i].ID.Value.(int64) |
| 1138 | nodes[i].ID = int(id) |
| 1139 | } |
| 1140 | mutation.done = true |
| 1141 | return nodes[i], nil |
| 1142 | }) |
| 1143 | for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 1144 | mut = builder.hooks[i](mut) |
| 1145 | } |
| 1146 | mutators[i] = mut |
| 1147 | }(i, ctx) |
| 1148 | } |
| 1149 | if len(mutators) > 0 { |
| 1150 | if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { |
| 1151 | return nil, err |
| 1152 | } |
| 1153 | } |
| 1154 | return nodes, nil |
| 1155 | } |