Files
ent/examples/m2mbidi/ent/user_create.go
Ariel Mashraki c259aee24b entc/gen: generate sql builders with dialect option
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/130

Reviewed By: alexsn

Differential Revision: D18164397

fbshipit-source-id: 2858d69d3ff85c06b51382c01c3d4369ee2c3bdb
2019-10-27 21:53:50 -07:00

119 lines
2.7 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"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/examples/m2mbidi/ent/user"
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
age *int
name *string
friends map[int]struct{}
}
// SetAge sets the age field.
func (uc *UserCreate) SetAge(i int) *UserCreate {
uc.age = &i
return uc
}
// SetName sets the name field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.name = &s
return uc
}
// AddFriendIDs adds the friends edge to User by ids.
func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate {
if uc.friends == nil {
uc.friends = make(map[int]struct{})
}
for i := range ids {
uc.friends[ids[i]] = struct{}{}
}
return uc
}
// AddFriends adds the friends edges to User.
func (uc *UserCreate) AddFriends(u ...*User) *UserCreate {
ids := make([]int, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFriendIDs(ids...)
}
// Save creates the User in the database.
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
if uc.age == nil {
return nil, errors.New("ent: missing required field \"age\"")
}
if uc.name == nil {
return nil, errors.New("ent: missing required field \"name\"")
}
return uc.sqlSave(ctx)
}
// SaveX calls Save and panics if Save returns an error.
func (uc *UserCreate) SaveX(ctx context.Context) *User {
v, err := uc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
var (
res sql.Result
builder = sql.Dialect(uc.driver.Dialect())
u = &User{config: uc.config}
)
tx, err := uc.driver.Tx(ctx)
if err != nil {
return nil, err
}
insert := builder.Insert(user.Table).Default()
if value := uc.age; value != nil {
insert.Set(user.FieldAge, *value)
u.Age = *value
}
if value := uc.name; value != nil {
insert.Set(user.FieldName, *value)
u.Name = *value
}
id, err := insertLastID(ctx, tx, insert.Returning(user.FieldID))
if err != nil {
return nil, rollback(tx, err)
}
u.ID = int(id)
if len(uc.friends) > 0 {
for eid := range uc.friends {
query, args := builder.Insert(user.FriendsTable).
Columns(user.FriendsPrimaryKey[0], user.FriendsPrimaryKey[1]).
Values(id, eid).
Values(eid, id).
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 u, nil
}