Files
upstream-ent/entc/integration/multischema/ent/parent_update.go
upstream-mirror 4b3ad0b611
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped
snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 22:12:36 +05:00

263 lines
7.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 ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/multischema/ent/internal"
"entgo.io/ent/entc/integration/multischema/ent/parent"
"entgo.io/ent/entc/integration/multischema/ent/predicate"
"entgo.io/ent/schema/field"
)
// ParentUpdate is the builder for updating Parent entities.
type ParentUpdate struct {
config
hooks []Hook
mutation *ParentMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the ParentUpdate builder.
func (_u *ParentUpdate) Where(ps ...predicate.Parent) *ParentUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetByAdoption sets the "by_adoption" field.
func (_u *ParentUpdate) SetByAdoption(v bool) *ParentUpdate {
_u.mutation.SetByAdoption(v)
return _u
}
// SetNillableByAdoption sets the "by_adoption" field if the given value is not nil.
func (_u *ParentUpdate) SetNillableByAdoption(v *bool) *ParentUpdate {
if v != nil {
_u.SetByAdoption(*v)
}
return _u
}
// Mutation returns the ParentMutation object of the builder.
func (_u *ParentUpdate) Mutation() *ParentMutation {
return _u.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *ParentUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *ParentUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *ParentUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *ParentUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *ParentUpdate) check() error {
if _u.mutation.ChildCleared() && len(_u.mutation.ChildIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Parent.child"`)
}
if _u.mutation.ParentCleared() && len(_u.mutation.ParentIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Parent.parent"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *ParentUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ParentUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *ParentUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(parent.Table, parent.Columns, sqlgraph.NewFieldSpec(parent.FieldID, field.TypeInt))
if ps := _u.mutation.Predicates(); len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.ByAdoption(); ok {
_spec.SetField(parent.FieldByAdoption, field.TypeBool, value)
}
_spec.Node.Schema = _u.schemaConfig.Parent
ctx = internal.NewSchemaConfigContext(ctx, _u.schemaConfig)
_spec.AddModifiers(_u.modifiers...)
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{parent.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// ParentUpdateOne is the builder for updating a single Parent entity.
type ParentUpdateOne struct {
config
fields []string
hooks []Hook
mutation *ParentMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetByAdoption sets the "by_adoption" field.
func (_u *ParentUpdateOne) SetByAdoption(v bool) *ParentUpdateOne {
_u.mutation.SetByAdoption(v)
return _u
}
// SetNillableByAdoption sets the "by_adoption" field if the given value is not nil.
func (_u *ParentUpdateOne) SetNillableByAdoption(v *bool) *ParentUpdateOne {
if v != nil {
_u.SetByAdoption(*v)
}
return _u
}
// Mutation returns the ParentMutation object of the builder.
func (_u *ParentUpdateOne) Mutation() *ParentMutation {
return _u.mutation
}
// Where appends a list predicates to the ParentUpdate builder.
func (_u *ParentUpdateOne) Where(ps ...predicate.Parent) *ParentUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *ParentUpdateOne) Select(field string, fields ...string) *ParentUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated Parent entity.
func (_u *ParentUpdateOne) Save(ctx context.Context) (*Parent, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *ParentUpdateOne) SaveX(ctx context.Context) *Parent {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *ParentUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *ParentUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *ParentUpdateOne) check() error {
if _u.mutation.ChildCleared() && len(_u.mutation.ChildIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Parent.child"`)
}
if _u.mutation.ParentCleared() && len(_u.mutation.ParentIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Parent.parent"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *ParentUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ParentUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *ParentUpdateOne) sqlSave(ctx context.Context) (_node *Parent, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(parent.Table, parent.Columns, sqlgraph.NewFieldSpec(parent.FieldID, field.TypeInt))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Parent.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, parent.FieldID)
for _, f := range fields {
if !parent.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != parent.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.Predicates(); len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.ByAdoption(); ok {
_spec.SetField(parent.FieldByAdoption, field.TypeBool, value)
}
_spec.Node.Schema = _u.schemaConfig.Parent
ctx = internal.NewSchemaConfigContext(ctx, _u.schemaConfig)
_spec.AddModifiers(_u.modifiers...)
_node = &Parent{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{parent.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}