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

Method Save

ent/share_create.go:820–877  ·  view source on GitHub ↗

Save creates the Share entities in the database.

(ctx context.Context)

Source from the content-addressed store, hash-verified

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

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