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/182 Reviewed By: alexsn Differential Revision: D18638199 fbshipit-source-id: 0de79c78b51e544486c07a004c3c8ea82e5c3398
56 lines
1.3 KiB
Go
56 lines
1.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"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql"
|
|
"github.com/facebookincubator/ent/entc/integration/config/ent/user"
|
|
)
|
|
|
|
// UserCreate is the builder for creating a User entity.
|
|
type UserCreate struct {
|
|
config
|
|
}
|
|
|
|
// Save creates the User in the database.
|
|
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
|
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 (
|
|
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()
|
|
|
|
id, err := insertLastID(ctx, tx, insert.Returning(user.FieldID))
|
|
if err != nil {
|
|
return nil, rollback(tx, err)
|
|
}
|
|
u.ID = int(id)
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return u, nil
|
|
}
|