// 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" "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/ent/schema" "entgo.io/ent/entc/integration/gremlin/ent/document" "entgo.io/ent/entc/integration/gremlin/ent/predicate" ) // DocumentUpdate is the builder for updating Document entities. type DocumentUpdate struct { config hooks []Hook mutation *DocumentMutation } // Where appends a list predicates to the DocumentUpdate builder. func (_u *DocumentUpdate) Where(ps ...predicate.Document) *DocumentUpdate { _u.mutation.Where(ps...) return _u } // SetName sets the "name" field. func (_u *DocumentUpdate) SetName(v string) *DocumentUpdate { _u.mutation.SetName(v) return _u } // SetNillableName sets the "name" field if the given value is not nil. func (_u *DocumentUpdate) SetNillableName(v *string) *DocumentUpdate { if v != nil { _u.SetName(*v) } return _u } // Mutation returns the DocumentMutation object of the builder. func (_u *DocumentUpdate) Mutation() *DocumentMutation { return _u.mutation } // Save executes the query and returns the number of nodes affected by the update operation. func (_u *DocumentUpdate) 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 *DocumentUpdate) SaveX(ctx context.Context) int { affected, err := _u.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (_u *DocumentUpdate) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *DocumentUpdate) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *DocumentUpdate) 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 *DocumentUpdate) 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(document.Label) for _, p := range _u.mutation.Predicates() { p(v) } var ( rv = v.Clone() _ = rv trs []*dsl.Traversal ) if value, ok := _u.mutation.Name(); ok { constraints = append(constraints, &constraint{ pred: g.V().Has(document.Label, document.FieldName, value).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(document.Label, document.FieldName, value)), }) v.Property(dsl.Single, document.FieldName, value) } var properties []any if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } 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...) } // DocumentUpdateOne is the builder for updating a single Document entity. type DocumentUpdateOne struct { config fields []string hooks []Hook mutation *DocumentMutation } // SetName sets the "name" field. func (_u *DocumentUpdateOne) SetName(v string) *DocumentUpdateOne { _u.mutation.SetName(v) return _u } // SetNillableName sets the "name" field if the given value is not nil. func (_u *DocumentUpdateOne) SetNillableName(v *string) *DocumentUpdateOne { if v != nil { _u.SetName(*v) } return _u } // SetAttachment sets the "attachment" field. func (_u *DocumentUpdateOne) SetAttachment(v []byte) *DocumentUpdateOne { _u.mutation.SetAttachment(v) return _u } // SetNillableAttachment sets the "attachment" field if the given value is not nil. func (_u *DocumentUpdateOne) SetNillableAttachment(v *[]byte) *DocumentUpdateOne { if v != nil { _u.SetAttachment(*v) } return _u } // SetMetadata sets the "metadata" field. func (_u *DocumentUpdateOne) SetMetadata(v []byte) *DocumentUpdateOne { _u.mutation.SetMetadata(v) return _u } // SetNillableMetadata sets the "metadata" field if the given value is not nil. func (_u *DocumentUpdateOne) SetNillableMetadata(v *[]byte) *DocumentUpdateOne { if v != nil { _u.SetMetadata(*v) } return _u } // ClearMetadata clears the value of the "metadata" field. func (_u *DocumentUpdateOne) ClearMetadata() *DocumentUpdateOne { _u.mutation.ClearMetadata() return _u } // SetPayload sets the "payload" field. func (_u *DocumentUpdateOne) SetPayload(v *schema.DocPayload) *DocumentUpdateOne { _u.mutation.SetPayload(v) return _u } // ClearPayload clears the value of the "payload" field. func (_u *DocumentUpdateOne) ClearPayload() *DocumentUpdateOne { _u.mutation.ClearPayload() return _u } // SetDescription sets the "description" field. func (_u *DocumentUpdateOne) SetDescription(v string) *DocumentUpdateOne { _u.mutation.SetDescription(v) return _u } // SetNillableDescription sets the "description" field if the given value is not nil. func (_u *DocumentUpdateOne) SetNillableDescription(v *string) *DocumentUpdateOne { if v != nil { _u.SetDescription(*v) } return _u } // ClearDescription clears the value of the "description" field. func (_u *DocumentUpdateOne) ClearDescription() *DocumentUpdateOne { _u.mutation.ClearDescription() return _u } // Mutation returns the DocumentMutation object of the builder. func (_u *DocumentUpdateOne) Mutation() *DocumentMutation { return _u.mutation } // Where appends a list predicates to the DocumentUpdate builder. func (_u *DocumentUpdateOne) Where(ps ...predicate.Document) *DocumentUpdateOne { _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 *DocumentUpdateOne) Select(field string, fields ...string) *DocumentUpdateOne { _u.fields = append([]string{field}, fields...) return _u } // Save executes the query and returns the updated Document entity. func (_u *DocumentUpdateOne) Save(ctx context.Context) (*Document, error) { return withHooks(ctx, _u.gremlinSave, _u.mutation, _u.hooks) } // SaveX is like Save, but panics if an error occurs. func (_u *DocumentUpdateOne) SaveX(ctx context.Context) *Document { node, err := _u.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (_u *DocumentUpdateOne) Exec(ctx context.Context) error { _, err := _u.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_u *DocumentUpdateOne) ExecX(ctx context.Context) { if err := _u.Exec(ctx); err != nil { panic(err) } } func (_u *DocumentUpdateOne) gremlinSave(ctx context.Context) (*Document, error) { res := &gremlin.Response{} id, ok := _u.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Document.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 := &Document{config: _u.config} if err := _m.FromResponse(res); err != nil { return nil, err } return _m, nil } func (_u *DocumentUpdateOne) 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.Name(); ok { constraints = append(constraints, &constraint{ pred: g.V().Has(document.Label, document.FieldName, value).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(document.Label, document.FieldName, value)), }) v.Property(dsl.Single, document.FieldName, value) } var properties []any if len(properties) > 0 { v.SideEffect(__.Properties(properties...).Drop()) } 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...) }