mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
195 lines
4.6 KiB
Go
195 lines
4.6 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/gremlin"
|
|
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
|
|
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/g"
|
|
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/item"
|
|
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/predicate"
|
|
)
|
|
|
|
// ItemUpdate is the builder for updating Item entities.
|
|
type ItemUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *ItemMutation
|
|
predicates []predicate.Item
|
|
}
|
|
|
|
// Where adds a new predicate for the builder.
|
|
func (iu *ItemUpdate) Where(ps ...predicate.Item) *ItemUpdate {
|
|
iu.predicates = append(iu.predicates, ps...)
|
|
return iu
|
|
}
|
|
|
|
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
|
func (iu *ItemUpdate) Save(ctx context.Context) (int, error) {
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(iu.hooks) == 0 {
|
|
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)
|
|
}
|
|
iu.mutation = mutation
|
|
affected, err = iu.gremlinSave(ctx)
|
|
return affected, err
|
|
})
|
|
for i := len(iu.hooks) - 1; i >= 0; i-- {
|
|
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)
|
|
}
|
|
}
|
|
|
|
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 {
|
|
v := g.V().HasLabel(item.Label)
|
|
for _, p := range iu.predicates {
|
|
p(v)
|
|
}
|
|
var (
|
|
trs []*dsl.Traversal
|
|
)
|
|
v.Count()
|
|
trs = append(trs, v)
|
|
return dsl.Join(trs...)
|
|
}
|
|
|
|
// ItemUpdateOne is the builder for updating a single Item entity.
|
|
type ItemUpdateOne struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *ItemMutation
|
|
}
|
|
|
|
// Save executes the query and returns the updated entity.
|
|
func (iuo *ItemUpdateOne) Save(ctx context.Context) (*Item, error) {
|
|
var (
|
|
err error
|
|
node *Item
|
|
)
|
|
if len(iuo.hooks) == 0 {
|
|
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)
|
|
}
|
|
iuo.mutation = mutation
|
|
node, err = iuo.gremlinSave(ctx)
|
|
return node, err
|
|
})
|
|
for i := len(iuo.hooks) - 1; i >= 0; i-- {
|
|
mut = iuo.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, iuo.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (iuo *ItemUpdateOne) SaveX(ctx context.Context) *Item {
|
|
i, err := iuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
func (iuo *ItemUpdateOne) gremlinSave(ctx context.Context) (*Item, error) {
|
|
res := &gremlin.Response{}
|
|
id, ok := iuo.mutation.ID()
|
|
if !ok {
|
|
return nil, fmt.Errorf("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 {
|
|
v := g.V(id)
|
|
var (
|
|
trs []*dsl.Traversal
|
|
)
|
|
v.ValueMap(true)
|
|
trs = append(trs, v)
|
|
return dsl.Join(trs...)
|
|
}
|