// 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 (_u *PostUpdate) Where(ps ...predicate.Post) *PostUpdate { _u.mutation.Where(ps...) return _u } // SetText sets the "text" field. func (_u *PostUpdate) SetText(v string) *PostUpdate { _u.mutation.SetText(v) return _u } // SetNillableText sets the "text" field if the given value is not nil. func (_u *PostUpdate) SetNillableText(v *string) *PostUpdate { if v != nil { _u.SetText(*v) } return _u } // SetAuthorID sets the "author_id" field. func (_u *PostUpdate) SetAuthorID(v int) *PostUpdate { _u.mutation.SetAuthorID(v) return _u } // SetNillableAuthorID sets the "author_id" field if the given value is not nil. func (_u *PostUpdate) SetNillableAuthorID(v *int) *PostUpdate { if v != nil { _u.SetAuthorID(*v) } return _u } // ClearAuthorID clears the value of the "author_id" field. func (_u *PostUpdate) ClearAuthorID() *PostUpdate { _u.mutation.ClearAuthorID() return _u } // SetAuthor sets the "author" edge to the User entity. func (_u *PostUpdate) SetAuthor(v *User) *PostUpdate { return _u.SetAuthorID(v.ID) } // AddCommentIDs adds the "comments" edge to the Comment entity by IDs. func (_u *PostUpdate) AddCommentIDs(ids ...int) *PostUpdate { _u.mutation.AddCommentIDs(ids...) return _u } // AddComments adds the "comments" edges to the Comment entity. func (_u *PostUpdate) AddComments(v ...*Comment) *PostUpdate { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _u.AddCommentIDs(ids...) } // Mutation returns the PostMutation object of the builder. func (_u *PostUpdate) Mutation() *PostMutation { return _u.mutation } // ClearAuthor clears the "author" edge to the User entity. func (_u *PostUpdate) ClearAuthor() *PostUpdate { _u.mutation.ClearAuthor() return _u } // ClearComments clears all "comments" edges to the Comment entity. func (_u *PostUpdate) ClearComments() *PostUpdate { _u.mutation.ClearComments() return _u } // RemoveCommentIDs removes the "comments" edge to Comment entities by IDs. func (_u *PostUpdate) RemoveCommentIDs(ids ...int) *PostUpdate { _u.mutation.RemoveCommentIDs(ids...) return _u } // RemoveComments removes "comments" edges to Comment entities. func (_u *PostUpdate) RemoveComments(v ...*Comment) *PostUpdate { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _u.RemoveCommentIDs(ids...) } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *PostUpdate) 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 *PostUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *PostUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *PostUpdate) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *PostUpdate) sqlSave(ctx context.Context) (_node int, err error) { _spec := sqlgraph.NewUpdateSpec(post.Table, post.Columns, sqlgraph.NewFieldSpec(post.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.Text(); ok { _spec.SetField(post.FieldText, field.TypeString, value) } if _u.mutation.AuthorCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: post.AuthorTable, Columns: []string{post.AuthorColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.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.NewFieldSpec(user.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if _u.mutation.CommentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: post.CommentsTable, Columns: []string{post.CommentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(comment.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.RemovedCommentsIDs(); len(nodes) > 0 && !_u.mutation.CommentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: post.CommentsTable, Columns: []string{post.CommentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(comment.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.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.NewFieldSpec(comment.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if _node, err = sqlgraph.UpdateNodes(ctx, _u.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 } _u.mutation.done = true return _node, 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 (_u *PostUpdateOne) SetText(v string) *PostUpdateOne { _u.mutation.SetText(v) return _u } // SetNillableText sets the "text" field if the given value is not nil. func (_u *PostUpdateOne) SetNillableText(v *string) *PostUpdateOne { if v != nil { _u.SetText(*v) } return _u } // SetAuthorID sets the "author_id" field. func (_u *PostUpdateOne) SetAuthorID(v int) *PostUpdateOne { _u.mutation.SetAuthorID(v) return _u } // SetNillableAuthorID sets the "author_id" field if the given value is not nil. func (_u *PostUpdateOne) SetNillableAuthorID(v *int) *PostUpdateOne { if v != nil { _u.SetAuthorID(*v) } return _u } // ClearAuthorID clears the value of the "author_id" field. func (_u *PostUpdateOne) ClearAuthorID() *PostUpdateOne { _u.mutation.ClearAuthorID() return _u } // SetAuthor sets the "author" edge to the User entity. func (_u *PostUpdateOne) SetAuthor(v *User) *PostUpdateOne { return _u.SetAuthorID(v.ID) } // AddCommentIDs adds the "comments" edge to the Comment entity by IDs. func (_u *PostUpdateOne) AddCommentIDs(ids ...int) *PostUpdateOne { _u.mutation.AddCommentIDs(ids...) return _u } // AddComments adds the "comments" edges to the Comment entity. func (_u *PostUpdateOne) AddComments(v ...*Comment) *PostUpdateOne { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _u.AddCommentIDs(ids...) } // Mutation returns the PostMutation object of the builder. func (_u *PostUpdateOne) Mutation() *PostMutation { return _u.mutation } // ClearAuthor clears the "author" edge to the User entity. func (_u *PostUpdateOne) ClearAuthor() *PostUpdateOne { _u.mutation.ClearAuthor() return _u } // ClearComments clears all "comments" edges to the Comment entity. func (_u *PostUpdateOne) ClearComments() *PostUpdateOne { _u.mutation.ClearComments() return _u } // RemoveCommentIDs removes the "comments" edge to Comment entities by IDs. func (_u *PostUpdateOne) RemoveCommentIDs(ids ...int) *PostUpdateOne { _u.mutation.RemoveCommentIDs(ids...) return _u } // RemoveComments removes "comments" edges to Comment entities. func (_u *PostUpdateOne) RemoveComments(v ...*Comment) *PostUpdateOne { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _u.RemoveCommentIDs(ids...) } // Where appends a list predicates to the PostUpdate builder. func (_u *PostUpdateOne) Where(ps ...predicate.Post) *PostUpdateOne { _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 *PostUpdateOne) Select(field string, fields ...string) *PostUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Post entity. func (_u *PostUpdateOne) Save(ctx context.Context) (*Post, error) { return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *PostUpdateOne) SaveX(ctx context.Context) *Post { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *PostUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *PostUpdateOne) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *PostUpdateOne) sqlSave(ctx context.Context) (_node *Post, err error) { _spec := sqlgraph.NewUpdateSpec(post.Table, post.Columns, sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt)) id, ok := _u.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 := _u.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 := _u.mutation.Predicates(); len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := _u.mutation.Text(); ok { _spec.SetField(post.FieldText, field.TypeString, value) } if _u.mutation.AuthorCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: post.AuthorTable, Columns: []string{post.AuthorColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.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.NewFieldSpec(user.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if _u.mutation.CommentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: post.CommentsTable, Columns: []string{post.CommentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(comment.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.RemovedCommentsIDs(); len(nodes) > 0 && !_u.mutation.CommentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: post.CommentsTable, Columns: []string{post.CommentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(comment.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.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.NewFieldSpec(comment.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Post{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{post.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } _u.mutation.done = true return _node, nil }