Files
ent/entc/integration/gremlin/ent/item_update.go
2022-08-19 18:23:04 +03:00

370 lines
9.5 KiB
Go

// Copyright 2019-present Facebook Inc. 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 ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/gremlin"
"entgo.io/ent/dialect/gremlin/graph/dsl"
"entgo.io/ent/dialect/gremlin/graph/dsl/__"
"entgo.io/ent/dialect/gremlin/graph/dsl/g"
"entgo.io/ent/dialect/gremlin/graph/dsl/p"
"entgo.io/ent/entc/integration/gremlin/ent/item"
"entgo.io/ent/entc/integration/gremlin/ent/predicate"
)
// ItemUpdate is the builder for updating Item entities.
type ItemUpdate struct {
config
hooks []Hook
mutation *ItemMutation
}
// Where appends a list predicates to the ItemUpdate builder.
func (iu *ItemUpdate) Where(ps ...predicate.Item) *ItemUpdate {
iu.mutation.Where(ps...)
return iu
}
// SetText sets the "text" field.
func (iu *ItemUpdate) SetText(s string) *ItemUpdate {
iu.mutation.SetText(s)
return iu
}
// SetNillableText sets the "text" field if the given value is not nil.
func (iu *ItemUpdate) SetNillableText(s *string) *ItemUpdate {
if s != nil {
iu.SetText(*s)
}
return iu
}
// ClearText clears the value of the "text" field.
func (iu *ItemUpdate) ClearText() *ItemUpdate {
iu.mutation.ClearText()
return iu
}
// Mutation returns the ItemMutation object of the builder.
func (iu *ItemUpdate) Mutation() *ItemMutation {
return iu.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (iu *ItemUpdate) Save(ctx context.Context) (int, error) {
var (
err error
affected int
)
if len(iu.hooks) == 0 {
if err = iu.check(); err != nil {
return 0, err
}
affected, err = iu.gremlinSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ItemMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = iu.check(); err != nil {
return 0, err
}
iu.mutation = mutation
affected, err = iu.gremlinSave(ctx)
mutation.done = true
return affected, err
})
for i := len(iu.hooks) - 1; i >= 0; i-- {
if iu.hooks[i] == nil {
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = iu.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, iu.mutation); err != nil {
return 0, err
}
}
return affected, err
}
// SaveX is like Save, but panics if an error occurs.
func (iu *ItemUpdate) SaveX(ctx context.Context) int {
affected, err := iu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (iu *ItemUpdate) Exec(ctx context.Context) error {
_, err := iu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (iu *ItemUpdate) ExecX(ctx context.Context) {
if err := iu.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (iu *ItemUpdate) check() error {
if v, ok := iu.mutation.Text(); ok {
if err := item.TextValidator(v); err != nil {
return &ValidationError{Name: "text", err: fmt.Errorf(`ent: validator failed for field "Item.text": %w`, err)}
}
}
return nil
}
func (iu *ItemUpdate) gremlinSave(ctx context.Context) (int, error) {
res := &gremlin.Response{}
query, bindings := iu.gremlin().Query()
if err := iu.driver.Exec(ctx, query, bindings, res); err != nil {
return 0, err
}
if err, ok := isConstantError(res); ok {
return 0, err
}
return res.ReadInt()
}
func (iu *ItemUpdate) gremlin() *dsl.Traversal {
type constraint struct {
pred *dsl.Traversal // constraint predicate.
test *dsl.Traversal // test matches and its constant.
}
constraints := make([]*constraint, 0, 1)
v := g.V().HasLabel(item.Label)
for _, p := range iu.mutation.predicates {
p(v)
}
var (
rv = v.Clone()
_ = rv
trs []*dsl.Traversal
)
if value, ok := iu.mutation.Text(); ok {
constraints = append(constraints, &constraint{
pred: g.V().Has(item.Label, item.FieldText, value).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(item.Label, item.FieldText, value)),
})
v.Property(dsl.Single, item.FieldText, value)
}
var properties []any
if iu.mutation.TextCleared() {
properties = append(properties, item.FieldText)
}
if len(properties) > 0 {
v.SideEffect(__.Properties(properties...).Drop())
}
v.Count()
if len(constraints) > 0 {
constraints = append(constraints, &constraint{
pred: rv.Count(),
test: __.Is(p.GT(1)).Constant(&ConstraintError{msg: "update traversal contains more than one vertex"}),
})
v = constraints[0].pred.Coalesce(constraints[0].test, v)
for _, cr := range constraints[1:] {
v = cr.pred.Coalesce(cr.test, v)
}
}
trs = append(trs, v)
return dsl.Join(trs...)
}
// ItemUpdateOne is the builder for updating a single Item entity.
type ItemUpdateOne struct {
config
fields []string
hooks []Hook
mutation *ItemMutation
}
// SetText sets the "text" field.
func (iuo *ItemUpdateOne) SetText(s string) *ItemUpdateOne {
iuo.mutation.SetText(s)
return iuo
}
// SetNillableText sets the "text" field if the given value is not nil.
func (iuo *ItemUpdateOne) SetNillableText(s *string) *ItemUpdateOne {
if s != nil {
iuo.SetText(*s)
}
return iuo
}
// ClearText clears the value of the "text" field.
func (iuo *ItemUpdateOne) ClearText() *ItemUpdateOne {
iuo.mutation.ClearText()
return iuo
}
// Mutation returns the ItemMutation object of the builder.
func (iuo *ItemUpdateOne) Mutation() *ItemMutation {
return iuo.mutation
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (iuo *ItemUpdateOne) Select(field string, fields ...string) *ItemUpdateOne {
iuo.fields = append([]string{field}, fields...)
return iuo
}
// Save executes the query and returns the updated Item entity.
func (iuo *ItemUpdateOne) Save(ctx context.Context) (*Item, error) {
var (
err error
node *Item
)
if len(iuo.hooks) == 0 {
if err = iuo.check(); err != nil {
return nil, err
}
node, err = iuo.gremlinSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ItemMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = iuo.check(); err != nil {
return nil, err
}
iuo.mutation = mutation
node, err = iuo.gremlinSave(ctx)
mutation.done = true
return node, err
})
for i := len(iuo.hooks) - 1; i >= 0; i-- {
if iuo.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = iuo.hooks[i](mut)
}
v, err := mut.Mutate(ctx, iuo.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*Item)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from ItemMutation", v)
}
node = nv
}
return node, err
}
// SaveX is like Save, but panics if an error occurs.
func (iuo *ItemUpdateOne) SaveX(ctx context.Context) *Item {
node, err := iuo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (iuo *ItemUpdateOne) Exec(ctx context.Context) error {
_, err := iuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (iuo *ItemUpdateOne) ExecX(ctx context.Context) {
if err := iuo.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (iuo *ItemUpdateOne) check() error {
if v, ok := iuo.mutation.Text(); ok {
if err := item.TextValidator(v); err != nil {
return &ValidationError{Name: "text", err: fmt.Errorf(`ent: validator failed for field "Item.text": %w`, err)}
}
}
return nil
}
func (iuo *ItemUpdateOne) gremlinSave(ctx context.Context) (*Item, error) {
res := &gremlin.Response{}
id, ok := iuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Item.id" for update`)}
}
query, bindings := iuo.gremlin(id).Query()
if err := iuo.driver.Exec(ctx, query, bindings, res); err != nil {
return nil, err
}
if err, ok := isConstantError(res); ok {
return nil, err
}
i := &Item{config: iuo.config}
if err := i.FromResponse(res); err != nil {
return nil, err
}
return i, nil
}
func (iuo *ItemUpdateOne) gremlin(id string) *dsl.Traversal {
type constraint struct {
pred *dsl.Traversal // constraint predicate.
test *dsl.Traversal // test matches and its constant.
}
constraints := make([]*constraint, 0, 1)
v := g.V(id)
var (
rv = v.Clone()
_ = rv
trs []*dsl.Traversal
)
if value, ok := iuo.mutation.Text(); ok {
constraints = append(constraints, &constraint{
pred: g.V().Has(item.Label, item.FieldText, value).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(item.Label, item.FieldText, value)),
})
v.Property(dsl.Single, item.FieldText, value)
}
var properties []any
if iuo.mutation.TextCleared() {
properties = append(properties, item.FieldText)
}
if len(properties) > 0 {
v.SideEffect(__.Properties(properties...).Drop())
}
if len(iuo.fields) > 0 {
fields := make([]any, 0, len(iuo.fields)+1)
fields = append(fields, true)
for _, f := range iuo.fields {
fields = append(fields, f)
}
v.ValueMap(fields...)
} else {
v.ValueMap(true)
}
if len(constraints) > 0 {
v = constraints[0].pred.Coalesce(constraints[0].test, v)
for _, cr := range constraints[1:] {
v = cr.pred.Coalesce(cr.test, v)
}
}
trs = append(trs, v)
return dsl.Join(trs...)
}