Files
ent/entc/integration/ent/comment_update.go
Ariel Mashraki 5bc8568069 all: add license reference to README and add copyright headers to gencode
Reviewed By: alexsn

Differential Revision: D17119262

fbshipit-source-id: 046f095ca9432c920778db0edb2158dedb23c0a2
2019-08-30 08:46:03 -07:00

359 lines
9.6 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 (@generated) by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/facebookincubator/ent/entc/integration/ent/comment"
"github.com/facebookincubator/ent/entc/integration/ent/predicate"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/dialect/gremlin"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/__"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/g"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/p"
"github.com/facebookincubator/ent/dialect/sql"
)
// CommentUpdate is the builder for updating Comment entities.
type CommentUpdate struct {
config
unique_int *int
unique_float *float64
predicates []predicate.Comment
}
// Where adds a new predicate for the builder.
func (cu *CommentUpdate) Where(ps ...predicate.Comment) *CommentUpdate {
cu.predicates = append(cu.predicates, ps...)
return cu
}
// SetUniqueInt sets the unique_int field.
func (cu *CommentUpdate) SetUniqueInt(i int) *CommentUpdate {
cu.unique_int = &i
return cu
}
// SetUniqueFloat sets the unique_float field.
func (cu *CommentUpdate) SetUniqueFloat(f float64) *CommentUpdate {
cu.unique_float = &f
return cu
}
// Save executes the query and returns the number of rows/vertices matched by this operation.
func (cu *CommentUpdate) Save(ctx context.Context) (int, error) {
switch cu.driver.Dialect() {
case dialect.MySQL, dialect.SQLite:
return cu.sqlSave(ctx)
case dialect.Gremlin:
return cu.gremlinSave(ctx)
default:
return 0, errors.New("ent: unsupported dialect")
}
}
// SaveX is like Save, but panics if an error occurs.
func (cu *CommentUpdate) SaveX(ctx context.Context) int {
affected, err := cu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (cu *CommentUpdate) Exec(ctx context.Context) error {
_, err := cu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (cu *CommentUpdate) ExecX(ctx context.Context) {
if err := cu.Exec(ctx); err != nil {
panic(err)
}
}
func (cu *CommentUpdate) sqlSave(ctx context.Context) (n int, err error) {
selector := sql.Select(comment.FieldID).From(sql.Table(comment.Table))
for _, p := range cu.predicates {
p(selector)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err = cu.driver.Query(ctx, query, args, rows); err != nil {
return 0, err
}
defer rows.Close()
var ids []int
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return 0, fmt.Errorf("ent: failed reading id: %v", err)
}
ids = append(ids, id)
}
if len(ids) == 0 {
return 0, nil
}
tx, err := cu.driver.Tx(ctx)
if err != nil {
return 0, err
}
var (
update bool
res sql.Result
builder = sql.Update(comment.Table).Where(sql.InInts(comment.FieldID, ids...))
)
if cu.unique_int != nil {
update = true
builder.Set(comment.FieldUniqueInt, *cu.unique_int)
}
if cu.unique_float != nil {
update = true
builder.Set(comment.FieldUniqueFloat, *cu.unique_float)
}
if update {
query, args := builder.Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return 0, rollback(tx, err)
}
}
if err = tx.Commit(); err != nil {
return 0, err
}
return len(ids), nil
}
func (cu *CommentUpdate) gremlinSave(ctx context.Context) (int, error) {
res := &gremlin.Response{}
query, bindings := cu.gremlin().Query()
if err := cu.driver.Exec(ctx, query, bindings, res); err != nil {
return 0, err
}
if err, ok := isConstantError(res); ok {
return 0, err
}
return res.ReadInt()
}
func (cu *CommentUpdate) 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.V().HasLabel(comment.Label)
for _, p := range cu.predicates {
p(v)
}
var (
rv = v.Clone()
_ = rv
trs []*dsl.Traversal
)
if cu.unique_int != nil {
constraints = append(constraints, &constraint{
pred: g.V().Has(comment.Label, comment.FieldUniqueInt, *cu.unique_int).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueInt, *cu.unique_int)),
})
v.Property(dsl.Single, comment.FieldUniqueInt, *cu.unique_int)
}
if cu.unique_float != nil {
constraints = append(constraints, &constraint{
pred: g.V().Has(comment.Label, comment.FieldUniqueFloat, *cu.unique_float).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueFloat, *cu.unique_float)),
})
v.Property(dsl.Single, comment.FieldUniqueFloat, *cu.unique_float)
}
v.Count()
if len(constraints) > 0 {
constraints = append(constraints, &constraint{
pred: rv.Count(),
test: __.Is(p.GT(1)).Constant(&ErrConstraintFailed{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...)
}
// CommentUpdateOne is the builder for updating a single Comment entity.
type CommentUpdateOne struct {
config
id string
unique_int *int
unique_float *float64
}
// SetUniqueInt sets the unique_int field.
func (cuo *CommentUpdateOne) SetUniqueInt(i int) *CommentUpdateOne {
cuo.unique_int = &i
return cuo
}
// SetUniqueFloat sets the unique_float field.
func (cuo *CommentUpdateOne) SetUniqueFloat(f float64) *CommentUpdateOne {
cuo.unique_float = &f
return cuo
}
// Save executes the query and returns the updated entity.
func (cuo *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) {
switch cuo.driver.Dialect() {
case dialect.MySQL, dialect.SQLite:
return cuo.sqlSave(ctx)
case dialect.Gremlin:
return cuo.gremlinSave(ctx)
default:
return nil, errors.New("ent: unsupported dialect")
}
}
// SaveX is like Save, but panics if an error occurs.
func (cuo *CommentUpdateOne) SaveX(ctx context.Context) *Comment {
c, err := cuo.Save(ctx)
if err != nil {
panic(err)
}
return c
}
// Exec executes the query on the entity.
func (cuo *CommentUpdateOne) Exec(ctx context.Context) error {
_, err := cuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (cuo *CommentUpdateOne) ExecX(ctx context.Context) {
if err := cuo.Exec(ctx); err != nil {
panic(err)
}
}
func (cuo *CommentUpdateOne) sqlSave(ctx context.Context) (c *Comment, err error) {
selector := sql.Select(comment.Columns...).From(sql.Table(comment.Table))
comment.ID(cuo.id)(selector)
rows := &sql.Rows{}
query, args := selector.Query()
if err = cuo.driver.Query(ctx, query, args, rows); err != nil {
return nil, err
}
defer rows.Close()
var ids []int
for rows.Next() {
var id int
c = &Comment{config: cuo.config}
if err := c.FromRows(rows); err != nil {
return nil, fmt.Errorf("ent: failed scanning row into Comment: %v", err)
}
id = c.id()
ids = append(ids, id)
}
switch n := len(ids); {
case n == 0:
return nil, fmt.Errorf("ent: Comment not found with id: %v", cuo.id)
case n > 1:
return nil, fmt.Errorf("ent: more than one Comment with the same id: %v", cuo.id)
}
tx, err := cuo.driver.Tx(ctx)
if err != nil {
return nil, err
}
var (
update bool
res sql.Result
builder = sql.Update(comment.Table).Where(sql.InInts(comment.FieldID, ids...))
)
if cuo.unique_int != nil {
update = true
builder.Set(comment.FieldUniqueInt, *cuo.unique_int)
c.UniqueInt = *cuo.unique_int
}
if cuo.unique_float != nil {
update = true
builder.Set(comment.FieldUniqueFloat, *cuo.unique_float)
c.UniqueFloat = *cuo.unique_float
}
if update {
query, args := builder.Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return nil, rollback(tx, err)
}
}
if err = tx.Commit(); err != nil {
return nil, err
}
return c, nil
}
func (cuo *CommentUpdateOne) gremlinSave(ctx context.Context) (*Comment, error) {
res := &gremlin.Response{}
query, bindings := cuo.gremlin(cuo.id).Query()
if err := cuo.driver.Exec(ctx, query, bindings, res); err != nil {
return nil, err
}
if err, ok := isConstantError(res); ok {
return nil, err
}
c := &Comment{config: cuo.config}
if err := c.FromResponse(res); err != nil {
return nil, err
}
return c, nil
}
func (cuo *CommentUpdateOne) 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, 2)
v := g.V(id)
var (
rv = v.Clone()
_ = rv
trs []*dsl.Traversal
)
if cuo.unique_int != nil {
constraints = append(constraints, &constraint{
pred: g.V().Has(comment.Label, comment.FieldUniqueInt, *cuo.unique_int).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueInt, *cuo.unique_int)),
})
v.Property(dsl.Single, comment.FieldUniqueInt, *cuo.unique_int)
}
if cuo.unique_float != nil {
constraints = append(constraints, &constraint{
pred: g.V().Has(comment.Label, comment.FieldUniqueFloat, *cuo.unique_float).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(comment.Label, comment.FieldUniqueFloat, *cuo.unique_float)),
})
v.Property(dsl.Single, comment.FieldUniqueFloat, *cuo.unique_float)
}
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...)
}