mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
490 lines
12 KiB
Go
490 lines
12 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. 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 entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql"
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebookincubator/ent/entc/integration/template/ent/pet"
|
|
"github.com/facebookincubator/ent/entc/integration/template/ent/predicate"
|
|
"github.com/facebookincubator/ent/entc/integration/template/ent/user"
|
|
"github.com/facebookincubator/ent/schema/field"
|
|
)
|
|
|
|
// UserUpdate is the builder for updating User entities.
|
|
type UserUpdate struct {
|
|
config
|
|
name *string
|
|
pets map[int]struct{}
|
|
friends map[int]struct{}
|
|
removedPets map[int]struct{}
|
|
removedFriends map[int]struct{}
|
|
predicates []predicate.User
|
|
}
|
|
|
|
// Where adds a new predicate for the builder.
|
|
func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
|
|
uu.predicates = append(uu.predicates, ps...)
|
|
return uu
|
|
}
|
|
|
|
// SetName sets the name field.
|
|
func (uu *UserUpdate) SetName(s string) *UserUpdate {
|
|
uu.name = &s
|
|
return uu
|
|
}
|
|
|
|
// AddPetIDs adds the pets edge to Pet by ids.
|
|
func (uu *UserUpdate) AddPetIDs(ids ...int) *UserUpdate {
|
|
if uu.pets == nil {
|
|
uu.pets = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uu.pets[ids[i]] = struct{}{}
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// AddPets adds the pets edges to Pet.
|
|
func (uu *UserUpdate) AddPets(p ...*Pet) *UserUpdate {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return uu.AddPetIDs(ids...)
|
|
}
|
|
|
|
// AddFriendIDs adds the friends edge to User by ids.
|
|
func (uu *UserUpdate) AddFriendIDs(ids ...int) *UserUpdate {
|
|
if uu.friends == nil {
|
|
uu.friends = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uu.friends[ids[i]] = struct{}{}
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// AddFriends adds the friends edges to User.
|
|
func (uu *UserUpdate) AddFriends(u ...*User) *UserUpdate {
|
|
ids := make([]int, len(u))
|
|
for i := range u {
|
|
ids[i] = u[i].ID
|
|
}
|
|
return uu.AddFriendIDs(ids...)
|
|
}
|
|
|
|
// RemovePetIDs removes the pets edge to Pet by ids.
|
|
func (uu *UserUpdate) RemovePetIDs(ids ...int) *UserUpdate {
|
|
if uu.removedPets == nil {
|
|
uu.removedPets = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uu.removedPets[ids[i]] = struct{}{}
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// RemovePets removes pets edges to Pet.
|
|
func (uu *UserUpdate) RemovePets(p ...*Pet) *UserUpdate {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return uu.RemovePetIDs(ids...)
|
|
}
|
|
|
|
// RemoveFriendIDs removes the friends edge to User by ids.
|
|
func (uu *UserUpdate) RemoveFriendIDs(ids ...int) *UserUpdate {
|
|
if uu.removedFriends == nil {
|
|
uu.removedFriends = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uu.removedFriends[ids[i]] = struct{}{}
|
|
}
|
|
return uu
|
|
}
|
|
|
|
// RemoveFriends removes friends edges to User.
|
|
func (uu *UserUpdate) RemoveFriends(u ...*User) *UserUpdate {
|
|
ids := make([]int, len(u))
|
|
for i := range u {
|
|
ids[i] = u[i].ID
|
|
}
|
|
return uu.RemoveFriendIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
|
func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
|
return uu.sqlSave(ctx)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (uu *UserUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := uu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (uu *UserUpdate) Exec(ctx context.Context) error {
|
|
_, err := uu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (uu *UserUpdate) ExecX(ctx context.Context) {
|
|
if err := uu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: user.Table,
|
|
Columns: user.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
},
|
|
}
|
|
if ps := uu.predicates; len(ps) > 0 {
|
|
spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value := uu.name; value != nil {
|
|
spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: *value,
|
|
Column: user.FieldName,
|
|
})
|
|
}
|
|
if nodes := uu.removedPets; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: user.PetsTable,
|
|
Columns: []string{user.PetsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: pet.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for k, _ := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
spec.Edges.Clear = append(spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uu.pets; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: user.PetsTable,
|
|
Columns: []string{user.PetsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: pet.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for k, _ := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
spec.Edges.Add = append(spec.Edges.Add, edge)
|
|
}
|
|
if nodes := uu.removedFriends; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: false,
|
|
Table: user.FriendsTable,
|
|
Columns: user.FriendsPrimaryKey,
|
|
Bidi: true,
|
|
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.Clear = append(spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uu.friends; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: false,
|
|
Table: user.FriendsTable,
|
|
Columns: user.FriendsPrimaryKey,
|
|
Bidi: true,
|
|
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 n, err = sqlgraph.UpdateNodes(ctx, uu.driver, spec); err != nil {
|
|
if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return 0, err
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// UserUpdateOne is the builder for updating a single User entity.
|
|
type UserUpdateOne struct {
|
|
config
|
|
id int
|
|
name *string
|
|
pets map[int]struct{}
|
|
friends map[int]struct{}
|
|
removedPets map[int]struct{}
|
|
removedFriends map[int]struct{}
|
|
}
|
|
|
|
// SetName sets the name field.
|
|
func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
|
|
uuo.name = &s
|
|
return uuo
|
|
}
|
|
|
|
// AddPetIDs adds the pets edge to Pet by ids.
|
|
func (uuo *UserUpdateOne) AddPetIDs(ids ...int) *UserUpdateOne {
|
|
if uuo.pets == nil {
|
|
uuo.pets = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uuo.pets[ids[i]] = struct{}{}
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// AddPets adds the pets edges to Pet.
|
|
func (uuo *UserUpdateOne) AddPets(p ...*Pet) *UserUpdateOne {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return uuo.AddPetIDs(ids...)
|
|
}
|
|
|
|
// AddFriendIDs adds the friends edge to User by ids.
|
|
func (uuo *UserUpdateOne) AddFriendIDs(ids ...int) *UserUpdateOne {
|
|
if uuo.friends == nil {
|
|
uuo.friends = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uuo.friends[ids[i]] = struct{}{}
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// AddFriends adds the friends edges to User.
|
|
func (uuo *UserUpdateOne) AddFriends(u ...*User) *UserUpdateOne {
|
|
ids := make([]int, len(u))
|
|
for i := range u {
|
|
ids[i] = u[i].ID
|
|
}
|
|
return uuo.AddFriendIDs(ids...)
|
|
}
|
|
|
|
// RemovePetIDs removes the pets edge to Pet by ids.
|
|
func (uuo *UserUpdateOne) RemovePetIDs(ids ...int) *UserUpdateOne {
|
|
if uuo.removedPets == nil {
|
|
uuo.removedPets = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uuo.removedPets[ids[i]] = struct{}{}
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// RemovePets removes pets edges to Pet.
|
|
func (uuo *UserUpdateOne) RemovePets(p ...*Pet) *UserUpdateOne {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return uuo.RemovePetIDs(ids...)
|
|
}
|
|
|
|
// RemoveFriendIDs removes the friends edge to User by ids.
|
|
func (uuo *UserUpdateOne) RemoveFriendIDs(ids ...int) *UserUpdateOne {
|
|
if uuo.removedFriends == nil {
|
|
uuo.removedFriends = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uuo.removedFriends[ids[i]] = struct{}{}
|
|
}
|
|
return uuo
|
|
}
|
|
|
|
// RemoveFriends removes friends edges to User.
|
|
func (uuo *UserUpdateOne) RemoveFriends(u ...*User) *UserUpdateOne {
|
|
ids := make([]int, len(u))
|
|
for i := range u {
|
|
ids[i] = u[i].ID
|
|
}
|
|
return uuo.RemoveFriendIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the updated entity.
|
|
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
|
return uuo.sqlSave(ctx)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (uuo *UserUpdateOne) SaveX(ctx context.Context) *User {
|
|
u, err := uuo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return u
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (uuo *UserUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := uuo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (uuo *UserUpdateOne) ExecX(ctx context.Context) {
|
|
if err := uuo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
|
spec := &sqlgraph.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: user.Table,
|
|
Columns: user.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Value: uuo.id,
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
},
|
|
}
|
|
if value := uuo.name; value != nil {
|
|
spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: *value,
|
|
Column: user.FieldName,
|
|
})
|
|
}
|
|
if nodes := uuo.removedPets; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: user.PetsTable,
|
|
Columns: []string{user.PetsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: pet.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for k, _ := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
spec.Edges.Clear = append(spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uuo.pets; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: user.PetsTable,
|
|
Columns: []string{user.PetsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: pet.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for k, _ := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
spec.Edges.Add = append(spec.Edges.Add, edge)
|
|
}
|
|
if nodes := uuo.removedFriends; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: false,
|
|
Table: user.FriendsTable,
|
|
Columns: user.FriendsPrimaryKey,
|
|
Bidi: true,
|
|
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.Clear = append(spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := uuo.friends; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: false,
|
|
Table: user.FriendsTable,
|
|
Columns: user.FriendsPrimaryKey,
|
|
Bidi: true,
|
|
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)
|
|
}
|
|
u = &User{config: uuo.config}
|
|
spec.Assign = u.assignValues
|
|
spec.ScanValues = u.scanValues()
|
|
if err = sqlgraph.UpdateNode(ctx, uuo.driver, spec); err != nil {
|
|
if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return nil, err
|
|
}
|
|
return u, nil
|
|
}
|