mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/123 Reviewed By: alexsn Differential Revision: D18117641 fbshipit-source-id: 8e520a1b1f70c7bea70f8a37ac698116e98a804e
237 lines
6.3 KiB
Go
237 lines
6.3 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"
|
|
"strconv"
|
|
"time"
|
|
|
|
"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"
|
|
"github.com/facebookincubator/ent/entc/integration/ent/card"
|
|
"github.com/facebookincubator/ent/entc/integration/ent/user"
|
|
)
|
|
|
|
// CardCreate is the builder for creating a Card entity.
|
|
type CardCreate struct {
|
|
config
|
|
create_time *time.Time
|
|
update_time *time.Time
|
|
number *string
|
|
owner map[string]struct{}
|
|
}
|
|
|
|
// SetCreateTime sets the create_time field.
|
|
func (cc *CardCreate) SetCreateTime(t time.Time) *CardCreate {
|
|
cc.create_time = &t
|
|
return cc
|
|
}
|
|
|
|
// SetNillableCreateTime sets the create_time field if the given value is not nil.
|
|
func (cc *CardCreate) SetNillableCreateTime(t *time.Time) *CardCreate {
|
|
if t != nil {
|
|
cc.SetCreateTime(*t)
|
|
}
|
|
return cc
|
|
}
|
|
|
|
// SetUpdateTime sets the update_time field.
|
|
func (cc *CardCreate) SetUpdateTime(t time.Time) *CardCreate {
|
|
cc.update_time = &t
|
|
return cc
|
|
}
|
|
|
|
// SetNillableUpdateTime sets the update_time field if the given value is not nil.
|
|
func (cc *CardCreate) SetNillableUpdateTime(t *time.Time) *CardCreate {
|
|
if t != nil {
|
|
cc.SetUpdateTime(*t)
|
|
}
|
|
return cc
|
|
}
|
|
|
|
// SetNumber sets the number field.
|
|
func (cc *CardCreate) SetNumber(s string) *CardCreate {
|
|
cc.number = &s
|
|
return cc
|
|
}
|
|
|
|
// SetOwnerID sets the owner edge to User by id.
|
|
func (cc *CardCreate) SetOwnerID(id string) *CardCreate {
|
|
if cc.owner == nil {
|
|
cc.owner = make(map[string]struct{})
|
|
}
|
|
cc.owner[id] = struct{}{}
|
|
return cc
|
|
}
|
|
|
|
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
|
|
func (cc *CardCreate) SetNillableOwnerID(id *string) *CardCreate {
|
|
if id != nil {
|
|
cc = cc.SetOwnerID(*id)
|
|
}
|
|
return cc
|
|
}
|
|
|
|
// SetOwner sets the owner edge to User.
|
|
func (cc *CardCreate) SetOwner(u *User) *CardCreate {
|
|
return cc.SetOwnerID(u.ID)
|
|
}
|
|
|
|
// Save creates the Card in the database.
|
|
func (cc *CardCreate) Save(ctx context.Context) (*Card, error) {
|
|
if cc.create_time == nil {
|
|
v := card.DefaultCreateTime()
|
|
cc.create_time = &v
|
|
}
|
|
if cc.update_time == nil {
|
|
v := card.DefaultUpdateTime()
|
|
cc.update_time = &v
|
|
}
|
|
if cc.number == nil {
|
|
return nil, errors.New("ent: missing required field \"number\"")
|
|
}
|
|
if err := card.NumberValidator(*cc.number); err != nil {
|
|
return nil, fmt.Errorf("ent: validator failed for field \"number\": %v", err)
|
|
}
|
|
if len(cc.owner) > 1 {
|
|
return nil, errors.New("ent: multiple assignments on a unique edge \"owner\"")
|
|
}
|
|
switch cc.driver.Dialect() {
|
|
case dialect.MySQL, dialect.Postgres, dialect.SQLite:
|
|
return cc.sqlSave(ctx)
|
|
case dialect.Gremlin:
|
|
return cc.gremlinSave(ctx)
|
|
default:
|
|
return nil, errors.New("ent: unsupported dialect")
|
|
}
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (cc *CardCreate) SaveX(ctx context.Context) *Card {
|
|
v, err := cc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) {
|
|
var (
|
|
res sql.Result
|
|
c = &Card{config: cc.config}
|
|
)
|
|
tx, err := cc.driver.Tx(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
builder := sql.Dialect(cc.driver.Dialect()).
|
|
Insert(card.Table).
|
|
Default()
|
|
if value := cc.create_time; value != nil {
|
|
builder.Set(card.FieldCreateTime, *value)
|
|
c.CreateTime = *value
|
|
}
|
|
if value := cc.update_time; value != nil {
|
|
builder.Set(card.FieldUpdateTime, *value)
|
|
c.UpdateTime = *value
|
|
}
|
|
if value := cc.number; value != nil {
|
|
builder.Set(card.FieldNumber, *value)
|
|
c.Number = *value
|
|
}
|
|
query, args := builder.Query()
|
|
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
|
return nil, rollback(tx, err)
|
|
}
|
|
id, err := res.LastInsertId()
|
|
if err != nil {
|
|
return nil, rollback(tx, err)
|
|
}
|
|
c.ID = strconv.FormatInt(id, 10)
|
|
if len(cc.owner) > 0 {
|
|
eid, err := strconv.Atoi(keys(cc.owner)[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query, args := sql.Update(card.OwnerTable).
|
|
Set(card.OwnerColumn, eid).
|
|
Where(sql.EQ(card.FieldID, id).And().IsNull(card.OwnerColumn)).
|
|
Query()
|
|
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
|
return nil, rollback(tx, err)
|
|
}
|
|
affected, err := res.RowsAffected()
|
|
if err != nil {
|
|
return nil, rollback(tx, err)
|
|
}
|
|
if int(affected) < len(cc.owner) {
|
|
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"owner\" %v already connected to a different \"Card\"", keys(cc.owner))})
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (cc *CardCreate) gremlinSave(ctx context.Context) (*Card, error) {
|
|
res := &gremlin.Response{}
|
|
query, bindings := cc.gremlin().Query()
|
|
if err := cc.driver.Exec(ctx, query, bindings, res); err != nil {
|
|
return nil, err
|
|
}
|
|
if err, ok := isConstantError(res); ok {
|
|
return nil, err
|
|
}
|
|
c := &Card{config: cc.config}
|
|
if err := c.FromResponse(res); err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (cc *CardCreate) 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.AddV(card.Label)
|
|
if cc.create_time != nil {
|
|
v.Property(dsl.Single, card.FieldCreateTime, *cc.create_time)
|
|
}
|
|
if cc.update_time != nil {
|
|
v.Property(dsl.Single, card.FieldUpdateTime, *cc.update_time)
|
|
}
|
|
if cc.number != nil {
|
|
v.Property(dsl.Single, card.FieldNumber, *cc.number)
|
|
}
|
|
for id := range cc.owner {
|
|
v.AddE(user.CardLabel).From(g.V(id)).InV()
|
|
constraints = append(constraints, &constraint{
|
|
pred: g.E().HasLabel(user.CardLabel).OutV().HasID(id).Count(),
|
|
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(card.Label, user.CardLabel, id)),
|
|
})
|
|
}
|
|
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
|
|
}
|