mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/gen: move node creation to sqlgraph
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/facebookincubator/ent/dialect"
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
)
|
||||
|
||||
// Order applies an ordering on either graph traversal or sql selector.
|
||||
@@ -159,6 +160,9 @@ func isSQLConstraintError(err error) (*ErrConstraintFailed, bool) {
|
||||
"duplicate key value violates unique constraint", // PostgreSQL.
|
||||
}
|
||||
)
|
||||
if _, ok := err.(*sqlgraph.ConstraintError); ok {
|
||||
return &ErrConstraintFailed{msg, err}, true
|
||||
}
|
||||
for i := range errors {
|
||||
if strings.Contains(msg, errors[i]) {
|
||||
return &ErrConstraintFailed{msg, err}, true
|
||||
|
||||
@@ -10,8 +10,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/group"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/user"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// GroupCreate is the builder for creating a Group entity.
|
||||
@@ -66,39 +68,51 @@ func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
|
||||
|
||||
func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
|
||||
var (
|
||||
res sql.Result
|
||||
builder = sql.Dialect(gc.driver.Dialect())
|
||||
gr = &Group{config: gc.config}
|
||||
gr = &Group{config: gc.config}
|
||||
spec = &sqlgraph.CreateSpec{
|
||||
Table: group.Table,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: group.FieldID,
|
||||
},
|
||||
}
|
||||
)
|
||||
tx, err := gc.driver.Tx(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
insert := builder.Insert(group.Table).Default()
|
||||
if value := gc.name; value != nil {
|
||||
insert.Set(group.FieldName, *value)
|
||||
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
|
||||
Type: field.TypeString,
|
||||
Value: *value,
|
||||
Column: group.FieldName,
|
||||
})
|
||||
gr.Name = *value
|
||||
}
|
||||
|
||||
id, err := insertLastID(ctx, tx, insert.Returning(group.FieldID))
|
||||
if err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
}
|
||||
gr.ID = int(id)
|
||||
if len(gc.users) > 0 {
|
||||
for eid := range gc.users {
|
||||
|
||||
query, args := builder.Insert(group.UsersTable).
|
||||
Columns(group.UsersPrimaryKey[0], group.UsersPrimaryKey[1]).
|
||||
Values(id, eid).
|
||||
Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
}
|
||||
if nodes := gc.users; len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2M,
|
||||
Inverse: false,
|
||||
Table: group.UsersTable,
|
||||
Columns: group.UsersPrimaryKey,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: user.FieldID,
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, _ := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
spec.Edges = append(spec.Edges, edge)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
if err := sqlgraph.CreateNode(ctx, gc.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := spec.ID.Value.(int64)
|
||||
gr.ID = int(id)
|
||||
|
||||
return gr, nil
|
||||
}
|
||||
|
||||
@@ -10,8 +10,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/group"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/user"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserCreate is the builder for creating a User entity.
|
||||
@@ -76,43 +78,59 @@ 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)
|
||||
}
|
||||
u.ID = int(id)
|
||||
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 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,
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, _ := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
spec.Edges = append(spec.Edges, edge)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
if err := sqlgraph.CreateNode(ctx, uc.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := spec.ID.Value.(int64)
|
||||
u.ID = int(id)
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user