// 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/edgeschema/ent/predicate" "entgo.io/ent/entc/integration/edgeschema/ent/relationship" "entgo.io/ent/entc/integration/edgeschema/ent/relationshipinfo" "entgo.io/ent/entc/integration/edgeschema/ent/user" "entgo.io/ent/schema/field" ) // RelationshipUpdate is the builder for updating Relationship entities. type RelationshipUpdate struct { config hooks []Hook mutation *RelationshipMutation } // Where appends a list predicates to the RelationshipUpdate builder. func (_u *RelationshipUpdate) Where(ps ...predicate.Relationship) *RelationshipUpdate { _u.mutation.Where(ps...) return _u } // SetWeight sets the "weight" field. func (_u *RelationshipUpdate) SetWeight(v int) *RelationshipUpdate { _u.mutation.ResetWeight() _u.mutation.SetWeight(v) return _u } // SetNillableWeight sets the "weight" field if the given value is not nil. func (_u *RelationshipUpdate) SetNillableWeight(v *int) *RelationshipUpdate { if v != nil { _u.SetWeight(*v) } return _u } // AddWeight adds value to the "weight" field. func (_u *RelationshipUpdate) AddWeight(v int) *RelationshipUpdate { _u.mutation.AddWeight(v) return _u } // SetUserID sets the "user_id" field. func (_u *RelationshipUpdate) SetUserID(v int) *RelationshipUpdate { _u.mutation.SetUserID(v) return _u } // SetNillableUserID sets the "user_id" field if the given value is not nil. func (_u *RelationshipUpdate) SetNillableUserID(v *int) *RelationshipUpdate { if v != nil { _u.SetUserID(*v) } return _u } // SetRelativeID sets the "relative_id" field. func (_u *RelationshipUpdate) SetRelativeID(v int) *RelationshipUpdate { _u.mutation.SetRelativeID(v) return _u } // SetNillableRelativeID sets the "relative_id" field if the given value is not nil. func (_u *RelationshipUpdate) SetNillableRelativeID(v *int) *RelationshipUpdate { if v != nil { _u.SetRelativeID(*v) } return _u } // SetInfoID sets the "info_id" field. func (_u *RelationshipUpdate) SetInfoID(v int) *RelationshipUpdate { _u.mutation.SetInfoID(v) return _u } // SetNillableInfoID sets the "info_id" field if the given value is not nil. func (_u *RelationshipUpdate) SetNillableInfoID(v *int) *RelationshipUpdate { if v != nil { _u.SetInfoID(*v) } return _u } // ClearInfoID clears the value of the "info_id" field. func (_u *RelationshipUpdate) ClearInfoID() *RelationshipUpdate { _u.mutation.ClearInfoID() return _u } // SetUser sets the "user" edge to the User entity. func (_u *RelationshipUpdate) SetUser(v *User) *RelationshipUpdate { return _u.SetUserID(v.ID) } // SetRelative sets the "relative" edge to the User entity. func (_u *RelationshipUpdate) SetRelative(v *User) *RelationshipUpdate { return _u.SetRelativeID(v.ID) } // SetInfo sets the "info" edge to the RelationshipInfo entity. func (_u *RelationshipUpdate) SetInfo(v *RelationshipInfo) *RelationshipUpdate { return _u.SetInfoID(v.ID) } // Mutation returns the RelationshipMutation object of the builder. func (_u *RelationshipUpdate) Mutation() *RelationshipMutation { return _u.mutation } // ClearUser clears the "user" edge to the User entity. func (_u *RelationshipUpdate) ClearUser() *RelationshipUpdate { _u.mutation.ClearUser() return _u } // ClearRelative clears the "relative" edge to the User entity. func (_u *RelationshipUpdate) ClearRelative() *RelationshipUpdate { _u.mutation.ClearRelative() return _u } // ClearInfo clears the "info" edge to the RelationshipInfo entity. func (_u *RelationshipUpdate) ClearInfo() *RelationshipUpdate { _u.mutation.ClearInfo() return _u } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *RelationshipUpdate) 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 *RelationshipUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *RelationshipUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *RelationshipUpdate) 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 *RelationshipUpdate) check() error { if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Relationship.user"`) } if _u.mutation.RelativeCleared() && len(_u.mutation.RelativeIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Relationship.relative"`) } return nil } func (_u *RelationshipUpdate) sqlSave(ctx context.Context) (_node int, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(relationship.Table, relationship.Columns, sqlgraph.NewFieldSpec(relationship.FieldUserID, field.TypeInt), sqlgraph.NewFieldSpec(relationship.FieldRelativeID, 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.Weight(); ok { _spec.SetField(relationship.FieldWeight, field.TypeInt, value) } if value, ok := _u.mutation.AddedWeight(); ok { _spec.AddField(relationship.FieldWeight, field.TypeInt, value) } if _u.mutation.UserCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.UserTable, Columns: []string{relationship.UserColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.UserIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.UserTable, Columns: []string{relationship.UserColumn}, 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.RelativeCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.RelativeTable, Columns: []string{relationship.RelativeColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.RelativeIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.RelativeTable, Columns: []string{relationship.RelativeColumn}, 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.InfoCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.InfoTable, Columns: []string{relationship.InfoColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(relationshipinfo.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.InfoIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.InfoTable, Columns: []string{relationship.InfoColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(relationshipinfo.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{relationship.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } _u.mutation.done = true return _node, nil } // RelationshipUpdateOne is the builder for updating a single Relationship entity. type RelationshipUpdateOne struct { config fields []string hooks []Hook mutation *RelationshipMutation } // SetWeight sets the "weight" field. func (_u *RelationshipUpdateOne) SetWeight(v int) *RelationshipUpdateOne { _u.mutation.ResetWeight() _u.mutation.SetWeight(v) return _u } // SetNillableWeight sets the "weight" field if the given value is not nil. func (_u *RelationshipUpdateOne) SetNillableWeight(v *int) *RelationshipUpdateOne { if v != nil { _u.SetWeight(*v) } return _u } // AddWeight adds value to the "weight" field. func (_u *RelationshipUpdateOne) AddWeight(v int) *RelationshipUpdateOne { _u.mutation.AddWeight(v) return _u } // SetUserID sets the "user_id" field. func (_u *RelationshipUpdateOne) SetUserID(v int) *RelationshipUpdateOne { _u.mutation.SetUserID(v) return _u } // SetNillableUserID sets the "user_id" field if the given value is not nil. func (_u *RelationshipUpdateOne) SetNillableUserID(v *int) *RelationshipUpdateOne { if v != nil { _u.SetUserID(*v) } return _u } // SetRelativeID sets the "relative_id" field. func (_u *RelationshipUpdateOne) SetRelativeID(v int) *RelationshipUpdateOne { _u.mutation.SetRelativeID(v) return _u } // SetNillableRelativeID sets the "relative_id" field if the given value is not nil. func (_u *RelationshipUpdateOne) SetNillableRelativeID(v *int) *RelationshipUpdateOne { if v != nil { _u.SetRelativeID(*v) } return _u } // SetInfoID sets the "info_id" field. func (_u *RelationshipUpdateOne) SetInfoID(v int) *RelationshipUpdateOne { _u.mutation.SetInfoID(v) return _u } // SetNillableInfoID sets the "info_id" field if the given value is not nil. func (_u *RelationshipUpdateOne) SetNillableInfoID(v *int) *RelationshipUpdateOne { if v != nil { _u.SetInfoID(*v) } return _u } // ClearInfoID clears the value of the "info_id" field. func (_u *RelationshipUpdateOne) ClearInfoID() *RelationshipUpdateOne { _u.mutation.ClearInfoID() return _u } // SetUser sets the "user" edge to the User entity. func (_u *RelationshipUpdateOne) SetUser(v *User) *RelationshipUpdateOne { return _u.SetUserID(v.ID) } // SetRelative sets the "relative" edge to the User entity. func (_u *RelationshipUpdateOne) SetRelative(v *User) *RelationshipUpdateOne { return _u.SetRelativeID(v.ID) } // SetInfo sets the "info" edge to the RelationshipInfo entity. func (_u *RelationshipUpdateOne) SetInfo(v *RelationshipInfo) *RelationshipUpdateOne { return _u.SetInfoID(v.ID) } // Mutation returns the RelationshipMutation object of the builder. func (_u *RelationshipUpdateOne) Mutation() *RelationshipMutation { return _u.mutation } // ClearUser clears the "user" edge to the User entity. func (_u *RelationshipUpdateOne) ClearUser() *RelationshipUpdateOne { _u.mutation.ClearUser() return _u } // ClearRelative clears the "relative" edge to the User entity. func (_u *RelationshipUpdateOne) ClearRelative() *RelationshipUpdateOne { _u.mutation.ClearRelative() return _u } // ClearInfo clears the "info" edge to the RelationshipInfo entity. func (_u *RelationshipUpdateOne) ClearInfo() *RelationshipUpdateOne { _u.mutation.ClearInfo() return _u } // Where appends a list predicates to the RelationshipUpdate builder. func (_u *RelationshipUpdateOne) Where(ps ...predicate.Relationship) *RelationshipUpdateOne { _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 *RelationshipUpdateOne) Select(field string, fields ...string) *RelationshipUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Relationship entity. func (_u *RelationshipUpdateOne) Save(ctx context.Context) (*Relationship, error) { return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *RelationshipUpdateOne) SaveX(ctx context.Context) *Relationship { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *RelationshipUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *RelationshipUpdateOne) 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 *RelationshipUpdateOne) check() error { if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Relationship.user"`) } if _u.mutation.RelativeCleared() && len(_u.mutation.RelativeIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Relationship.relative"`) } return nil } func (_u *RelationshipUpdateOne) sqlSave(ctx context.Context) (_node *Relationship, err error) { if err := _u.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(relationship.Table, relationship.Columns, sqlgraph.NewFieldSpec(relationship.FieldUserID, field.TypeInt), sqlgraph.NewFieldSpec(relationship.FieldRelativeID, field.TypeInt)) if id, ok := _u.mutation.UserID(); !ok { return nil, &ValidationError{Name: "user_id", err: errors.New(`ent: missing "Relationship.user_id" for update`)} } else { _spec.Node.CompositeID[0].Value = id } if id, ok := _u.mutation.RelativeID(); !ok { return nil, &ValidationError{Name: "relative_id", err: errors.New(`ent: missing "Relationship.relative_id" for update`)} } else { _spec.Node.CompositeID[1].Value = id } if fields := _u.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, len(fields)) for i, f := range fields { if !relationship.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } _spec.Node.Columns[i] = 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.Weight(); ok { _spec.SetField(relationship.FieldWeight, field.TypeInt, value) } if value, ok := _u.mutation.AddedWeight(); ok { _spec.AddField(relationship.FieldWeight, field.TypeInt, value) } if _u.mutation.UserCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.UserTable, Columns: []string{relationship.UserColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.UserIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.UserTable, Columns: []string{relationship.UserColumn}, 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.RelativeCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.RelativeTable, Columns: []string{relationship.RelativeColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.RelativeIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.RelativeTable, Columns: []string{relationship.RelativeColumn}, 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.InfoCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.InfoTable, Columns: []string{relationship.InfoColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(relationshipinfo.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := _u.mutation.InfoIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: relationship.InfoTable, Columns: []string{relationship.InfoColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(relationshipinfo.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Relationship{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{relationship.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } _u.mutation.done = true return _node, nil }