// 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" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/customid/ent/car" "entgo.io/ent/entc/integration/customid/ent/pet" "entgo.io/ent/entc/integration/customid/ent/user" "entgo.io/ent/schema/field" ) // PetCreate is the builder for creating a Pet entity. type PetCreate struct { config mutation *PetMutation hooks []Hook conflict []sql.ConflictOption } // SetID sets the "id" field. func (_c *PetCreate) SetID(v string) *PetCreate { _c.mutation.SetID(v) return _c } // SetNillableID sets the "id" field if the given value is not nil. func (_c *PetCreate) SetNillableID(v *string) *PetCreate { if v != nil { _c.SetID(*v) } return _c } // SetOwnerID sets the "owner" edge to the User entity by ID. func (_c *PetCreate) SetOwnerID(id int) *PetCreate { _c.mutation.SetOwnerID(id) return _c } // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil. func (_c *PetCreate) SetNillableOwnerID(id *int) *PetCreate { if id != nil { _c = _c.SetOwnerID(*id) } return _c } // SetOwner sets the "owner" edge to the User entity. func (_c *PetCreate) SetOwner(v *User) *PetCreate { return _c.SetOwnerID(v.ID) } // AddCarIDs adds the "cars" edge to the Car entity by IDs. func (_c *PetCreate) AddCarIDs(ids ...int) *PetCreate { _c.mutation.AddCarIDs(ids...) return _c } // AddCars adds the "cars" edges to the Car entity. func (_c *PetCreate) AddCars(v ...*Car) *PetCreate { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddCarIDs(ids...) } // AddFriendIDs adds the "friends" edge to the Pet entity by IDs. func (_c *PetCreate) AddFriendIDs(ids ...string) *PetCreate { _c.mutation.AddFriendIDs(ids...) return _c } // AddFriends adds the "friends" edges to the Pet entity. func (_c *PetCreate) AddFriends(v ...*Pet) *PetCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddFriendIDs(ids...) } // SetBestFriendID sets the "best_friend" edge to the Pet entity by ID. func (_c *PetCreate) SetBestFriendID(id string) *PetCreate { _c.mutation.SetBestFriendID(id) return _c } // SetNillableBestFriendID sets the "best_friend" edge to the Pet entity by ID if the given value is not nil. func (_c *PetCreate) SetNillableBestFriendID(id *string) *PetCreate { if id != nil { _c = _c.SetBestFriendID(*id) } return _c } // SetBestFriend sets the "best_friend" edge to the Pet entity. func (_c *PetCreate) SetBestFriend(v *Pet) *PetCreate { return _c.SetBestFriendID(v.ID) } // Mutation returns the PetMutation object of the builder. func (_c *PetCreate) Mutation() *PetMutation { return _c.mutation } // Save creates the Pet in the database. func (_c *PetCreate) Save(ctx context.Context) (*Pet, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *PetCreate) SaveX(ctx context.Context) *Pet { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *PetCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *PetCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *PetCreate) defaults() { if _, ok := _c.mutation.ID(); !ok { v := pet.DefaultID() _c.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (_c *PetCreate) check() error { if v, ok := _c.mutation.ID(); ok { if err := pet.IDValidator(v); err != nil { return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Pet.id": %w`, err)} } } return nil } func (_c *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != nil { if id, ok := _spec.ID.Value.(string); ok { _node.ID = id } else { return nil, fmt.Errorf("unexpected Pet.ID type: %T", _spec.ID.Value) } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *PetCreate) createSpec() (*Pet, *sqlgraph.CreateSpec) { var ( _node = &Pet{config: _c.config} _spec = sqlgraph.NewCreateSpec(pet.Table, sqlgraph.NewFieldSpec(pet.FieldID, field.TypeString)) ) _spec.OnConflict = _c.conflict if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if nodes := _c.mutation.OwnerIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: pet.OwnerTable, Columns: []string{pet.OwnerColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.user_pets = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.CarsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: pet.CarsTable, Columns: []string{pet.CarsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(car.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.FriendsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: pet.FriendsTable, Columns: pet.FriendsPrimaryKey, Bidi: true, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(pet.FieldID, field.TypeString), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.BestFriendIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: false, Table: pet.BestFriendTable, Columns: []string{pet.BestFriendColumn}, Bidi: true, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(pet.FieldID, field.TypeString), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.pet_best_friend = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.Pet.Create(). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (_c *PetCreate) OnConflict(opts ...sql.ConflictOption) *PetUpsertOne { _c.conflict = opts return &PetUpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *PetCreate) OnConflictColumns(columns ...string) *PetUpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &PetUpsertOne{ create: _c, } } type ( // PetUpsertOne is the builder for "upsert"-ing // one Pet node. PetUpsertOne struct { create *PetCreate } // PetUpsert is the "OnConflict" setter. PetUpsert struct { *sql.UpdateSet } ) // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field. // Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(pet.FieldID) // }), // ). // Exec(ctx) func (u *PetUpsertOne) UpdateNewValues() *PetUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { if _, exists := u.create.mutation.ID(); exists { s.SetIgnore(pet.FieldID) } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *PetUpsertOne) Ignore() *PetUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *PetUpsertOne) DoNothing() *PetUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the PetCreate.OnConflict // documentation for more info. func (u *PetUpsertOne) Update(set func(*PetUpsert)) *PetUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&PetUpsert{UpdateSet: update}) })) return u } // Exec executes the query. func (u *PetUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("ent: missing options for PetCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *PetUpsertOne) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } } // Exec executes the UPSERT query and returns the inserted/updated ID. func (u *PetUpsertOne) ID(ctx context.Context) (id string, err error) { if u.create.driver.Dialect() == dialect.MySQL { // In case of "ON CONFLICT", there is no way to get back non-numeric ID // fields from the database since MySQL does not support the RETURNING clause. return id, errors.New("ent: PetUpsertOne.ID is not supported by MySQL driver. Use PetUpsertOne.Exec instead") } node, err := u.create.Save(ctx) if err != nil { return id, err } return node.ID, nil } // IDX is like ID, but panics if an error occurs. func (u *PetUpsertOne) IDX(ctx context.Context) string { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // PetCreateBulk is the builder for creating many Pet entities in bulk. type PetCreateBulk struct { config err error builders []*PetCreate conflict []sql.ConflictOption } // Save creates the Pet entities in the database. func (_c *PetCreateBulk) Save(ctx context.Context) ([]*Pet, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Pet, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*PetMutation) 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, _c.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} spec.OnConflict = _c.conflict // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, _c.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 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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *PetCreateBulk) SaveX(ctx context.Context) []*Pet { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *PetCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *PetCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.Pet.CreateBulk(builders...). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (_c *PetCreateBulk) OnConflict(opts ...sql.ConflictOption) *PetUpsertBulk { _c.conflict = opts return &PetUpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *PetCreateBulk) OnConflictColumns(columns ...string) *PetUpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &PetUpsertBulk{ create: _c, } } // PetUpsertBulk is the builder for "upsert"-ing // a bulk of Pet nodes. type PetUpsertBulk struct { create *PetCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(pet.FieldID) // }), // ). // Exec(ctx) func (u *PetUpsertBulk) UpdateNewValues() *PetUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { for _, b := range u.create.builders { if _, exists := b.mutation.ID(); exists { s.SetIgnore(pet.FieldID) } } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Pet.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *PetUpsertBulk) Ignore() *PetUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *PetUpsertBulk) DoNothing() *PetUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the PetCreateBulk.OnConflict // documentation for more info. func (u *PetUpsertBulk) Update(set func(*PetUpsert)) *PetUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&PetUpsert{UpdateSet: update}) })) return u } // Exec executes the query. func (u *PetUpsertBulk) Exec(ctx context.Context) error { if u.create.err != nil { return u.create.err } for i, b := range u.create.builders { if len(b.conflict) != 0 { return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PetCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("ent: missing options for PetCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *PetUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }