// 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" "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/ent/node" "entgo.io/ent/schema/field" ) // NodeCreate is the builder for creating a Node entity. type NodeCreate struct { config mutation *NodeMutation hooks []Hook conflict []sql.ConflictOption } // SetValue sets the "value" field. func (_c *NodeCreate) SetValue(v int) *NodeCreate { _c.mutation.SetValue(v) return _c } // SetNillableValue sets the "value" field if the given value is not nil. func (_c *NodeCreate) SetNillableValue(v *int) *NodeCreate { if v != nil { _c.SetValue(*v) } return _c } // SetUpdatedAt sets the "updated_at" field. func (_c *NodeCreate) SetUpdatedAt(v time.Time) *NodeCreate { _c.mutation.SetUpdatedAt(v) return _c } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (_c *NodeCreate) SetNillableUpdatedAt(v *time.Time) *NodeCreate { if v != nil { _c.SetUpdatedAt(*v) } return _c } // SetPrevID sets the "prev" edge to the Node entity by ID. func (_c *NodeCreate) SetPrevID(id int) *NodeCreate { _c.mutation.SetPrevID(id) return _c } // SetNillablePrevID sets the "prev" edge to the Node entity by ID if the given value is not nil. func (_c *NodeCreate) SetNillablePrevID(id *int) *NodeCreate { if id != nil { _c = _c.SetPrevID(*id) } return _c } // SetPrev sets the "prev" edge to the Node entity. func (_c *NodeCreate) SetPrev(v *Node) *NodeCreate { return _c.SetPrevID(v.ID) } // SetNextID sets the "next" edge to the Node entity by ID. func (_c *NodeCreate) SetNextID(id int) *NodeCreate { _c.mutation.SetNextID(id) return _c } // SetNillableNextID sets the "next" edge to the Node entity by ID if the given value is not nil. func (_c *NodeCreate) SetNillableNextID(id *int) *NodeCreate { if id != nil { _c = _c.SetNextID(*id) } return _c } // SetNext sets the "next" edge to the Node entity. func (_c *NodeCreate) SetNext(v *Node) *NodeCreate { return _c.SetNextID(v.ID) } // Mutation returns the NodeMutation object of the builder. func (_c *NodeCreate) Mutation() *NodeMutation { return _c.mutation } // Save creates the Node in the database. func (_c *NodeCreate) Save(ctx context.Context) (*Node, error) { return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *NodeCreate) SaveX(ctx context.Context) *Node { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *NodeCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *NodeCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (_c *NodeCreate) check() error { return nil } func (_c *NodeCreate) sqlSave(ctx context.Context) (*Node, 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 } id := _spec.ID.Value.(int64) _node.ID = int(id) _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) { var ( _node = &Node{config: _c.config} _spec = sqlgraph.NewCreateSpec(node.Table, sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt)) ) _spec.OnConflict = _c.conflict if value, ok := _c.mutation.Value(); ok { _spec.SetField(node.FieldValue, field.TypeInt, value) _node.Value = value } if value, ok := _c.mutation.UpdatedAt(); ok { _spec.SetField(node.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = &value } if nodes := _c.mutation.PrevIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: true, Table: node.PrevTable, Columns: []string{node.PrevColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.node_next = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.NextIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: false, Table: node.NextTable, Columns: []string{node.NextColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _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.Node.Create(). // SetValue(v). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.NodeUpsert) { // SetValue(v+v). // }). // Exec(ctx) func (_c *NodeCreate) OnConflict(opts ...sql.ConflictOption) *NodeUpsertOne { _c.conflict = opts return &NodeUpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Node.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *NodeCreate) OnConflictColumns(columns ...string) *NodeUpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &NodeUpsertOne{ create: _c, } } type ( // NodeUpsertOne is the builder for "upsert"-ing // one Node node. NodeUpsertOne struct { create *NodeCreate } // NodeUpsert is the "OnConflict" setter. NodeUpsert struct { *sql.UpdateSet } ) // SetValue sets the "value" field. func (u *NodeUpsert) SetValue(v int) *NodeUpsert { u.Set(node.FieldValue, v) return u } // UpdateValue sets the "value" field to the value that was provided on create. func (u *NodeUpsert) UpdateValue() *NodeUpsert { u.SetExcluded(node.FieldValue) return u } // AddValue adds v to the "value" field. func (u *NodeUpsert) AddValue(v int) *NodeUpsert { u.Add(node.FieldValue, v) return u } // ClearValue clears the value of the "value" field. func (u *NodeUpsert) ClearValue() *NodeUpsert { u.SetNull(node.FieldValue) return u } // SetUpdatedAt sets the "updated_at" field. func (u *NodeUpsert) SetUpdatedAt(v time.Time) *NodeUpsert { u.Set(node.FieldUpdatedAt, v) return u } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *NodeUpsert) UpdateUpdatedAt() *NodeUpsert { u.SetExcluded(node.FieldUpdatedAt) return u } // ClearUpdatedAt clears the value of the "updated_at" field. func (u *NodeUpsert) ClearUpdatedAt() *NodeUpsert { u.SetNull(node.FieldUpdatedAt) return u } // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // // client.Node.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *NodeUpsertOne) UpdateNewValues() *NodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Node.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *NodeUpsertOne) Ignore() *NodeUpsertOne { 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 *NodeUpsertOne) DoNothing() *NodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the NodeCreate.OnConflict // documentation for more info. func (u *NodeUpsertOne) Update(set func(*NodeUpsert)) *NodeUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&NodeUpsert{UpdateSet: update}) })) return u } // SetValue sets the "value" field. func (u *NodeUpsertOne) SetValue(v int) *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.SetValue(v) }) } // AddValue adds v to the "value" field. func (u *NodeUpsertOne) AddValue(v int) *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.AddValue(v) }) } // UpdateValue sets the "value" field to the value that was provided on create. func (u *NodeUpsertOne) UpdateValue() *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.UpdateValue() }) } // ClearValue clears the value of the "value" field. func (u *NodeUpsertOne) ClearValue() *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.ClearValue() }) } // SetUpdatedAt sets the "updated_at" field. func (u *NodeUpsertOne) SetUpdatedAt(v time.Time) *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *NodeUpsertOne) UpdateUpdatedAt() *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.UpdateUpdatedAt() }) } // ClearUpdatedAt clears the value of the "updated_at" field. func (u *NodeUpsertOne) ClearUpdatedAt() *NodeUpsertOne { return u.Update(func(s *NodeUpsert) { s.ClearUpdatedAt() }) } // Exec executes the query. func (u *NodeUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("ent: missing options for NodeCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *NodeUpsertOne) 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 *NodeUpsertOne) ID(ctx context.Context) (id int, err error) { 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 *NodeUpsertOne) IDX(ctx context.Context) int { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // NodeCreateBulk is the builder for creating many Node entities in bulk. type NodeCreateBulk struct { config err error builders []*NodeCreate conflict []sql.ConflictOption } // Save creates the Node entities in the database. func (_c *NodeCreateBulk) Save(ctx context.Context) ([]*Node, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Node, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*NodeMutation) 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 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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *NodeCreateBulk) SaveX(ctx context.Context) []*Node { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *NodeCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *NodeCreateBulk) 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.Node.CreateBulk(builders...). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.NodeUpsert) { // SetValue(v+v). // }). // Exec(ctx) func (_c *NodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *NodeUpsertBulk { _c.conflict = opts return &NodeUpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.Node.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *NodeCreateBulk) OnConflictColumns(columns ...string) *NodeUpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &NodeUpsertBulk{ create: _c, } } // NodeUpsertBulk is the builder for "upsert"-ing // a bulk of Node nodes. type NodeUpsertBulk struct { create *NodeCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.Node.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *NodeUpsertBulk) UpdateNewValues() *NodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.Node.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *NodeUpsertBulk) Ignore() *NodeUpsertBulk { 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 *NodeUpsertBulk) DoNothing() *NodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the NodeCreateBulk.OnConflict // documentation for more info. func (u *NodeUpsertBulk) Update(set func(*NodeUpsert)) *NodeUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&NodeUpsert{UpdateSet: update}) })) return u } // SetValue sets the "value" field. func (u *NodeUpsertBulk) SetValue(v int) *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.SetValue(v) }) } // AddValue adds v to the "value" field. func (u *NodeUpsertBulk) AddValue(v int) *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.AddValue(v) }) } // UpdateValue sets the "value" field to the value that was provided on create. func (u *NodeUpsertBulk) UpdateValue() *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.UpdateValue() }) } // ClearValue clears the value of the "value" field. func (u *NodeUpsertBulk) ClearValue() *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.ClearValue() }) } // SetUpdatedAt sets the "updated_at" field. func (u *NodeUpsertBulk) SetUpdatedAt(v time.Time) *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.SetUpdatedAt(v) }) } // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. func (u *NodeUpsertBulk) UpdateUpdatedAt() *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.UpdateUpdatedAt() }) } // ClearUpdatedAt clears the value of the "updated_at" field. func (u *NodeUpsertBulk) ClearUpdatedAt() *NodeUpsertBulk { return u.Update(func(s *NodeUpsert) { s.ClearUpdatedAt() }) } // Exec executes the query. func (u *NodeUpsertBulk) 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 NodeCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("ent: missing options for NodeCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *NodeUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }