mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* Failing test * Fix propagating entirely models from OpCreate hook * Apply suggestions from code review Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * whitespace * Failing test for updateone * fix for updateone * Regnerate * regen from root Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
325 lines
8.4 KiB
Go
325 lines
8.4 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 entc, 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/entc/integration/ent/schema/task"
|
|
"entgo.io/ent/entc/integration/gremlin/ent/predicate"
|
|
|
|
enttask "entgo.io/ent/entc/integration/gremlin/ent/task"
|
|
)
|
|
|
|
// TaskUpdate is the builder for updating Task entities.
|
|
type TaskUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *TaskMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the TaskUpdate builder.
|
|
func (tu *TaskUpdate) Where(ps ...predicate.Task) *TaskUpdate {
|
|
tu.mutation.Where(ps...)
|
|
return tu
|
|
}
|
|
|
|
// SetPriority sets the "priority" field.
|
|
func (tu *TaskUpdate) SetPriority(t task.Priority) *TaskUpdate {
|
|
tu.mutation.ResetPriority()
|
|
tu.mutation.SetPriority(t)
|
|
return tu
|
|
}
|
|
|
|
// SetNillablePriority sets the "priority" field if the given value is not nil.
|
|
func (tu *TaskUpdate) SetNillablePriority(t *task.Priority) *TaskUpdate {
|
|
if t != nil {
|
|
tu.SetPriority(*t)
|
|
}
|
|
return tu
|
|
}
|
|
|
|
// AddPriority adds t to the "priority" field.
|
|
func (tu *TaskUpdate) AddPriority(t task.Priority) *TaskUpdate {
|
|
tu.mutation.AddPriority(t)
|
|
return tu
|
|
}
|
|
|
|
// Mutation returns the TaskMutation object of the builder.
|
|
func (tu *TaskUpdate) Mutation() *TaskMutation {
|
|
return tu.mutation
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (tu *TaskUpdate) Save(ctx context.Context) (int, error) {
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(tu.hooks) == 0 {
|
|
if err = tu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
affected, err = tu.gremlinSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*TaskMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = tu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
tu.mutation = mutation
|
|
affected, err = tu.gremlinSave(ctx)
|
|
mutation.done = true
|
|
return affected, err
|
|
})
|
|
for i := len(tu.hooks) - 1; i >= 0; i-- {
|
|
if tu.hooks[i] == nil {
|
|
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = tu.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, tu.mutation); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return affected, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (tu *TaskUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := tu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (tu *TaskUpdate) Exec(ctx context.Context) error {
|
|
_, err := tu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tu *TaskUpdate) ExecX(ctx context.Context) {
|
|
if err := tu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tu *TaskUpdate) check() error {
|
|
if v, ok := tu.mutation.Priority(); ok {
|
|
if err := enttask.PriorityValidator(int(v)); err != nil {
|
|
return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tu *TaskUpdate) gremlinSave(ctx context.Context) (int, error) {
|
|
res := &gremlin.Response{}
|
|
query, bindings := tu.gremlin().Query()
|
|
if err := tu.driver.Exec(ctx, query, bindings, res); err != nil {
|
|
return 0, err
|
|
}
|
|
if err, ok := isConstantError(res); ok {
|
|
return 0, err
|
|
}
|
|
return res.ReadInt()
|
|
}
|
|
|
|
func (tu *TaskUpdate) gremlin() *dsl.Traversal {
|
|
v := g.V().HasLabel(enttask.Label)
|
|
for _, p := range tu.mutation.predicates {
|
|
p(v)
|
|
}
|
|
var (
|
|
trs []*dsl.Traversal
|
|
)
|
|
if value, ok := tu.mutation.Priority(); ok {
|
|
v.Property(dsl.Single, enttask.FieldPriority, value)
|
|
}
|
|
if value, ok := tu.mutation.AddedPriority(); ok {
|
|
v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum())
|
|
}
|
|
v.Count()
|
|
trs = append(trs, v)
|
|
return dsl.Join(trs...)
|
|
}
|
|
|
|
// TaskUpdateOne is the builder for updating a single Task entity.
|
|
type TaskUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *TaskMutation
|
|
}
|
|
|
|
// SetPriority sets the "priority" field.
|
|
func (tuo *TaskUpdateOne) SetPriority(t task.Priority) *TaskUpdateOne {
|
|
tuo.mutation.ResetPriority()
|
|
tuo.mutation.SetPriority(t)
|
|
return tuo
|
|
}
|
|
|
|
// SetNillablePriority sets the "priority" field if the given value is not nil.
|
|
func (tuo *TaskUpdateOne) SetNillablePriority(t *task.Priority) *TaskUpdateOne {
|
|
if t != nil {
|
|
tuo.SetPriority(*t)
|
|
}
|
|
return tuo
|
|
}
|
|
|
|
// AddPriority adds t to the "priority" field.
|
|
func (tuo *TaskUpdateOne) AddPriority(t task.Priority) *TaskUpdateOne {
|
|
tuo.mutation.AddPriority(t)
|
|
return tuo
|
|
}
|
|
|
|
// Mutation returns the TaskMutation object of the builder.
|
|
func (tuo *TaskUpdateOne) Mutation() *TaskMutation {
|
|
return tuo.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 (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne {
|
|
tuo.fields = append([]string{field}, fields...)
|
|
return tuo
|
|
}
|
|
|
|
// Save executes the query and returns the updated Task entity.
|
|
func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) {
|
|
var (
|
|
err error
|
|
node *Task
|
|
)
|
|
if len(tuo.hooks) == 0 {
|
|
if err = tuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = tuo.gremlinSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*TaskMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = tuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
tuo.mutation = mutation
|
|
node, err = tuo.gremlinSave(ctx)
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(tuo.hooks) - 1; i >= 0; i-- {
|
|
if tuo.hooks[i] == nil {
|
|
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = tuo.hooks[i](mut)
|
|
}
|
|
v, err := mut.Mutate(ctx, tuo.mutation)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nv, ok := v.(*Task)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v)
|
|
}
|
|
node = nv
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (tuo *TaskUpdateOne) SaveX(ctx context.Context) *Task {
|
|
node, err := tuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (tuo *TaskUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := tuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tuo *TaskUpdateOne) ExecX(ctx context.Context) {
|
|
if err := tuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tuo *TaskUpdateOne) check() error {
|
|
if v, ok := tuo.mutation.Priority(); ok {
|
|
if err := enttask.PriorityValidator(int(v)); err != nil {
|
|
return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tuo *TaskUpdateOne) gremlinSave(ctx context.Context) (*Task, error) {
|
|
res := &gremlin.Response{}
|
|
id, ok := tuo.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Task.id" for update`)}
|
|
}
|
|
query, bindings := tuo.gremlin(id).Query()
|
|
if err := tuo.driver.Exec(ctx, query, bindings, res); err != nil {
|
|
return nil, err
|
|
}
|
|
if err, ok := isConstantError(res); ok {
|
|
return nil, err
|
|
}
|
|
t := &Task{config: tuo.config}
|
|
if err := t.FromResponse(res); err != nil {
|
|
return nil, err
|
|
}
|
|
return t, nil
|
|
}
|
|
|
|
func (tuo *TaskUpdateOne) gremlin(id string) *dsl.Traversal {
|
|
v := g.V(id)
|
|
var (
|
|
trs []*dsl.Traversal
|
|
)
|
|
if value, ok := tuo.mutation.Priority(); ok {
|
|
v.Property(dsl.Single, enttask.FieldPriority, value)
|
|
}
|
|
if value, ok := tuo.mutation.AddedPriority(); ok {
|
|
v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum())
|
|
}
|
|
if len(tuo.fields) > 0 {
|
|
fields := make([]interface{}, 0, len(tuo.fields)+1)
|
|
fields = append(fields, true)
|
|
for _, f := range tuo.fields {
|
|
fields = append(fields, f)
|
|
}
|
|
v.ValueMap(fields...)
|
|
} else {
|
|
v.ValueMap(true)
|
|
}
|
|
trs = append(trs, v)
|
|
return dsl.Join(trs...)
|
|
}
|