Files
ent/entc/integration/cascadelete/ent/post_update.go

598 lines
16 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/cascadelete/ent/comment"
"entgo.io/ent/entc/integration/cascadelete/ent/post"
"entgo.io/ent/entc/integration/cascadelete/ent/predicate"
"entgo.io/ent/entc/integration/cascadelete/ent/user"
"entgo.io/ent/schema/field"
)
// PostUpdate is the builder for updating Post entities.
type PostUpdate struct {
config
hooks []Hook
mutation *PostMutation
}
// Where appends a list predicates to the PostUpdate builder.
func (pu *PostUpdate) Where(ps ...predicate.Post) *PostUpdate {
pu.mutation.Where(ps...)
return pu
}
// SetText sets the "text" field.
func (pu *PostUpdate) SetText(s string) *PostUpdate {
pu.mutation.SetText(s)
return pu
}
// SetNillableText sets the "text" field if the given value is not nil.
func (pu *PostUpdate) SetNillableText(s *string) *PostUpdate {
if s != nil {
pu.SetText(*s)
}
return pu
}
// SetAuthorID sets the "author_id" field.
func (pu *PostUpdate) SetAuthorID(i int) *PostUpdate {
pu.mutation.SetAuthorID(i)
return pu
}
// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (pu *PostUpdate) SetNillableAuthorID(i *int) *PostUpdate {
if i != nil {
pu.SetAuthorID(*i)
}
return pu
}
// ClearAuthorID clears the value of the "author_id" field.
func (pu *PostUpdate) ClearAuthorID() *PostUpdate {
pu.mutation.ClearAuthorID()
return pu
}
// SetAuthor sets the "author" edge to the User entity.
func (pu *PostUpdate) SetAuthor(u *User) *PostUpdate {
return pu.SetAuthorID(u.ID)
}
// AddCommentIDs adds the "comments" edge to the Comment entity by IDs.
func (pu *PostUpdate) AddCommentIDs(ids ...int) *PostUpdate {
pu.mutation.AddCommentIDs(ids...)
return pu
}
// AddComments adds the "comments" edges to the Comment entity.
func (pu *PostUpdate) AddComments(c ...*Comment) *PostUpdate {
ids := make([]int, len(c))
for i := range c {
ids[i] = c[i].ID
}
return pu.AddCommentIDs(ids...)
}
// Mutation returns the PostMutation object of the builder.
func (pu *PostUpdate) Mutation() *PostMutation {
return pu.mutation
}
// ClearAuthor clears the "author" edge to the User entity.
func (pu *PostUpdate) ClearAuthor() *PostUpdate {
pu.mutation.ClearAuthor()
return pu
}
// ClearComments clears all "comments" edges to the Comment entity.
func (pu *PostUpdate) ClearComments() *PostUpdate {
pu.mutation.ClearComments()
return pu
}
// RemoveCommentIDs removes the "comments" edge to Comment entities by IDs.
func (pu *PostUpdate) RemoveCommentIDs(ids ...int) *PostUpdate {
pu.mutation.RemoveCommentIDs(ids...)
return pu
}
// RemoveComments removes "comments" edges to Comment entities.
func (pu *PostUpdate) RemoveComments(c ...*Comment) *PostUpdate {
ids := make([]int, len(c))
for i := range c {
ids[i] = c[i].ID
}
return pu.RemoveCommentIDs(ids...)
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (pu *PostUpdate) Save(ctx context.Context) (int, error) {
var (
err error
affected int
)
if len(pu.hooks) == 0 {
affected, err = pu.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PostMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
pu.mutation = mutation
affected, err = pu.sqlSave(ctx)
mutation.done = true
return affected, err
})
for i := len(pu.hooks) - 1; i >= 0; i-- {
if pu.hooks[i] == nil {
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = pu.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, pu.mutation); err != nil {
return 0, err
}
}
return affected, err
}
// SaveX is like Save, but panics if an error occurs.
func (pu *PostUpdate) SaveX(ctx context.Context) int {
affected, err := pu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (pu *PostUpdate) Exec(ctx context.Context) error {
_, err := pu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pu *PostUpdate) ExecX(ctx context.Context) {
if err := pu.Exec(ctx); err != nil {
panic(err)
}
}
func (pu *PostUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: post.Table,
Columns: post.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: post.FieldID,
},
},
}
if ps := pu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := pu.mutation.Text(); ok {
_spec.SetField(post.FieldText, field.TypeString, value)
}
if pu.mutation.AuthorCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.AuthorTable,
Columns: []string{post.AuthorColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.AuthorIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.AuthorTable,
Columns: []string{post.AuthorColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if pu.mutation.CommentsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.RemovedCommentsIDs(); len(nodes) > 0 && !pu.mutation.CommentsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.CommentsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{post.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
return n, nil
}
// PostUpdateOne is the builder for updating a single Post entity.
type PostUpdateOne struct {
config
fields []string
hooks []Hook
mutation *PostMutation
}
// SetText sets the "text" field.
func (puo *PostUpdateOne) SetText(s string) *PostUpdateOne {
puo.mutation.SetText(s)
return puo
}
// SetNillableText sets the "text" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableText(s *string) *PostUpdateOne {
if s != nil {
puo.SetText(*s)
}
return puo
}
// SetAuthorID sets the "author_id" field.
func (puo *PostUpdateOne) SetAuthorID(i int) *PostUpdateOne {
puo.mutation.SetAuthorID(i)
return puo
}
// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableAuthorID(i *int) *PostUpdateOne {
if i != nil {
puo.SetAuthorID(*i)
}
return puo
}
// ClearAuthorID clears the value of the "author_id" field.
func (puo *PostUpdateOne) ClearAuthorID() *PostUpdateOne {
puo.mutation.ClearAuthorID()
return puo
}
// SetAuthor sets the "author" edge to the User entity.
func (puo *PostUpdateOne) SetAuthor(u *User) *PostUpdateOne {
return puo.SetAuthorID(u.ID)
}
// AddCommentIDs adds the "comments" edge to the Comment entity by IDs.
func (puo *PostUpdateOne) AddCommentIDs(ids ...int) *PostUpdateOne {
puo.mutation.AddCommentIDs(ids...)
return puo
}
// AddComments adds the "comments" edges to the Comment entity.
func (puo *PostUpdateOne) AddComments(c ...*Comment) *PostUpdateOne {
ids := make([]int, len(c))
for i := range c {
ids[i] = c[i].ID
}
return puo.AddCommentIDs(ids...)
}
// Mutation returns the PostMutation object of the builder.
func (puo *PostUpdateOne) Mutation() *PostMutation {
return puo.mutation
}
// ClearAuthor clears the "author" edge to the User entity.
func (puo *PostUpdateOne) ClearAuthor() *PostUpdateOne {
puo.mutation.ClearAuthor()
return puo
}
// ClearComments clears all "comments" edges to the Comment entity.
func (puo *PostUpdateOne) ClearComments() *PostUpdateOne {
puo.mutation.ClearComments()
return puo
}
// RemoveCommentIDs removes the "comments" edge to Comment entities by IDs.
func (puo *PostUpdateOne) RemoveCommentIDs(ids ...int) *PostUpdateOne {
puo.mutation.RemoveCommentIDs(ids...)
return puo
}
// RemoveComments removes "comments" edges to Comment entities.
func (puo *PostUpdateOne) RemoveComments(c ...*Comment) *PostUpdateOne {
ids := make([]int, len(c))
for i := range c {
ids[i] = c[i].ID
}
return puo.RemoveCommentIDs(ids...)
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (puo *PostUpdateOne) Select(field string, fields ...string) *PostUpdateOne {
puo.fields = append([]string{field}, fields...)
return puo
}
// Save executes the query and returns the updated Post entity.
func (puo *PostUpdateOne) Save(ctx context.Context) (*Post, error) {
var (
err error
node *Post
)
if len(puo.hooks) == 0 {
node, err = puo.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PostMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
puo.mutation = mutation
node, err = puo.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(puo.hooks) - 1; i >= 0; i-- {
if puo.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = puo.hooks[i](mut)
}
v, err := mut.Mutate(ctx, puo.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*Post)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from PostMutation", v)
}
node = nv
}
return node, err
}
// SaveX is like Save, but panics if an error occurs.
func (puo *PostUpdateOne) SaveX(ctx context.Context) *Post {
node, err := puo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (puo *PostUpdateOne) Exec(ctx context.Context) error {
_, err := puo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (puo *PostUpdateOne) ExecX(ctx context.Context) {
if err := puo.Exec(ctx); err != nil {
panic(err)
}
}
func (puo *PostUpdateOne) sqlSave(ctx context.Context) (_node *Post, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: post.Table,
Columns: post.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: post.FieldID,
},
},
}
id, ok := puo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Post.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := puo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, post.FieldID)
for _, f := range fields {
if !post.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != post.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := puo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := puo.mutation.Text(); ok {
_spec.SetField(post.FieldText, field.TypeString, value)
}
if puo.mutation.AuthorCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.AuthorTable,
Columns: []string{post.AuthorColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.AuthorIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.AuthorTable,
Columns: []string{post.AuthorColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if puo.mutation.CommentsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.RemovedCommentsIDs(); len(nodes) > 0 && !puo.mutation.CommentsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.CommentsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: post.CommentsTable,
Columns: []string{post.CommentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: comment.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &Post{config: puo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{post.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
return _node, nil
}