dialect/sql/sqlgraph: delete nodes in the graph

This commit is contained in:
Ariel Mashraki
2019-12-11 15:38:29 +02:00
parent ddd69ec90a
commit 9f324ce030
2 changed files with 51 additions and 1 deletions

View File

@@ -352,7 +352,7 @@ type (
}
// UpdateSpec holds the information for updating one
// or more nodes in the graph in the graph.
// or more nodes in the graph.
UpdateSpec struct {
Node *NodeSpec
Edges EdgeMut
@@ -393,6 +393,39 @@ func UpdateNodes(ctx context.Context, drv dialect.Driver, spec *UpdateSpec) (int
return affected, tx.Commit()
}
// DeleteSpec holds the information for delete one
// or more nodes in the graph.
type DeleteSpec struct {
Node *NodeSpec
Predicate func(*Selector)
}
// DeleteNodes applies the DeleteSpec on the graph.
func DeleteNodes(ctx context.Context, drv dialect.Driver, spec *DeleteSpec) (int, error) {
tx, err := drv.Tx(ctx)
if err != nil {
return 0, err
}
var (
res Result
builder = Dialect(drv.Dialect())
)
selector := builder.Select().
From(builder.Table(spec.Node.Table))
if pred := spec.Predicate; pred != nil {
pred(selector)
}
query, args := builder.Delete(spec.Node.Table).FromSelect(selector).Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return 0, rollback(tx, err)
}
affected, err := res.RowsAffected()
if err != nil {
return 0, rollback(tx, err)
}
return int(affected), tx.Commit()
}
type updater struct {
graph
*UpdateSpec