Files
ent/entc/integration/ent/item_update.go
2021-03-14 23:17:30 +02:00

237 lines
5.9 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"
"fmt"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/ent/item"
"entgo.io/ent/entc/integration/ent/predicate"
"entgo.io/ent/schema/field"
)
// ItemUpdate is the builder for updating Item entities.
type ItemUpdate struct {
config
hooks []Hook
mutation *ItemMutation
}
// Where adds a new predicate for the ItemUpdate builder.
func (iu *ItemUpdate) Where(ps ...predicate.Item) *ItemUpdate {
iu.mutation.predicates = append(iu.mutation.predicates, ps...)
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 {
affected, err = iu.sqlSave(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.sqlSave(ctx)
mutation.done = true
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) sqlSave(ctx context.Context) (n int, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: item.Table,
Columns: item.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: item.FieldID,
},
},
}
if ps := iu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if n, err = sqlgraph.UpdateNodes(ctx, iu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{item.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return 0, err
}
return n, nil
}
// ItemUpdateOne is the builder for updating a single Item entity.
type ItemUpdateOne struct {
config
fields []string
hooks []Hook
mutation *ItemMutation
}
// 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 {
node, err = iuo.sqlSave(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.sqlSave(ctx)
mutation.done = true
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 {
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)
}
}
func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (_node *Item, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: item.Table,
Columns: item.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: item.FieldID,
},
},
}
id, ok := iuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Item.ID for update")}
}
_spec.Node.ID.Value = id
if fields := iuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, item.FieldID)
for _, f := range fields {
if !item.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != item.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := iuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
_node = &Item{config: iuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, iuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{item.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
return _node, nil
}