// Copyright 2019-present Facebook Inc. 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 by ent, DO NOT EDIT. package versioned import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/multischema/versioned/friendship" "entgo.io/ent/entc/integration/multischema/versioned/group" "entgo.io/ent/entc/integration/multischema/versioned/pet" "entgo.io/ent/entc/integration/multischema/versioned/user" "entgo.io/ent/schema/field" ) // UserCreate is the builder for creating a User entity. type UserCreate struct { config mutation *UserMutation hooks []Hook } // SetName sets the "name" field. func (uc *UserCreate) SetName(s string) *UserCreate { uc.mutation.SetName(s) return uc } // SetNillableName sets the "name" field if the given value is not nil. func (uc *UserCreate) SetNillableName(s *string) *UserCreate { if s != nil { uc.SetName(*s) } return uc } // AddPetIDs adds the "pets" edge to the Pet entity by IDs. func (uc *UserCreate) AddPetIDs(ids ...int) *UserCreate { uc.mutation.AddPetIDs(ids...) return uc } // AddPets adds the "pets" edges to the Pet entity. func (uc *UserCreate) AddPets(p ...*Pet) *UserCreate { ids := make([]int, len(p)) for i := range p { ids[i] = p[i].ID } return uc.AddPetIDs(ids...) } // AddGroupIDs adds the "groups" edge to the Group entity by IDs. func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate { uc.mutation.AddGroupIDs(ids...) return uc } // AddGroups adds the "groups" edges to the Group entity. func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate { ids := make([]int, len(g)) for i := range g { ids[i] = g[i].ID } return uc.AddGroupIDs(ids...) } // AddFriendIDs adds the "friends" edge to the User entity by IDs. func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate { uc.mutation.AddFriendIDs(ids...) return uc } // AddFriends adds the "friends" edges to the User entity. func (uc *UserCreate) AddFriends(u ...*User) *UserCreate { ids := make([]int, len(u)) for i := range u { ids[i] = u[i].ID } return uc.AddFriendIDs(ids...) } // AddFollowerIDs adds the "followers" edge to the User entity by IDs. func (uc *UserCreate) AddFollowerIDs(ids ...int) *UserCreate { uc.mutation.AddFollowerIDs(ids...) return uc } // AddFollowers adds the "followers" edges to the User entity. func (uc *UserCreate) AddFollowers(u ...*User) *UserCreate { ids := make([]int, len(u)) for i := range u { ids[i] = u[i].ID } return uc.AddFollowerIDs(ids...) } // AddFollowingIDs adds the "following" edge to the User entity by IDs. func (uc *UserCreate) AddFollowingIDs(ids ...int) *UserCreate { uc.mutation.AddFollowingIDs(ids...) return uc } // AddFollowing adds the "following" edges to the User entity. func (uc *UserCreate) AddFollowing(u ...*User) *UserCreate { ids := make([]int, len(u)) for i := range u { ids[i] = u[i].ID } return uc.AddFollowingIDs(ids...) } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. func (uc *UserCreate) AddFriendshipIDs(ids ...int) *UserCreate { uc.mutation.AddFriendshipIDs(ids...) return uc } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uc *UserCreate) AddFriendships(f ...*Friendship) *UserCreate { ids := make([]int, len(f)) for i := range f { ids[i] = f[i].ID } return uc.AddFriendshipIDs(ids...) } // Mutation returns the UserMutation object of the builder. func (uc *UserCreate) Mutation() *UserMutation { return uc.mutation } // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { uc.defaults() return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks) } // 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 } // Exec executes the query. func (uc *UserCreate) Exec(ctx context.Context) error { _, err := uc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (uc *UserCreate) ExecX(ctx context.Context) { if err := uc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (uc *UserCreate) defaults() { if _, ok := uc.mutation.Name(); !ok { v := user.DefaultName uc.mutation.SetName(v) } } // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { if _, ok := uc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`versioned: missing required field "User.name"`)} } return nil } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err := uc.check(); err != nil { return nil, err } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) uc.mutation.id = &_node.ID uc.mutation.done = true return _node, nil } func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { var ( _node = &User{config: uc.config} _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) ) _spec.Schema = uc.schemaConfig.User if value, ok := uc.mutation.Name(); ok { _spec.SetField(user.FieldName, field.TypeString, value) _node.Name = value } if nodes := uc.mutation.PetsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.PetsTable, Columns: []string{user.PetsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(pet.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.Pet for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: user.GroupsTable, Columns: user.GroupsPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.GroupUsers for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.FriendsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: user.FriendsTable, Columns: user.FriendsPrimaryKey, Bidi: true, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.Friendship for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } createE := &FriendshipCreate{config: uc.config, mutation: newFriendshipMutation(uc.config, OpCreate)} createE.defaults() _, specE := createE.createSpec() edge.Target.Fields = specE.Fields _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.FollowersIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: user.FollowersTable, Columns: user.FollowersPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.UserFollowing for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.FollowingIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: user.FollowingTable, Columns: user.FollowingPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.UserFollowing for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.FriendshipsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: true, Table: user.FriendshipsTable, Columns: []string{user.FriendshipsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(friendship.FieldID, field.TypeInt), }, } edge.Schema = uc.schemaConfig.Friendship for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // UserCreateBulk is the builder for creating many User entities in bulk. type UserCreateBulk struct { config err error builders []*UserCreate } // Save creates the User entities in the database. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { if ucb.err != nil { return nil, ucb.err } specs := make([]*sqlgraph.CreateSpec, len(ucb.builders)) nodes := make([]*User, len(ucb.builders)) mutators := make([]Mutator, len(ucb.builders)) for i := range ucb.builders { func(i int, root context.Context) { builder := ucb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID if specs[i].ID.Value != nil { id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) } mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User { v, err := ucb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (ucb *UserCreateBulk) Exec(ctx context.Context) error { _, err := ucb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (ucb *UserCreateBulk) ExecX(ctx context.Context) { if err := ucb.Exec(ctx); err != nil { panic(err) } }