// 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" schemadir "entgo.io/ent/entc/integration/ent/schema/dir" "entgo.io/ent/entc/integration/gremlin/ent/comment" ) // CommentCreate is the builder for creating a Comment entity. type CommentCreate struct { config mutation *CommentMutation hooks []Hook } // SetUniqueInt sets the "unique_int" field. func (_c *CommentCreate) SetUniqueInt(v int) *CommentCreate { _c.mutation.SetUniqueInt(v) return _c } // SetUniqueFloat sets the "unique_float" field. func (_c *CommentCreate) SetUniqueFloat(v float64) *CommentCreate { _c.mutation.SetUniqueFloat(v) return _c } // SetNillableInt sets the "nillable_int" field. func (_c *CommentCreate) SetNillableInt(v int) *CommentCreate { _c.mutation.SetNillableInt(v) return _c } // SetNillableNillableInt sets the "nillable_int" field if the given value is not nil. func (_c *CommentCreate) SetNillableNillableInt(v *int) *CommentCreate { if v != nil { _c.SetNillableInt(*v) } return _c } // SetTable sets the "table" field. func (_c *CommentCreate) SetTable(v string) *CommentCreate { _c.mutation.SetTable(v) return _c } // SetNillableTable sets the "table" field if the given value is not nil. func (_c *CommentCreate) SetNillableTable(v *string) *CommentCreate { if v != nil { _c.SetTable(*v) } return _c } // SetDir sets the "dir" field. func (_c *CommentCreate) SetDir(v schemadir.Dir) *CommentCreate { _c.mutation.SetDir(v) return _c } // SetNillableDir sets the "dir" field if the given value is not nil. func (_c *CommentCreate) SetNillableDir(v *schemadir.Dir) *CommentCreate { if v != nil { _c.SetDir(*v) } return _c } // SetClient sets the "client" field. func (_c *CommentCreate) SetClient(v string) *CommentCreate { _c.mutation.SetClient(v) return _c } // SetNillableClient sets the "client" field if the given value is not nil. func (_c *CommentCreate) SetNillableClient(v *string) *CommentCreate { if v != nil { _c.SetClient(*v) } return _c } // Mutation returns the CommentMutation object of the builder. func (_c *CommentCreate) Mutation() *CommentMutation { return _c.mutation } // Save creates the Comment in the database. func (_c *CommentCreate) Save(ctx context.Context) (*Comment, error) { return withHooks(ctx, _c.gremlinSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *CommentCreate) SaveX(ctx context.Context) *Comment { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *CommentCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *CommentCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (_c *CommentCreate) check() error { if _, ok := _c.mutation.UniqueInt(); !ok { return &ValidationError{Name: "unique_int", err: errors.New(`ent: missing required field "Comment.unique_int"`)} } if _, ok := _c.mutation.UniqueFloat(); !ok { return &ValidationError{Name: "unique_float", err: errors.New(`ent: missing required field "Comment.unique_float"`)} } return nil } func (_c *CommentCreate) gremlinSave(ctx context.Context) (*Comment, error) { if err := _c.check(); err != nil { return nil, err } res := &gremlin.Response{} query, bindings := _c.gremlin().Query() if err := _c.driver.Exec(ctx, query, bindings, res); err != nil { return nil, err } if err, ok := isConstantError(res); ok { return nil, err } rnode := &Comment{config: _c.config} if err := rnode.FromResponse(res); err != nil { return nil, err } _c.mutation.id = &rnode.ID _c.mutation.done = true return rnode, nil } func (_c *CommentCreate) gremlin() *dsl.Traversal { type constraint struct { pred *dsl.Traversal // constraint predicate. test *dsl.Traversal // test matches and its constant. } constraints := make([]*constraint, 0, 2) v := g.AddV(comment.Label) if value, ok := _c.mutation.UniqueInt(); ok { constraints = append(constraints, &constraint{ pred: g.V().Has(comment.Label, comment.FieldUniqueInt, value).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueInt, value)), }) v.Property(dsl.Single, comment.FieldUniqueInt, value) } if value, ok := _c.mutation.UniqueFloat(); ok { constraints = append(constraints, &constraint{ pred: g.V().Has(comment.Label, comment.FieldUniqueFloat, value).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueFloat, value)), }) v.Property(dsl.Single, comment.FieldUniqueFloat, value) } if value, ok := _c.mutation.NillableInt(); ok { v.Property(dsl.Single, comment.FieldNillableInt, value) } if value, ok := _c.mutation.Table(); ok { v.Property(dsl.Single, comment.FieldTable, value) } if value, ok := _c.mutation.Dir(); ok { v.Property(dsl.Single, comment.FieldDir, value) } if value, ok := _c.mutation.GetClient(); ok { v.Property(dsl.Single, comment.FieldClient, value) } if len(constraints) == 0 { return v.ValueMap(true) } tr := constraints[0].pred.Coalesce(constraints[0].test, v.ValueMap(true)) for _, cr := range constraints[1:] { tr = cr.pred.Coalesce(cr.test, tr) } return tr } // CommentCreateBulk is the builder for creating many Comment entities in bulk. type CommentCreateBulk struct { config err error builders []*CommentCreate }