Save creates the Zoo entities in the database.
(ctx context.Context)
| 106 | |
| 107 | // Save creates the Zoo entities in the database. |
| 108 | func (_c *ZooCreateBulk) Save(ctx context.Context) ([]*Zoo, error) { |
| 109 | if _c.err != nil { |
| 110 | return nil, _c.err |
| 111 | } |
| 112 | specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
| 113 | nodes := make([]*Zoo, len(_c.builders)) |
| 114 | mutators := make([]Mutator, len(_c.builders)) |
| 115 | for i := range _c.builders { |
| 116 | func(i int, root context.Context) { |
| 117 | builder := _c.builders[i] |
| 118 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 119 | mutation, ok := m.(*ZooMutation) |
| 120 | if !ok { |
| 121 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 122 | } |
| 123 | if err := builder.check(); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | builder.mutation = mutation |
| 127 | var err error |
| 128 | nodes[i], specs[i] = builder.createSpec() |
| 129 | if i < len(mutators)-1 { |
| 130 | _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) |
| 131 | } else { |
| 132 | spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 133 | // Invoke the actual operation on the latest mutation in the chain. |
| 134 | if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { |
| 135 | if sqlgraph.IsConstraintError(err) { |
| 136 | err = &ConstraintError{msg: err.Error(), wrap: err} |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | mutation.id = &nodes[i].ID |
| 144 | if specs[i].ID.Value != nil && nodes[i].ID == 0 { |
| 145 | id := specs[i].ID.Value.(int64) |
| 146 | nodes[i].ID = int(id) |
| 147 | } |
| 148 | mutation.done = true |
| 149 | return nodes[i], nil |
| 150 | }) |
| 151 | for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 152 | mut = builder.hooks[i](mut) |
| 153 | } |
| 154 | mutators[i] = mut |
| 155 | }(i, ctx) |
| 156 | } |
| 157 | if len(mutators) > 0 { |
| 158 | if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | } |
| 162 | return nodes, nil |
| 163 | } |
| 164 | |
| 165 | // SaveX is like Save, but panics if an error occurs. |
no test coverage detected