// 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/schema/field" ) // CommentUpdate is the builder for updating Comment entities. type CommentUpdate struct { config hooks []Hook mutation *CommentMutation } // Where appends a list predicates to the CommentUpdate builder. func (_u *CommentUpdate) Where(ps ...predicate.Comment) *CommentUpdate { _u.mutation.Where(ps...) return _u } // SetText sets the "text" field. func (_u *CommentUpdate) SetText(v string) *CommentUpdate { _u.mutation.SetText(v) return _u } // SetNillableText sets the "text" field if the given value is not nil. func (_u *CommentUpdate) SetNillableText(v *string) *CommentUpdate { if v != nil { _u.SetText(*v) } return _u } // SetPostID sets the "post_id" field. func (_u *CommentUpdate) SetPostID(v int) *CommentUpdate { _u.mutation.SetPostID(v) return _u } // SetNillablePostID sets the "post_id" field if the given value is not nil. func (_u *CommentUpdate) SetNillablePostID(v *int) *CommentUpdate { if v != nil { _u.SetPostID(*v) } return _u } // SetPost sets the "post" edge to the Post entity. func (_u *CommentUpdate) SetPost(v *Post) *CommentUpdate { return _u.SetPostID(v.ID) } // Mutation returns the CommentMutation object of the builder. func (_u *CommentUpdate) Mutation() *CommentMutation { return _u.mutation } // ClearPost clears the "post" edge to the Post entity. func (_u *CommentUpdate) ClearPost() *CommentUpdate { _u.mutation.ClearPost() return _u } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *CommentUpdate) 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 *CommentUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *CommentUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *CommentUpdate) 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 *CommentUpdate) check() error { if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Comment.post"`) } return nil } func (_u *CommentUpdate) sqlSave(ctx context.Context) (_node int, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(comment.Table, comment.Columns, sqlgraph.NewFieldSpec(comment.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(comment.FieldText, field.TypeString, value) } if _u.mutation.PostCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: comment.PostTable, Columns: []string{comment.PostColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.PostIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: comment.PostTable, Columns: []string{comment.PostColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(post.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{comment.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } _u.mutation.done = true return _node, nil } // CommentUpdateOne is the builder for updating a single Comment entity. type CommentUpdateOne struct { config fields []string hooks []Hook mutation *CommentMutation } // SetText sets the "text" field. func (_u *CommentUpdateOne) SetText(v string) *CommentUpdateOne { _u.mutation.SetText(v) return _u } // SetNillableText sets the "text" field if the given value is not nil. func (_u *CommentUpdateOne) SetNillableText(v *string) *CommentUpdateOne { if v != nil { _u.SetText(*v) } return _u } // SetPostID sets the "post_id" field. func (_u *CommentUpdateOne) SetPostID(v int) *CommentUpdateOne { _u.mutation.SetPostID(v) return _u } // SetNillablePostID sets the "post_id" field if the given value is not nil. func (_u *CommentUpdateOne) SetNillablePostID(v *int) *CommentUpdateOne { if v != nil { _u.SetPostID(*v) } return _u } // SetPost sets the "post" edge to the Post entity. func (_u *CommentUpdateOne) SetPost(v *Post) *CommentUpdateOne { return _u.SetPostID(v.ID) } // Mutation returns the CommentMutation object of the builder. func (_u *CommentUpdateOne) Mutation() *CommentMutation { return _u.mutation } // ClearPost clears the "post" edge to the Post entity. func (_u *CommentUpdateOne) ClearPost() *CommentUpdateOne { _u.mutation.ClearPost() return _u } // Where appends a list predicates to the CommentUpdate builder. func (_u *CommentUpdateOne) Where(ps ...predicate.Comment) *CommentUpdateOne { _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 *CommentUpdateOne) Select(field string, fields ...string) *CommentUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Comment entity. func (_u *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) { return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *CommentUpdateOne) SaveX(ctx context.Context) *Comment { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *CommentUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *CommentUpdateOne) 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 *CommentUpdateOne) check() error { if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Comment.post"`) } return nil } func (_u *CommentUpdateOne) sqlSave(ctx context.Context) (_node *Comment, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(comment.Table, comment.Columns, sqlgraph.NewFieldSpec(comment.FieldID, field.TypeInt)) id, ok := _u.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Comment.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, comment.FieldID) for _, f := range fields { if !comment.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != comment.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(comment.FieldText, field.TypeString, value) } if _u.mutation.PostCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: comment.PostTable, Columns: []string{comment.PostColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.PostIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: comment.PostTable, Columns: []string{comment.PostColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Comment{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{comment.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } _u.mutation.done = true return _node, nil }