// 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" "time" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" "entgo.io/ent/dialect/gremlin/graph/dsl/p" "entgo.io/ent/entc/integration/gremlin/ent/pet" "entgo.io/ent/entc/integration/gremlin/ent/predicate" "entgo.io/ent/entc/integration/gremlin/ent/user" "github.com/google/uuid" ) // PetUpdate is the builder for updating Pet entities. type PetUpdate struct { config hooks []Hook mutation *PetMutation } // Where appends a list predicates to the PetUpdate builder. func (_u *PetUpdate) Where(ps ...predicate.Pet) *PetUpdate { _u.mutation.Where(ps...) return _u } // SetAge sets the "age" field. func (_u *PetUpdate) SetAge(v float64) *PetUpdate { _u.mutation.ResetAge() _u.mutation.SetAge(v) return _u } // SetNillableAge sets the "age" field if the given value is not nil. func (_u *PetUpdate) SetNillableAge(v *float64) *PetUpdate { if v != nil { _u.SetAge(*v) } return _u } // AddAge adds value to the "age" field. func (_u *PetUpdate) AddAge(v float64) *PetUpdate { _u.mutation.AddAge(v) return _u } // SetName sets the "name" field. func (_u *PetUpdate) SetName(v string) *PetUpdate { _u.mutation.SetName(v) return _u } // SetNillableName sets the "name" field if the given value is not nil. func (_u *PetUpdate) SetNillableName(v *string) *PetUpdate { if v != nil { _u.SetName(*v) } return _u } // SetUUID sets the "uuid" field. func (_u *PetUpdate) SetUUID(v uuid.UUID) *PetUpdate { _u.mutation.SetUUID(v) return _u } // SetNillableUUID sets the "uuid" field if the given value is not nil. func (_u *PetUpdate) SetNillableUUID(v *uuid.UUID) *PetUpdate { if v != nil { _u.SetUUID(*v) } return _u } // ClearUUID clears the value of the "uuid" field. func (_u *PetUpdate) ClearUUID() *PetUpdate { _u.mutation.ClearUUID() return _u } // SetNickname sets the "nickname" field. func (_u *PetUpdate) SetNickname(v string) *PetUpdate { _u.mutation.SetNickname(v) return _u } // SetNillableNickname sets the "nickname" field if the given value is not nil. func (_u *PetUpdate) SetNillableNickname(v *string) *PetUpdate { if v != nil { _u.SetNickname(*v) } return _u } // ClearNickname clears the value of the "nickname" field. func (_u *PetUpdate) ClearNickname() *PetUpdate { _u.mutation.ClearNickname() return _u } // SetTrained sets the "trained" field. func (_u *PetUpdate) SetTrained(v bool) *PetUpdate { _u.mutation.SetTrained(v) return _u } // SetNillableTrained sets the "trained" field if the given value is not nil. func (_u *PetUpdate) SetNillableTrained(v *bool) *PetUpdate { if v != nil { _u.SetTrained(*v) } return _u } // SetOptionalTime sets the "optional_time" field. func (_u *PetUpdate) SetOptionalTime(v time.Time) *PetUpdate { _u.mutation.SetOptionalTime(v) return _u } // SetNillableOptionalTime sets the "optional_time" field if the given value is not nil. func (_u *PetUpdate) SetNillableOptionalTime(v *time.Time) *PetUpdate { if v != nil { _u.SetOptionalTime(*v) } return _u } // ClearOptionalTime clears the value of the "optional_time" field. func (_u *PetUpdate) ClearOptionalTime() *PetUpdate { _u.mutation.ClearOptionalTime() return _u } // SetTeamID sets the "team" edge to the User entity by ID. func (_u *PetUpdate) SetTeamID(id string) *PetUpdate { _u.mutation.SetTeamID(id) return _u } // SetNillableTeamID sets the "team" edge to the User entity by ID if the given value is not nil. func (_u *PetUpdate) SetNillableTeamID(id *string) *PetUpdate { if id != nil { _u = _u.SetTeamID(*id) } return _u } // SetTeam sets the "team" edge to the User entity. func (_u *PetUpdate) SetTeam(v *User) *PetUpdate { return _u.SetTeamID(v.ID) } // SetOwnerID sets the "owner" edge to the User entity by ID. func (_u *PetUpdate) SetOwnerID(id string) *PetUpdate { _u.mutation.SetOwnerID(id) return _u } // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil. func (_u *PetUpdate) SetNillableOwnerID(id *string) *PetUpdate { if id != nil { _u = _u.SetOwnerID(*id) } return _u } // SetOwner sets the "owner" edge to the User entity. func (_u *PetUpdate) SetOwner(v *User) *PetUpdate { return _u.SetOwnerID(v.ID) } // Mutation returns the PetMutation object of the builder. func (_u *PetUpdate) Mutation() *PetMutation { return _u.mutation } // ClearTeam clears the "team" edge to the User entity. func (_u *PetUpdate) ClearTeam() *PetUpdate { _u.mutation.ClearTeam() return _u } // ClearOwner clears the "owner" edge to the User entity. func (_u *PetUpdate) ClearOwner() *PetUpdate { _u.mutation.ClearOwner() return _u } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *PetUpdate) Save(ctx context.Context) (int, error) { return withHooks(ctx, _u.gremlinSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *PetUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *PetUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *PetUpdate) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *PetUpdate) gremlinSave(ctx context.Context) (int, error) { res := &gremlin.Response{} query, bindings := _u.gremlin().Query() if err := _u.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } if err, ok := isConstantError(res); ok { return 0, err } _u.mutation.done = true return res.ReadInt() } func (_u *PetUpdate) gremlin() *dsl.Traversal { type constraint struct { pred *dsl.Traversal // constraint predicate. test *dsl.Traversal // test matches and its constant. } constraints := make([]*constraint, 0, 1) v := g.V().HasLabel(pet.Label) for _, p := range _u.mutation.Predicates() { p(v) } var ( rv = v.Clone() _ = rv trs []*dsl.Traversal ) if value, ok := _u.mutation.Age(); ok { v.Property(dsl.Single, pet.FieldAge, value) } if value, ok := _u.mutation.AddedAge(); ok { v.Property(dsl.Single, pet.FieldAge, __.Union(__.Values(pet.FieldAge), __.Constant(value)).Sum()) } if value, ok := _u.mutation.Name(); ok { v.Property(dsl.Single, pet.FieldName, value) } if value, ok := _u.mutation.UUID(); ok { v.Property(dsl.Single, pet.FieldUUID, value) } if value, ok := _u.mutation.Nickname(); ok { v.Property(dsl.Single, pet.FieldNickname, value) } if value, ok := _u.mutation.Trained(); ok { v.Property(dsl.Single, pet.FieldTrained, value) } if value, ok := _u.mutation.OptionalTime(); ok { v.Property(dsl.Single, pet.FieldOptionalTime, value) } var properties []any if _u.mutation.UUIDCleared() { properties = append(properties, pet.FieldUUID) } if _u.mutation.NicknameCleared() { properties = append(properties, pet.FieldNickname) } if _u.mutation.OptionalTimeCleared() { properties = append(properties, pet.FieldOptionalTime) } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } if _u.mutation.TeamCleared() { tr := rv.Clone().InE(user.TeamLabel).Drop().Iterate() trs = append(trs, tr) } for _, id := range _u.mutation.TeamIDs() { v.AddE(user.TeamLabel).From(g.V(id)).InV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(user.TeamLabel).OutV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(pet.Label, user.TeamLabel, id)), }) } if _u.mutation.OwnerCleared() { tr := rv.Clone().InE(user.PetsLabel).Drop().Iterate() trs = append(trs, tr) } for _, id := range _u.mutation.OwnerIDs() { v.AddE(user.PetsLabel).From(g.V(id)).InV() } v.Count() if len(constraints) > 0 { constraints = append(constraints, &constraint{ pred: rv.Count(), test: __.Is(p.GT(1)).Constant(&ConstraintError{msg: "update traversal contains more than one vertex"}), }) v = constraints[0].pred.Coalesce(constraints[0].test, v) for _, cr := range constraints[1:] { v = cr.pred.Coalesce(cr.test, v) } } trs = append(trs, v) return dsl.Join(trs...) } // PetUpdateOne is the builder for updating a single Pet entity. type PetUpdateOne struct { config fields []string hooks []Hook mutation *PetMutation } // SetAge sets the "age" field. func (_u *PetUpdateOne) SetAge(v float64) *PetUpdateOne { _u.mutation.ResetAge() _u.mutation.SetAge(v) return _u } // SetNillableAge sets the "age" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableAge(v *float64) *PetUpdateOne { if v != nil { _u.SetAge(*v) } return _u } // AddAge adds value to the "age" field. func (_u *PetUpdateOne) AddAge(v float64) *PetUpdateOne { _u.mutation.AddAge(v) return _u } // SetName sets the "name" field. func (_u *PetUpdateOne) SetName(v string) *PetUpdateOne { _u.mutation.SetName(v) return _u } // SetNillableName sets the "name" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableName(v *string) *PetUpdateOne { if v != nil { _u.SetName(*v) } return _u } // SetUUID sets the "uuid" field. func (_u *PetUpdateOne) SetUUID(v uuid.UUID) *PetUpdateOne { _u.mutation.SetUUID(v) return _u } // SetNillableUUID sets the "uuid" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableUUID(v *uuid.UUID) *PetUpdateOne { if v != nil { _u.SetUUID(*v) } return _u } // ClearUUID clears the value of the "uuid" field. func (_u *PetUpdateOne) ClearUUID() *PetUpdateOne { _u.mutation.ClearUUID() return _u } // SetNickname sets the "nickname" field. func (_u *PetUpdateOne) SetNickname(v string) *PetUpdateOne { _u.mutation.SetNickname(v) return _u } // SetNillableNickname sets the "nickname" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableNickname(v *string) *PetUpdateOne { if v != nil { _u.SetNickname(*v) } return _u } // ClearNickname clears the value of the "nickname" field. func (_u *PetUpdateOne) ClearNickname() *PetUpdateOne { _u.mutation.ClearNickname() return _u } // SetTrained sets the "trained" field. func (_u *PetUpdateOne) SetTrained(v bool) *PetUpdateOne { _u.mutation.SetTrained(v) return _u } // SetNillableTrained sets the "trained" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableTrained(v *bool) *PetUpdateOne { if v != nil { _u.SetTrained(*v) } return _u } // SetOptionalTime sets the "optional_time" field. func (_u *PetUpdateOne) SetOptionalTime(v time.Time) *PetUpdateOne { _u.mutation.SetOptionalTime(v) return _u } // SetNillableOptionalTime sets the "optional_time" field if the given value is not nil. func (_u *PetUpdateOne) SetNillableOptionalTime(v *time.Time) *PetUpdateOne { if v != nil { _u.SetOptionalTime(*v) } return _u } // ClearOptionalTime clears the value of the "optional_time" field. func (_u *PetUpdateOne) ClearOptionalTime() *PetUpdateOne { _u.mutation.ClearOptionalTime() return _u } // SetTeamID sets the "team" edge to the User entity by ID. func (_u *PetUpdateOne) SetTeamID(id string) *PetUpdateOne { _u.mutation.SetTeamID(id) return _u } // SetNillableTeamID sets the "team" edge to the User entity by ID if the given value is not nil. func (_u *PetUpdateOne) SetNillableTeamID(id *string) *PetUpdateOne { if id != nil { _u = _u.SetTeamID(*id) } return _u } // SetTeam sets the "team" edge to the User entity. func (_u *PetUpdateOne) SetTeam(v *User) *PetUpdateOne { return _u.SetTeamID(v.ID) } // SetOwnerID sets the "owner" edge to the User entity by ID. func (_u *PetUpdateOne) SetOwnerID(id string) *PetUpdateOne { _u.mutation.SetOwnerID(id) return _u } // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil. func (_u *PetUpdateOne) SetNillableOwnerID(id *string) *PetUpdateOne { if id != nil { _u = _u.SetOwnerID(*id) } return _u } // SetOwner sets the "owner" edge to the User entity. func (_u *PetUpdateOne) SetOwner(v *User) *PetUpdateOne { return _u.SetOwnerID(v.ID) } // Mutation returns the PetMutation object of the builder. func (_u *PetUpdateOne) Mutation() *PetMutation { return _u.mutation } // ClearTeam clears the "team" edge to the User entity. func (_u *PetUpdateOne) ClearTeam() *PetUpdateOne { _u.mutation.ClearTeam() return _u } // ClearOwner clears the "owner" edge to the User entity. func (_u *PetUpdateOne) ClearOwner() *PetUpdateOne { _u.mutation.ClearOwner() return _u } // Where appends a list predicates to the PetUpdate builder. func (_u *PetUpdateOne) Where(ps ...predicate.Pet) *PetUpdateOne { _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 *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Pet entity. func (_u *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { return withHooks(ctx, _u.gremlinSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *PetUpdateOne) SaveX(ctx context.Context) *Pet { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *PetUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *PetUpdateOne) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *PetUpdateOne) gremlinSave(ctx context.Context) (*Pet, error) { res := &gremlin.Response{} id, ok := _u.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Pet.id" for update`)} } query, bindings := _u.gremlin(id).Query() if err := _u.driver.Exec(ctx, query, bindings, res); err != nil { return nil, err } if err, ok := isConstantError(res); ok { return nil, err } _u.mutation.done = true _m := &Pet{config: _u.config} if err := _m.FromResponse(res); err != nil { return nil, err } return _m, nil } func (_u *PetUpdateOne) gremlin(id string) *dsl.Traversal { type constraint struct { pred *dsl.Traversal // constraint predicate. test *dsl.Traversal // test matches and its constant. } constraints := make([]*constraint, 0, 1) v := g.V(id) var ( rv = v.Clone() _ = rv trs []*dsl.Traversal ) if value, ok := _u.mutation.Age(); ok { v.Property(dsl.Single, pet.FieldAge, value) } if value, ok := _u.mutation.AddedAge(); ok { v.Property(dsl.Single, pet.FieldAge, __.Union(__.Values(pet.FieldAge), __.Constant(value)).Sum()) } if value, ok := _u.mutation.Name(); ok { v.Property(dsl.Single, pet.FieldName, value) } if value, ok := _u.mutation.UUID(); ok { v.Property(dsl.Single, pet.FieldUUID, value) } if value, ok := _u.mutation.Nickname(); ok { v.Property(dsl.Single, pet.FieldNickname, value) } if value, ok := _u.mutation.Trained(); ok { v.Property(dsl.Single, pet.FieldTrained, value) } if value, ok := _u.mutation.OptionalTime(); ok { v.Property(dsl.Single, pet.FieldOptionalTime, value) } var properties []any if _u.mutation.UUIDCleared() { properties = append(properties, pet.FieldUUID) } if _u.mutation.NicknameCleared() { properties = append(properties, pet.FieldNickname) } if _u.mutation.OptionalTimeCleared() { properties = append(properties, pet.FieldOptionalTime) } if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } if _u.mutation.TeamCleared() { tr := rv.Clone().InE(user.TeamLabel).Drop().Iterate() trs = append(trs, tr) } for _, id := range _u.mutation.TeamIDs() { v.AddE(user.TeamLabel).From(g.V(id)).InV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(user.TeamLabel).OutV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(pet.Label, user.TeamLabel, id)), }) } if _u.mutation.OwnerCleared() { tr := rv.Clone().InE(user.PetsLabel).Drop().Iterate() trs = append(trs, tr) } for _, id := range _u.mutation.OwnerIDs() { v.AddE(user.PetsLabel).From(g.V(id)).InV() } if len(_u.fields) > 0 { fields := make([]any, 0, len(_u.fields)+1) fields = append(fields, true) for _, f := range _u.fields { fields = append(fields, f) } v.ValueMap(fields...) } else { v.ValueMap(true) } if len(constraints) > 0 { v = constraints[0].pred.Coalesce(constraints[0].test, v) for _, cr := range constraints[1:] { v = cr.pred.Coalesce(cr.test, v) } } trs = append(trs, v) return dsl.Join(trs...) }