mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Reviewed By: alexsn Differential Revision: D16601757 fbshipit-source-id: 35d5fbfb4ef40bae192e084ad4e067880175e71c
63 lines
1.3 KiB
Go
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
|
|
}
|