MCPcopy Index your code
hub / github.com/cloudreve/cloudreve / Save

Method Save

ent/node_create.go:815–872  ·  view source on GitHub ↗

Save creates the Node entities in the database.

(ctx context.Context)

Source from the content-addressed store, hash-verified

813
814// Save creates the Node entities in the database.
815func (ncb *NodeCreateBulk) Save(ctx context.Context) ([]*Node, error) {
816 if ncb.err != nil {
817 return nil, ncb.err
818 }
819 specs := make([]*sqlgraph.CreateSpec, len(ncb.builders))
820 nodes := make([]*Node, len(ncb.builders))
821 mutators := make([]Mutator, len(ncb.builders))
822 for i := range ncb.builders {
823 func(i int, root context.Context) {
824 builder := ncb.builders[i]
825 builder.defaults()
826 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
827 mutation, ok := m.(*NodeMutation)
828 if !ok {
829 return nil, fmt.Errorf("unexpected mutation type %T", m)
830 }
831 if err := builder.check(); err != nil {
832 return nil, err
833 }
834 builder.mutation = mutation
835 var err error
836 nodes[i], specs[i] = builder.createSpec()
837 if i < len(mutators)-1 {
838 _, err = mutators[i+1].Mutate(root, ncb.builders[i+1].mutation)
839 } else {
840 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
841 spec.OnConflict = ncb.conflict
842 // Invoke the actual operation on the latest mutation in the chain.
843 if err = sqlgraph.BatchCreate(ctx, ncb.driver, spec); err != nil {
844 if sqlgraph.IsConstraintError(err) {
845 err = &ConstraintError{msg: err.Error(), wrap: err}
846 }
847 }
848 }
849 if err != nil {
850 return nil, err
851 }
852 mutation.id = &nodes[i].ID
853 if specs[i].ID.Value != nil {
854 id := specs[i].ID.Value.(int64)
855 nodes[i].ID = int(id)
856 }
857 mutation.done = true
858 return nodes[i], nil
859 })
860 for i := len(builder.hooks) - 1; i >= 0; i-- {
861 mut = builder.hooks[i](mut)
862 }
863 mutators[i] = mut
864 }(i, ctx)
865 }
866 if len(mutators) > 0 {
867 if _, err := mutators[0].Mutate(ctx, ncb.builders[0].mutation); err != nil {
868 return nil, err
869 }
870 }
871 return nodes, nil
872}

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