(ctx context.Context, drv dialect.Driver)
| 1185 | } |
| 1186 | |
| 1187 | func (u *updater) nodes(ctx context.Context, drv dialect.Driver) (int, error) { |
| 1188 | var ( |
| 1189 | addEdges = EdgeSpecs(u.Edges.Add).GroupRel() |
| 1190 | clearEdges = EdgeSpecs(u.Edges.Clear).GroupRel() |
| 1191 | multiple = hasExternalEdges(addEdges, clearEdges) |
| 1192 | update = u.builder.Update(u.Node.Table).Schema(u.Node.Schema) |
| 1193 | selector = u.builder.Select(). |
| 1194 | From(u.builder.Table(u.Node.Table).Schema(u.Node.Schema)). |
| 1195 | WithContext(ctx) |
| 1196 | ) |
| 1197 | switch { |
| 1198 | // In case it is not an edge schema, the id holds the PK of |
| 1199 | // the returned nodes are used for updating external tables. |
| 1200 | case u.Node.ID != nil: |
| 1201 | selector.Select(u.Node.ID.Column) |
| 1202 | case len(u.Node.CompositeID) == 2: |
| 1203 | // Other edge-schemas (M2M tables) cannot be updated by this operation. |
| 1204 | // Also, in case there is a need to update an external foreign-key, it must |
| 1205 | // be a single value and the user should use the "update by id" API instead. |
| 1206 | if multiple { |
| 1207 | return 0, fmt.Errorf("sql/sqlgraph: update edge schema table %q cannot update external tables", u.Node.Table) |
| 1208 | } |
| 1209 | case len(u.Node.CompositeID) != 2: |
| 1210 | return 0, fmt.Errorf("sql/sqlgraph: invalid composite id for update table %q", u.Node.Table) |
| 1211 | default: |
| 1212 | return 0, fmt.Errorf("sql/sqlgraph: missing node id for update table %q", u.Node.Table) |
| 1213 | } |
| 1214 | if err := u.setTableColumns(update, addEdges, clearEdges); err != nil { |
| 1215 | return 0, err |
| 1216 | } |
| 1217 | if pred := u.Predicate; pred != nil { |
| 1218 | pred(selector) |
| 1219 | } |
| 1220 | // In case of single statement update, avoid opening a transaction manually. |
| 1221 | if !multiple { |
| 1222 | update.FromSelect(selector) |
| 1223 | return u.updateTable(ctx, update) |
| 1224 | } |
| 1225 | tx, err := drv.Tx(ctx) |
| 1226 | if err != nil { |
| 1227 | return 0, err |
| 1228 | } |
| 1229 | u.tx = tx |
| 1230 | affected, err := func() (int, error) { |
| 1231 | var ( |
| 1232 | ids []driver.Value |
| 1233 | rows = &sql.Rows{} |
| 1234 | query, args = selector.Query() |
| 1235 | ) |
| 1236 | if err := u.tx.Query(ctx, query, args, rows); err != nil { |
| 1237 | return 0, fmt.Errorf("querying table %s: %w", u.Node.Table, err) |
| 1238 | } |
| 1239 | defer rows.Close() |
| 1240 | if err := sql.ScanSlice(rows, &ids); err != nil { |
| 1241 | return 0, fmt.Errorf("scan node ids: %w", err) |
| 1242 | } |
| 1243 | if err := rows.Close(); err != nil { |
| 1244 | return 0, err |
no test coverage detected