MCPcopy Index your code
hub / github.com/ent/ent / batchAddM2M

Method batchAddM2M

dialect/sql/sqlgraph/graph.go:1742–1790  ·  view source on GitHub ↗
(ctx context.Context, spec *BatchCreateSpec)

Source from the content-addressed store, hash-verified

1740}
1741
1742func (g *graph) batchAddM2M(ctx context.Context, spec *BatchCreateSpec) error {
1743 tables := make(map[string]*sql.InsertBuilder)
1744 for _, node := range spec.Nodes {
1745 edges := EdgeSpecs(node.Edges).FilterRel(M2M)
1746 for name, edges := range edges.GroupTable() {
1747 if len(edges) != 1 {
1748 return fmt.Errorf("expect exactly 1 edge-spec per table, but got %d", len(edges))
1749 }
1750 edge := edges[0]
1751 insert, ok := tables[name]
1752 if !ok {
1753 columns := edge.Columns
1754 // Additional fields, such as edge-schema fields.
1755 for _, f := range edge.Target.Fields {
1756 columns = append(columns, f.Column)
1757 }
1758 insert = g.builder.Insert(name).Columns(columns...)
1759 if edge.Schema != "" {
1760 // If the Schema field was provided to the EdgeSpec (by the
1761 // generated code), it should be the same for all EdgeSpecs.
1762 insert.Schema(edge.Schema)
1763 }
1764 // Ignore conflicts only if edges do not contain extra fields, because these fields
1765 // can hold different values on different insertions (e.g. time.Now() or uuid.New()).
1766 if len(edge.Target.Fields) == 0 {
1767 insert.OnConflict(sql.DoNothing())
1768 }
1769 }
1770 tables[name] = insert
1771 pk1, pk2 := []driver.Value{node.ID.Value}, edge.Target.Nodes
1772 if edge.Inverse {
1773 pk1, pk2 = pk2, pk1
1774 }
1775 for _, pair := range product(pk1, pk2) {
1776 insert.Values(append([]any{pair[0], pair[1]}, edge.Target.FieldValues()...)...)
1777 if edge.Bidi {
1778 insert.Values(append([]any{pair[1], pair[0]}, edge.Target.FieldValues()...)...)
1779 }
1780 }
1781 }
1782 }
1783 for _, table := range insertKeys(tables) {
1784 query, args := tables[table].Query()
1785 if err := g.tx.Exec(ctx, query, args, nil); err != nil {
1786 return fmt.Errorf("add m2m edge for table %s: %w", table, err)
1787 }
1788 }
1789 return nil
1790}
1791
1792func (g *graph) clearFKEdges(ctx context.Context, ids []driver.Value, edges []*EdgeSpec) error {
1793 for _, edge := range edges {

Callers 1

nodesMethod · 0.80

Calls 14

DoNothingFunction · 0.92
EdgeSpecsTypeAlias · 0.85
productFunction · 0.85
insertKeysFunction · 0.85
FilterRelMethod · 0.80
GroupTableMethod · 0.80
InsertMethod · 0.80
FieldValuesMethod · 0.80
ColumnsMethod · 0.65
ValuesMethod · 0.65
QueryMethod · 0.65
ExecMethod · 0.65

Tested by

no test coverage detected