// 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 ent import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/hooks/ent/card" "entgo.io/ent/entc/integration/hooks/ent/user" "entgo.io/ent/schema/field" ) // UserCreate is the builder for creating a User entity. type UserCreate struct { config mutation *UserMutation hooks []Hook } // SetVersion sets the "version" field. func (uc *UserCreate) SetVersion(i int) *UserCreate { uc.mutation.SetVersion(i) return uc } // SetNillableVersion sets the "version" field if the given value is not nil. func (uc *UserCreate) SetNillableVersion(i *int) *UserCreate { if i != nil { uc.SetVersion(*i) } return uc } // SetName sets the "name" field. func (uc *UserCreate) SetName(s string) *UserCreate { uc.mutation.SetName(s) return uc } // SetWorth sets the "worth" field. func (uc *UserCreate) SetWorth(u uint) *UserCreate { uc.mutation.SetWorth(u) return uc } // SetNillableWorth sets the "worth" field if the given value is not nil. func (uc *UserCreate) SetNillableWorth(u *uint) *UserCreate { if u != nil { uc.SetWorth(*u) } return uc } // SetPassword sets the "password" field. func (uc *UserCreate) SetPassword(s string) *UserCreate { uc.mutation.SetPassword(s) return uc } // SetNillablePassword sets the "password" field if the given value is not nil. func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate { if s != nil { uc.SetPassword(*s) } return uc } // AddCardIDs adds the "cards" edge to the Card entity by IDs. func (uc *UserCreate) AddCardIDs(ids ...int) *UserCreate { uc.mutation.AddCardIDs(ids...) return uc } // AddCards adds the "cards" edges to the Card entity. func (uc *UserCreate) AddCards(c ...*Card) *UserCreate { ids := make([]int, len(c)) for i := range c { ids[i] = c[i].ID } return uc.AddCardIDs(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...) } // SetBestFriendID sets the "best_friend" edge to the User entity by ID. func (uc *UserCreate) SetBestFriendID(id int) *UserCreate { uc.mutation.SetBestFriendID(id) return uc } // SetNillableBestFriendID sets the "best_friend" edge to the User entity by ID if the given value is not nil. func (uc *UserCreate) SetNillableBestFriendID(id *int) *UserCreate { if id != nil { uc = uc.SetBestFriendID(*id) } return uc } // SetBestFriend sets the "best_friend" edge to the User entity. func (uc *UserCreate) SetBestFriend(u *User) *UserCreate { return uc.SetBestFriendID(u.ID) } // 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) { var ( err error node *User ) if err := uc.defaults(); err != nil { return nil, err } if len(uc.hooks) == 0 { if err = uc.check(); err != nil { return nil, err } node, err = uc.sqlSave(ctx) } else { 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 = uc.check(); err != nil { return nil, err } uc.mutation = mutation if node, err = uc.sqlSave(ctx); err != nil { return nil, err } mutation.id = &node.ID mutation.done = true return node, err }) for i := len(uc.hooks) - 1; i >= 0; i-- { if uc.hooks[i] == nil { return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = uc.hooks[i](mut) } v, err := mut.Mutate(ctx, uc.mutation) if err != nil { return nil, err } nv, ok := v.(*User) if !ok { return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) } node = nv } return node, err } // 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() error { if _, ok := uc.mutation.Version(); !ok { v := user.DefaultVersion uc.mutation.SetVersion(v) } return nil } // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { if _, ok := uc.mutation.Version(); !ok { return &ValidationError{Name: "version", err: errors.New(`ent: missing required field "User.version"`)} } if _, ok := uc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)} } return nil } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { _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) return _node, nil } func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { var ( _node = &User{config: uc.config} _spec = &sqlgraph.CreateSpec{ Table: user.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: user.FieldID, }, } ) if value, ok := uc.mutation.Version(); ok { _spec.SetField(user.FieldVersion, field.TypeInt, value) _node.Version = value } if value, ok := uc.mutation.Name(); ok { _spec.SetField(user.FieldName, field.TypeString, value) _node.Name = value } if value, ok := uc.mutation.Worth(); ok { _spec.SetField(user.FieldWorth, field.TypeUint, value) _node.Worth = value } if value, ok := uc.mutation.Password(); ok { _spec.SetField(user.FieldPassword, field.TypeString, value) _node.Password = value } if nodes := uc.mutation.CardsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.CardsTable, Columns: []string{user.CardsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: card.FieldID, }, }, } 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.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 nodes := uc.mutation.BestFriendIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: false, Table: user.BestFriendTable, Columns: []string{user.BestFriendColumn}, Bidi: true, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: user.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.user_best_friend = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // UserCreateBulk is the builder for creating many User entities in bulk. type UserCreateBulk struct { config builders []*UserCreate } // Save creates the User entities in the database. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { 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 nodes[i], specs[i] = builder.createSpec() var err error 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) } }