mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
401 lines
10 KiB
Go
401 lines
10 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/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 (cu *CommentUpdate) Where(ps ...predicate.Comment) *CommentUpdate {
|
|
cu.mutation.Where(ps...)
|
|
return cu
|
|
}
|
|
|
|
// SetText sets the "text" field.
|
|
func (cu *CommentUpdate) SetText(s string) *CommentUpdate {
|
|
cu.mutation.SetText(s)
|
|
return cu
|
|
}
|
|
|
|
// SetPostID sets the "post_id" field.
|
|
func (cu *CommentUpdate) SetPostID(i int) *CommentUpdate {
|
|
cu.mutation.SetPostID(i)
|
|
return cu
|
|
}
|
|
|
|
// SetPost sets the "post" edge to the Post entity.
|
|
func (cu *CommentUpdate) SetPost(p *Post) *CommentUpdate {
|
|
return cu.SetPostID(p.ID)
|
|
}
|
|
|
|
// Mutation returns the CommentMutation object of the builder.
|
|
func (cu *CommentUpdate) Mutation() *CommentMutation {
|
|
return cu.mutation
|
|
}
|
|
|
|
// ClearPost clears the "post" edge to the Post entity.
|
|
func (cu *CommentUpdate) ClearPost() *CommentUpdate {
|
|
cu.mutation.ClearPost()
|
|
return cu
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (cu *CommentUpdate) Save(ctx context.Context) (int, error) {
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(cu.hooks) == 0 {
|
|
if err = cu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
affected, err = cu.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*CommentMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = cu.check(); err != nil {
|
|
return 0, err
|
|
}
|
|
cu.mutation = mutation
|
|
affected, err = cu.sqlSave(ctx)
|
|
mutation.done = true
|
|
return affected, err
|
|
})
|
|
for i := len(cu.hooks) - 1; i >= 0; i-- {
|
|
if cu.hooks[i] == nil {
|
|
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = cu.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, cu.mutation); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return affected, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (cu *CommentUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := cu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (cu *CommentUpdate) Exec(ctx context.Context) error {
|
|
_, err := cu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (cu *CommentUpdate) ExecX(ctx context.Context) {
|
|
if err := cu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (cu *CommentUpdate) check() error {
|
|
if _, ok := cu.mutation.PostID(); cu.mutation.PostCleared() && !ok {
|
|
return errors.New(`ent: clearing a required unique edge "Comment.post"`)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cu *CommentUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: comment.Table,
|
|
Columns: comment.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: comment.FieldID,
|
|
},
|
|
},
|
|
}
|
|
if ps := cu.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := cu.mutation.Text(); ok {
|
|
_spec.SetField(comment.FieldText, field.TypeString, value)
|
|
}
|
|
if cu.mutation.PostCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: comment.PostTable,
|
|
Columns: []string{comment.PostColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: post.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := cu.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.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: post.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, cu.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
|
|
}
|
|
return n, 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 (cuo *CommentUpdateOne) SetText(s string) *CommentUpdateOne {
|
|
cuo.mutation.SetText(s)
|
|
return cuo
|
|
}
|
|
|
|
// SetPostID sets the "post_id" field.
|
|
func (cuo *CommentUpdateOne) SetPostID(i int) *CommentUpdateOne {
|
|
cuo.mutation.SetPostID(i)
|
|
return cuo
|
|
}
|
|
|
|
// SetPost sets the "post" edge to the Post entity.
|
|
func (cuo *CommentUpdateOne) SetPost(p *Post) *CommentUpdateOne {
|
|
return cuo.SetPostID(p.ID)
|
|
}
|
|
|
|
// Mutation returns the CommentMutation object of the builder.
|
|
func (cuo *CommentUpdateOne) Mutation() *CommentMutation {
|
|
return cuo.mutation
|
|
}
|
|
|
|
// ClearPost clears the "post" edge to the Post entity.
|
|
func (cuo *CommentUpdateOne) ClearPost() *CommentUpdateOne {
|
|
cuo.mutation.ClearPost()
|
|
return cuo
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (cuo *CommentUpdateOne) Select(field string, fields ...string) *CommentUpdateOne {
|
|
cuo.fields = append([]string{field}, fields...)
|
|
return cuo
|
|
}
|
|
|
|
// Save executes the query and returns the updated Comment entity.
|
|
func (cuo *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) {
|
|
var (
|
|
err error
|
|
node *Comment
|
|
)
|
|
if len(cuo.hooks) == 0 {
|
|
if err = cuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = cuo.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*CommentMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err = cuo.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
cuo.mutation = mutation
|
|
node, err = cuo.sqlSave(ctx)
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(cuo.hooks) - 1; i >= 0; i-- {
|
|
if cuo.hooks[i] == nil {
|
|
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = cuo.hooks[i](mut)
|
|
}
|
|
v, err := mut.Mutate(ctx, cuo.mutation)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nv, ok := v.(*Comment)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v)
|
|
}
|
|
node = nv
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (cuo *CommentUpdateOne) SaveX(ctx context.Context) *Comment {
|
|
node, err := cuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (cuo *CommentUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := cuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (cuo *CommentUpdateOne) ExecX(ctx context.Context) {
|
|
if err := cuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (cuo *CommentUpdateOne) check() error {
|
|
if _, ok := cuo.mutation.PostID(); cuo.mutation.PostCleared() && !ok {
|
|
return errors.New(`ent: clearing a required unique edge "Comment.post"`)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cuo *CommentUpdateOne) sqlSave(ctx context.Context) (_node *Comment, err error) {
|
|
_spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: comment.Table,
|
|
Columns: comment.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: comment.FieldID,
|
|
},
|
|
},
|
|
}
|
|
id, ok := cuo.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 := cuo.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 := cuo.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := cuo.mutation.Text(); ok {
|
|
_spec.SetField(comment.FieldText, field.TypeString, value)
|
|
}
|
|
if cuo.mutation.PostCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: comment.PostTable,
|
|
Columns: []string{comment.PostColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: post.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := cuo.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.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: post.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
_node = &Comment{config: cuo.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
if err = sqlgraph.UpdateNode(ctx, cuo.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
|
|
}
|
|
return _node, nil
|
|
}
|