MCPcopy Create free account
hub / github.com/cloudreve/cloudreve / Save

Method Save

ent/task_create.go:688–745  ·  view source on GitHub ↗

Save creates the Task entities in the database.

(ctx context.Context)

Source from the content-addressed store, hash-verified

686
687// Save creates the Task entities in the database.
688func (tcb *TaskCreateBulk) Save(ctx context.Context) ([]*Task, error) {
689 if tcb.err != nil {
690 return nil, tcb.err
691 }
692 specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
693 nodes := make([]*Task, len(tcb.builders))
694 mutators := make([]Mutator, len(tcb.builders))
695 for i := range tcb.builders {
696 func(i int, root context.Context) {
697 builder := tcb.builders[i]
698 builder.defaults()
699 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
700 mutation, ok := m.(*TaskMutation)
701 if !ok {
702 return nil, fmt.Errorf("unexpected mutation type %T", m)
703 }
704 if err := builder.check(); err != nil {
705 return nil, err
706 }
707 builder.mutation = mutation
708 var err error
709 nodes[i], specs[i] = builder.createSpec()
710 if i < len(mutators)-1 {
711 _, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation)
712 } else {
713 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
714 spec.OnConflict = tcb.conflict
715 // Invoke the actual operation on the latest mutation in the chain.
716 if err = sqlgraph.BatchCreate(ctx, tcb.driver, spec); err != nil {
717 if sqlgraph.IsConstraintError(err) {
718 err = &ConstraintError{msg: err.Error(), wrap: err}
719 }
720 }
721 }
722 if err != nil {
723 return nil, err
724 }
725 mutation.id = &nodes[i].ID
726 if specs[i].ID.Value != nil {
727 id := specs[i].ID.Value.(int64)
728 nodes[i].ID = int(id)
729 }
730 mutation.done = true
731 return nodes[i], nil
732 })
733 for i := len(builder.hooks) - 1; i >= 0; i-- {
734 mut = builder.hooks[i](mut)
735 }
736 mutators[i] = mut
737 }(i, ctx)
738 }
739 if len(mutators) > 0 {
740 if _, err := mutators[0].Mutate(ctx, tcb.builders[0].mutation); err != nil {
741 return nil, err
742 }
743 }
744 return nodes, nil
745}

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