mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: move node creation to sqlgraph
This commit is contained in:
@@ -11,9 +11,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
"github.com/facebookincubator/ent/examples/start/ent/car"
|
||||
"github.com/facebookincubator/ent/examples/start/ent/group"
|
||||
"github.com/facebookincubator/ent/examples/start/ent/user"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserCreate is the builder for creating a User entity.
|
||||
@@ -111,63 +113,78 @@ func (uc *UserCreate) SaveX(ctx context.Context) *User {
|
||||
|
||||
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
var (
|
||||
res sql.Result
|
||||
builder = sql.Dialect(uc.driver.Dialect())
|
||||
u = &User{config: uc.config}
|
||||
u = &User{config: uc.config}
|
||||
spec = &sqlgraph.CreateSpec{
|
||||
Table: user.Table,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: user.FieldID,
|
||||
},
|
||||
}
|
||||
)
|
||||
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)
|
||||
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Value: *value,
|
||||
Column: user.FieldAge,
|
||||
})
|
||||
u.Age = *value
|
||||
}
|
||||
if value := uc.name; value != nil {
|
||||
insert.Set(user.FieldName, *value)
|
||||
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
|
||||
Type: field.TypeString,
|
||||
Value: *value,
|
||||
Column: user.FieldName,
|
||||
})
|
||||
u.Name = *value
|
||||
}
|
||||
|
||||
id, err := insertLastID(ctx, tx, insert.Returning(user.FieldID))
|
||||
if err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
if nodes := uc.cars; len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: false,
|
||||
Table: user.CarsTable,
|
||||
Columns: []string{user.CarsColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: car.FieldID,
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, _ := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
spec.Edges = append(spec.Edges, edge)
|
||||
}
|
||||
u.ID = int(id)
|
||||
if len(uc.cars) > 0 {
|
||||
p := sql.P()
|
||||
for eid := range uc.cars {
|
||||
p.Or().EQ(car.FieldID, eid)
|
||||
if nodes := uc.groups; len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2M,
|
||||
Inverse: true,
|
||||
Table: user.GroupsTable,
|
||||
Columns: user.GroupsPrimaryKey,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: group.FieldID,
|
||||
},
|
||||
},
|
||||
}
|
||||
query, args := builder.Update(user.CarsTable).
|
||||
Set(user.CarsColumn, id).
|
||||
Where(sql.And(p, sql.IsNull(user.CarsColumn))).
|
||||
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(uc.cars) {
|
||||
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"cars\" %v already connected to a different \"User\"", keys(uc.cars))})
|
||||
for k, _ := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
spec.Edges = append(spec.Edges, edge)
|
||||
}
|
||||
if len(uc.groups) > 0 {
|
||||
for eid := range uc.groups {
|
||||
|
||||
query, args := builder.Insert(user.GroupsTable).
|
||||
Columns(user.GroupsPrimaryKey[1], user.GroupsPrimaryKey[0]).
|
||||
Values(id, eid).
|
||||
Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
}
|
||||
if err := sqlgraph.CreateNode(ctx, uc.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := spec.ID.Value.(int64)
|
||||
u.ID = int(id)
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user