Files
ent/entc/integration/migrate/entv2/group_create.go
Ariel Mashraki 2cc8286f5f entc: add idtype option codegen
Reviewed By: alexsn

Differential Revision: D16601757

fbshipit-source-id: 35d5fbfb4ef40bae192e084ad4e067880175e71c
2019-08-04 03:13:01 -07:00

63 lines
1.3 KiB
Go

// Code generated (@generated) by entc, DO NOT EDIT.
package entv2
import (
"context"
"errors"
"fbc/ent/entc/integration/migrate/entv2/group"
"fbc/ent/dialect"
"fbc/ent/dialect/sql"
)
// GroupCreate is the builder for creating a Group entity.
type GroupCreate struct {
config
}
// Save creates the Group in the database.
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
switch gc.driver.Dialect() {
case dialect.MySQL, dialect.SQLite:
return gc.sqlSave(ctx)
default:
return nil, errors.New("entv2: unsupported dialect")
}
}
// SaveX calls Save and panics if Save returns an error.
func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
v, err := gc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
var (
res sql.Result
gr = &Group{config: gc.config}
)
tx, err := gc.driver.Tx(ctx)
if err != nil {
return nil, err
}
builder := sql.Insert(group.Table).Default(gc.driver.Dialect())
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)
}
gr.ID = int(id)
if err := tx.Commit(); err != nil {
return nil, err
}
return gr, nil
}