mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
397 lines
11 KiB
Go
397 lines
11 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 entv2
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/entc/integration/migrate/entv2/blog"
|
|
"entgo.io/ent/entc/integration/migrate/entv2/predicate"
|
|
"entgo.io/ent/entc/integration/migrate/entv2/user"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// BlogUpdate is the builder for updating Blog entities.
|
|
type BlogUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *BlogMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the BlogUpdate builder.
|
|
func (_u *BlogUpdate) Where(ps ...predicate.Blog) *BlogUpdate {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// SetOid sets the "oid" field.
|
|
func (_u *BlogUpdate) SetOid(v int) *BlogUpdate {
|
|
_u.mutation.ResetOid()
|
|
_u.mutation.SetOid(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableOid sets the "oid" field if the given value is not nil.
|
|
func (_u *BlogUpdate) SetNillableOid(v *int) *BlogUpdate {
|
|
if v != nil {
|
|
_u.SetOid(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddOid adds value to the "oid" field.
|
|
func (_u *BlogUpdate) AddOid(v int) *BlogUpdate {
|
|
_u.mutation.AddOid(v)
|
|
return _u
|
|
}
|
|
|
|
// AddAdminIDs adds the "admins" edge to the User entity by IDs.
|
|
func (_u *BlogUpdate) AddAdminIDs(ids ...int) *BlogUpdate {
|
|
_u.mutation.AddAdminIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// AddAdmins adds the "admins" edges to the User entity.
|
|
func (_u *BlogUpdate) AddAdmins(v ...*User) *BlogUpdate {
|
|
ids := make([]int, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.AddAdminIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the BlogMutation object of the builder.
|
|
func (_u *BlogUpdate) Mutation() *BlogMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearAdmins clears all "admins" edges to the User entity.
|
|
func (_u *BlogUpdate) ClearAdmins() *BlogUpdate {
|
|
_u.mutation.ClearAdmins()
|
|
return _u
|
|
}
|
|
|
|
// RemoveAdminIDs removes the "admins" edge to User entities by IDs.
|
|
func (_u *BlogUpdate) RemoveAdminIDs(ids ...int) *BlogUpdate {
|
|
_u.mutation.RemoveAdminIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// RemoveAdmins removes "admins" edges to User entities.
|
|
func (_u *BlogUpdate) RemoveAdmins(v ...*User) *BlogUpdate {
|
|
ids := make([]int, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.RemoveAdminIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (_u *BlogUpdate) 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 *BlogUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_u *BlogUpdate) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *BlogUpdate) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (_u *BlogUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
|
_spec := sqlgraph.NewUpdateSpec(blog.Table, blog.Columns, sqlgraph.NewFieldSpec(blog.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.Oid(); ok {
|
|
_spec.SetField(blog.FieldOid, field.TypeInt, value)
|
|
}
|
|
if value, ok := _u.mutation.AddedOid(); ok {
|
|
_spec.AddField(blog.FieldOid, field.TypeInt, value)
|
|
}
|
|
if _u.mutation.AdminsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.RemovedAdminsIDs(); len(nodes) > 0 && !_u.mutation.AdminsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
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.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.AdminsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
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 _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{blog.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
// BlogUpdateOne is the builder for updating a single Blog entity.
|
|
type BlogUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *BlogMutation
|
|
}
|
|
|
|
// SetOid sets the "oid" field.
|
|
func (_u *BlogUpdateOne) SetOid(v int) *BlogUpdateOne {
|
|
_u.mutation.ResetOid()
|
|
_u.mutation.SetOid(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableOid sets the "oid" field if the given value is not nil.
|
|
func (_u *BlogUpdateOne) SetNillableOid(v *int) *BlogUpdateOne {
|
|
if v != nil {
|
|
_u.SetOid(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// AddOid adds value to the "oid" field.
|
|
func (_u *BlogUpdateOne) AddOid(v int) *BlogUpdateOne {
|
|
_u.mutation.AddOid(v)
|
|
return _u
|
|
}
|
|
|
|
// AddAdminIDs adds the "admins" edge to the User entity by IDs.
|
|
func (_u *BlogUpdateOne) AddAdminIDs(ids ...int) *BlogUpdateOne {
|
|
_u.mutation.AddAdminIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// AddAdmins adds the "admins" edges to the User entity.
|
|
func (_u *BlogUpdateOne) AddAdmins(v ...*User) *BlogUpdateOne {
|
|
ids := make([]int, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.AddAdminIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the BlogMutation object of the builder.
|
|
func (_u *BlogUpdateOne) Mutation() *BlogMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// ClearAdmins clears all "admins" edges to the User entity.
|
|
func (_u *BlogUpdateOne) ClearAdmins() *BlogUpdateOne {
|
|
_u.mutation.ClearAdmins()
|
|
return _u
|
|
}
|
|
|
|
// RemoveAdminIDs removes the "admins" edge to User entities by IDs.
|
|
func (_u *BlogUpdateOne) RemoveAdminIDs(ids ...int) *BlogUpdateOne {
|
|
_u.mutation.RemoveAdminIDs(ids...)
|
|
return _u
|
|
}
|
|
|
|
// RemoveAdmins removes "admins" edges to User entities.
|
|
func (_u *BlogUpdateOne) RemoveAdmins(v ...*User) *BlogUpdateOne {
|
|
ids := make([]int, len(v))
|
|
for i := range v {
|
|
ids[i] = v[i].ID
|
|
}
|
|
return _u.RemoveAdminIDs(ids...)
|
|
}
|
|
|
|
// Where appends a list predicates to the BlogUpdate builder.
|
|
func (_u *BlogUpdateOne) Where(ps ...predicate.Blog) *BlogUpdateOne {
|
|
_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 *BlogUpdateOne) Select(field string, fields ...string) *BlogUpdateOne {
|
|
_u.fields = append([]string{field}, fields...)
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the updated Blog entity.
|
|
func (_u *BlogUpdateOne) Save(ctx context.Context) (*Blog, error) {
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *BlogUpdateOne) SaveX(ctx context.Context) *Blog {
|
|
node, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (_u *BlogUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *BlogUpdateOne) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (_u *BlogUpdateOne) sqlSave(ctx context.Context) (_node *Blog, err error) {
|
|
_spec := sqlgraph.NewUpdateSpec(blog.Table, blog.Columns, sqlgraph.NewFieldSpec(blog.FieldID, field.TypeInt))
|
|
id, ok := _u.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`entv2: missing "Blog.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, blog.FieldID)
|
|
for _, f := range fields {
|
|
if !blog.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("entv2: invalid field %q for query", f)}
|
|
}
|
|
if f != blog.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.Oid(); ok {
|
|
_spec.SetField(blog.FieldOid, field.TypeInt, value)
|
|
}
|
|
if value, ok := _u.mutation.AddedOid(); ok {
|
|
_spec.AddField(blog.FieldOid, field.TypeInt, value)
|
|
}
|
|
if _u.mutation.AdminsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.RemovedAdminsIDs(); len(nodes) > 0 && !_u.mutation.AdminsCleared() {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
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.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := _u.mutation.AdminsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: blog.AdminsTable,
|
|
Columns: []string{blog.AdminsColumn},
|
|
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)
|
|
}
|
|
_node = &Blog{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{blog.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|