mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* entc/hooks: initial work for mutations and hooks * ent/schema: adding policy to schema * ent: change op string to uint * entc: move entschema to runtime and enable smooth transition * entc/privacy: adding privacy template * all: goimports * intg/hooks: mutation client/tx and basic schema tests * ent/privacy: adding more verbose decisions * entc/gen: edge-ids getter and additional tests * all: regen assets * entc/gen: fix client hookd propagation * intg: add deletion example * intg/privacy: remove old entschema package * typed privacy * ent/privacy: hooks shouldn't be called on privacy deny * entc/gen: fix schema hooks invocation order * remove read policy from public api * update circleci go orb Co-authored-by: Ariel Mashraki <ariel@mashraki.co.il>
511 lines
13 KiB
Go
511 lines
13 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
// This source code is licensed under the Apache 2.0 license found
|
|
// in the LICENSE file in the root directory of this source tree.
|
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql"
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebookincubator/ent/examples/o2mrecur/ent/node"
|
|
"github.com/facebookincubator/ent/examples/o2mrecur/ent/predicate"
|
|
"github.com/facebookincubator/ent/schema/field"
|
|
)
|
|
|
|
// NodeUpdate is the builder for updating Node entities.
|
|
type NodeUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *NodeMutation
|
|
predicates []predicate.Node
|
|
}
|
|
|
|
// Where adds a new predicate for the builder.
|
|
func (nu *NodeUpdate) Where(ps ...predicate.Node) *NodeUpdate {
|
|
nu.predicates = append(nu.predicates, ps...)
|
|
return nu
|
|
}
|
|
|
|
// SetValue sets the value field.
|
|
func (nu *NodeUpdate) SetValue(i int) *NodeUpdate {
|
|
nu.mutation.ResetValue()
|
|
nu.mutation.SetValue(i)
|
|
return nu
|
|
}
|
|
|
|
// AddValue adds i to value.
|
|
func (nu *NodeUpdate) AddValue(i int) *NodeUpdate {
|
|
nu.mutation.AddValue(i)
|
|
return nu
|
|
}
|
|
|
|
// SetParentID sets the parent edge to Node by id.
|
|
func (nu *NodeUpdate) SetParentID(id int) *NodeUpdate {
|
|
nu.mutation.SetParentID(id)
|
|
return nu
|
|
}
|
|
|
|
// SetNillableParentID sets the parent edge to Node by id if the given value is not nil.
|
|
func (nu *NodeUpdate) SetNillableParentID(id *int) *NodeUpdate {
|
|
if id != nil {
|
|
nu = nu.SetParentID(*id)
|
|
}
|
|
return nu
|
|
}
|
|
|
|
// SetParent sets the parent edge to Node.
|
|
func (nu *NodeUpdate) SetParent(n *Node) *NodeUpdate {
|
|
return nu.SetParentID(n.ID)
|
|
}
|
|
|
|
// AddChildIDs adds the children edge to Node by ids.
|
|
func (nu *NodeUpdate) AddChildIDs(ids ...int) *NodeUpdate {
|
|
nu.mutation.AddChildIDs(ids...)
|
|
return nu
|
|
}
|
|
|
|
// AddChildren adds the children edges to Node.
|
|
func (nu *NodeUpdate) AddChildren(n ...*Node) *NodeUpdate {
|
|
ids := make([]int, len(n))
|
|
for i := range n {
|
|
ids[i] = n[i].ID
|
|
}
|
|
return nu.AddChildIDs(ids...)
|
|
}
|
|
|
|
// ClearParent clears the parent edge to Node.
|
|
func (nu *NodeUpdate) ClearParent() *NodeUpdate {
|
|
nu.mutation.ClearParent()
|
|
return nu
|
|
}
|
|
|
|
// RemoveChildIDs removes the children edge to Node by ids.
|
|
func (nu *NodeUpdate) RemoveChildIDs(ids ...int) *NodeUpdate {
|
|
nu.mutation.RemoveChildIDs(ids...)
|
|
return nu
|
|
}
|
|
|
|
// RemoveChildren removes children edges to Node.
|
|
func (nu *NodeUpdate) RemoveChildren(n ...*Node) *NodeUpdate {
|
|
ids := make([]int, len(n))
|
|
for i := range n {
|
|
ids[i] = n[i].ID
|
|
}
|
|
return nu.RemoveChildIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
|
func (nu *NodeUpdate) Save(ctx context.Context) (int, error) {
|
|
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(nu.hooks) == 0 {
|
|
affected, err = nu.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*NodeMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
nu.mutation = mutation
|
|
affected, err = nu.sqlSave(ctx)
|
|
return affected, err
|
|
})
|
|
for i := len(nu.hooks); i > 0; i-- {
|
|
mut = nu.hooks[i-1](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, nu.mutation); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return affected, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (nu *NodeUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := nu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (nu *NodeUpdate) Exec(ctx context.Context) error {
|
|
_, err := nu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (nu *NodeUpdate) ExecX(ctx context.Context) {
|
|
if err := nu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: node.Table,
|
|
Columns: node.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
if ps := nu.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := nu.mutation.Value(); ok {
|
|
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: node.FieldValue,
|
|
})
|
|
}
|
|
if value, ok := nu.mutation.AddedValue(); ok {
|
|
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: node.FieldValue,
|
|
})
|
|
}
|
|
if nu.mutation.ParentCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: node.ParentTable,
|
|
Columns: []string{node.ParentColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := nu.mutation.ParentIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: node.ParentTable,
|
|
Columns: []string{node.ParentColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
if nodes := nu.mutation.RemovedChildrenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: node.ChildrenTable,
|
|
Columns: []string{node.ChildrenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := nu.mutation.ChildrenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: node.ChildrenTable,
|
|
Columns: []string{node.ChildrenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
if n, err = sqlgraph.UpdateNodes(ctx, nu.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{node.Label}
|
|
} else if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return 0, err
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// NodeUpdateOne is the builder for updating a single Node entity.
|
|
type NodeUpdateOne struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *NodeMutation
|
|
}
|
|
|
|
// SetValue sets the value field.
|
|
func (nuo *NodeUpdateOne) SetValue(i int) *NodeUpdateOne {
|
|
nuo.mutation.ResetValue()
|
|
nuo.mutation.SetValue(i)
|
|
return nuo
|
|
}
|
|
|
|
// AddValue adds i to value.
|
|
func (nuo *NodeUpdateOne) AddValue(i int) *NodeUpdateOne {
|
|
nuo.mutation.AddValue(i)
|
|
return nuo
|
|
}
|
|
|
|
// SetParentID sets the parent edge to Node by id.
|
|
func (nuo *NodeUpdateOne) SetParentID(id int) *NodeUpdateOne {
|
|
nuo.mutation.SetParentID(id)
|
|
return nuo
|
|
}
|
|
|
|
// SetNillableParentID sets the parent edge to Node by id if the given value is not nil.
|
|
func (nuo *NodeUpdateOne) SetNillableParentID(id *int) *NodeUpdateOne {
|
|
if id != nil {
|
|
nuo = nuo.SetParentID(*id)
|
|
}
|
|
return nuo
|
|
}
|
|
|
|
// SetParent sets the parent edge to Node.
|
|
func (nuo *NodeUpdateOne) SetParent(n *Node) *NodeUpdateOne {
|
|
return nuo.SetParentID(n.ID)
|
|
}
|
|
|
|
// AddChildIDs adds the children edge to Node by ids.
|
|
func (nuo *NodeUpdateOne) AddChildIDs(ids ...int) *NodeUpdateOne {
|
|
nuo.mutation.AddChildIDs(ids...)
|
|
return nuo
|
|
}
|
|
|
|
// AddChildren adds the children edges to Node.
|
|
func (nuo *NodeUpdateOne) AddChildren(n ...*Node) *NodeUpdateOne {
|
|
ids := make([]int, len(n))
|
|
for i := range n {
|
|
ids[i] = n[i].ID
|
|
}
|
|
return nuo.AddChildIDs(ids...)
|
|
}
|
|
|
|
// ClearParent clears the parent edge to Node.
|
|
func (nuo *NodeUpdateOne) ClearParent() *NodeUpdateOne {
|
|
nuo.mutation.ClearParent()
|
|
return nuo
|
|
}
|
|
|
|
// RemoveChildIDs removes the children edge to Node by ids.
|
|
func (nuo *NodeUpdateOne) RemoveChildIDs(ids ...int) *NodeUpdateOne {
|
|
nuo.mutation.RemoveChildIDs(ids...)
|
|
return nuo
|
|
}
|
|
|
|
// RemoveChildren removes children edges to Node.
|
|
func (nuo *NodeUpdateOne) RemoveChildren(n ...*Node) *NodeUpdateOne {
|
|
ids := make([]int, len(n))
|
|
for i := range n {
|
|
ids[i] = n[i].ID
|
|
}
|
|
return nuo.RemoveChildIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the updated entity.
|
|
func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) {
|
|
|
|
var (
|
|
err error
|
|
node *Node
|
|
)
|
|
if len(nuo.hooks) == 0 {
|
|
node, err = nuo.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*NodeMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
nuo.mutation = mutation
|
|
node, err = nuo.sqlSave(ctx)
|
|
return node, err
|
|
})
|
|
for i := len(nuo.hooks); i > 0; i-- {
|
|
mut = nuo.hooks[i-1](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, nuo.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (nuo *NodeUpdateOne) SaveX(ctx context.Context) *Node {
|
|
n, err := nuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (nuo *NodeUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := nuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (nuo *NodeUpdateOne) ExecX(ctx context.Context) {
|
|
if err := nuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: node.Table,
|
|
Columns: node.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
id, ok := nuo.mutation.ID()
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing Node.ID for update")
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if value, ok := nuo.mutation.Value(); ok {
|
|
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: node.FieldValue,
|
|
})
|
|
}
|
|
if value, ok := nuo.mutation.AddedValue(); ok {
|
|
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: value,
|
|
Column: node.FieldValue,
|
|
})
|
|
}
|
|
if nuo.mutation.ParentCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: node.ParentTable,
|
|
Columns: []string{node.ParentColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := nuo.mutation.ParentIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: node.ParentTable,
|
|
Columns: []string{node.ParentColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
if nodes := nuo.mutation.RemovedChildrenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: node.ChildrenTable,
|
|
Columns: []string{node.ChildrenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := nuo.mutation.ChildrenIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: node.ChildrenTable,
|
|
Columns: []string{node.ChildrenColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: node.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
n = &Node{config: nuo.config}
|
|
_spec.Assign = n.assignValues
|
|
_spec.ScanValues = n.scanValues()
|
|
if err = sqlgraph.UpdateNode(ctx, nuo.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{node.Label}
|
|
} else if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return nil, err
|
|
}
|
|
return n, nil
|
|
}
|