// Code generated by ent, DO NOT EDIT. package db import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/chaitin/MonkeyCode/backend/db/agentskill" "github.com/chaitin/MonkeyCode/backend/db/agentskillgroupbinding" "github.com/chaitin/MonkeyCode/backend/db/teamgroup" "github.com/google/uuid" ) // AgentSkillGroupBindingCreate is the builder for creating a AgentSkillGroupBinding entity. type AgentSkillGroupBindingCreate struct { config mutation *AgentSkillGroupBindingMutation hooks []Hook conflict []sql.ConflictOption } // SetSkillID sets the "skill_id" field. func (_c *AgentSkillGroupBindingCreate) SetSkillID(v uuid.UUID) *AgentSkillGroupBindingCreate { _c.mutation.SetSkillID(v) return _c } // SetGroupID sets the "group_id" field. func (_c *AgentSkillGroupBindingCreate) SetGroupID(v uuid.UUID) *AgentSkillGroupBindingCreate { _c.mutation.SetGroupID(v) return _c } // SetCreatedAt sets the "created_at" field. func (_c *AgentSkillGroupBindingCreate) SetCreatedAt(v time.Time) *AgentSkillGroupBindingCreate { _c.mutation.SetCreatedAt(v) return _c } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (_c *AgentSkillGroupBindingCreate) SetNillableCreatedAt(v *time.Time) *AgentSkillGroupBindingCreate { if v != nil { _c.SetCreatedAt(*v) } return _c } // SetID sets the "id" field. func (_c *AgentSkillGroupBindingCreate) SetID(v uuid.UUID) *AgentSkillGroupBindingCreate { _c.mutation.SetID(v) return _c } // SetNillableID sets the "id" field if the given value is not nil. func (_c *AgentSkillGroupBindingCreate) SetNillableID(v *uuid.UUID) *AgentSkillGroupBindingCreate { if v != nil { _c.SetID(*v) } return _c } // SetSkill sets the "skill" edge to the AgentSkill entity. func (_c *AgentSkillGroupBindingCreate) SetSkill(v *AgentSkill) *AgentSkillGroupBindingCreate { return _c.SetSkillID(v.ID) } // SetGroup sets the "group" edge to the TeamGroup entity. func (_c *AgentSkillGroupBindingCreate) SetGroup(v *TeamGroup) *AgentSkillGroupBindingCreate { return _c.SetGroupID(v.ID) } // Mutation returns the AgentSkillGroupBindingMutation object of the builder. func (_c *AgentSkillGroupBindingCreate) Mutation() *AgentSkillGroupBindingMutation { return _c.mutation } // Save creates the AgentSkillGroupBinding in the database. func (_c *AgentSkillGroupBindingCreate) Save(ctx context.Context) (*AgentSkillGroupBinding, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *AgentSkillGroupBindingCreate) SaveX(ctx context.Context) *AgentSkillGroupBinding { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AgentSkillGroupBindingCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AgentSkillGroupBindingCreate) 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 *AgentSkillGroupBindingCreate) defaults() { if _, ok := _c.mutation.CreatedAt(); !ok { v := agentskillgroupbinding.DefaultCreatedAt() _c.mutation.SetCreatedAt(v) } if _, ok := _c.mutation.ID(); !ok { v := agentskillgroupbinding.DefaultID() _c.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (_c *AgentSkillGroupBindingCreate) check() error { if _, ok := _c.mutation.SkillID(); !ok { return &ValidationError{Name: "skill_id", err: errors.New(`db: missing required field "AgentSkillGroupBinding.skill_id"`)} } if _, ok := _c.mutation.GroupID(); !ok { return &ValidationError{Name: "group_id", err: errors.New(`db: missing required field "AgentSkillGroupBinding.group_id"`)} } if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "AgentSkillGroupBinding.created_at"`)} } if len(_c.mutation.SkillIDs()) == 0 { return &ValidationError{Name: "skill", err: errors.New(`db: missing required edge "AgentSkillGroupBinding.skill"`)} } if len(_c.mutation.GroupIDs()) == 0 { return &ValidationError{Name: "group", err: errors.New(`db: missing required edge "AgentSkillGroupBinding.group"`)} } return nil } func (_c *AgentSkillGroupBindingCreate) sqlSave(ctx context.Context) (*AgentSkillGroupBinding, 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.(*uuid.UUID); ok { _node.ID = *id } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { return nil, err } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *AgentSkillGroupBindingCreate) createSpec() (*AgentSkillGroupBinding, *sqlgraph.CreateSpec) { var ( _node = &AgentSkillGroupBinding{config: _c.config} _spec = sqlgraph.NewCreateSpec(agentskillgroupbinding.Table, sqlgraph.NewFieldSpec(agentskillgroupbinding.FieldID, field.TypeUUID)) ) _spec.OnConflict = _c.conflict if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = &id } if value, ok := _c.mutation.CreatedAt(); ok { _spec.SetField(agentskillgroupbinding.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if nodes := _c.mutation.SkillIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: agentskillgroupbinding.SkillTable, Columns: []string{agentskillgroupbinding.SkillColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(agentskill.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.SkillID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: agentskillgroupbinding.GroupTable, Columns: []string{agentskillgroupbinding.GroupColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(teamgroup.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.GroupID = 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.AgentSkillGroupBinding.Create(). // SetSkillID(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.AgentSkillGroupBindingUpsert) { // SetSkillID(v+v). // }). // Exec(ctx) func (_c *AgentSkillGroupBindingCreate) OnConflict(opts ...sql.ConflictOption) *AgentSkillGroupBindingUpsertOne { _c.conflict = opts return &AgentSkillGroupBindingUpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.AgentSkillGroupBinding.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *AgentSkillGroupBindingCreate) OnConflictColumns(columns ...string) *AgentSkillGroupBindingUpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &AgentSkillGroupBindingUpsertOne{ create: _c, } } type ( // AgentSkillGroupBindingUpsertOne is the builder for "upsert"-ing // one AgentSkillGroupBinding node. AgentSkillGroupBindingUpsertOne struct { create *AgentSkillGroupBindingCreate } // AgentSkillGroupBindingUpsert is the "OnConflict" setter. AgentSkillGroupBindingUpsert struct { *sql.UpdateSet } ) // SetSkillID sets the "skill_id" field. func (u *AgentSkillGroupBindingUpsert) SetSkillID(v uuid.UUID) *AgentSkillGroupBindingUpsert { u.Set(agentskillgroupbinding.FieldSkillID, v) return u } // UpdateSkillID sets the "skill_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsert) UpdateSkillID() *AgentSkillGroupBindingUpsert { u.SetExcluded(agentskillgroupbinding.FieldSkillID) return u } // SetGroupID sets the "group_id" field. func (u *AgentSkillGroupBindingUpsert) SetGroupID(v uuid.UUID) *AgentSkillGroupBindingUpsert { u.Set(agentskillgroupbinding.FieldGroupID, v) return u } // UpdateGroupID sets the "group_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsert) UpdateGroupID() *AgentSkillGroupBindingUpsert { u.SetExcluded(agentskillgroupbinding.FieldGroupID) return u } // SetCreatedAt sets the "created_at" field. func (u *AgentSkillGroupBindingUpsert) SetCreatedAt(v time.Time) *AgentSkillGroupBindingUpsert { u.Set(agentskillgroupbinding.FieldCreatedAt, v) return u } // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsert) UpdateCreatedAt() *AgentSkillGroupBindingUpsert { u.SetExcluded(agentskillgroupbinding.FieldCreatedAt) return u } // 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.AgentSkillGroupBinding.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(agentskillgroupbinding.FieldID) // }), // ). // Exec(ctx) func (u *AgentSkillGroupBindingUpsertOne) UpdateNewValues() *AgentSkillGroupBindingUpsertOne { 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(agentskillgroupbinding.FieldID) } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.AgentSkillGroupBinding.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *AgentSkillGroupBindingUpsertOne) Ignore() *AgentSkillGroupBindingUpsertOne { 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 *AgentSkillGroupBindingUpsertOne) DoNothing() *AgentSkillGroupBindingUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the AgentSkillGroupBindingCreate.OnConflict // documentation for more info. func (u *AgentSkillGroupBindingUpsertOne) Update(set func(*AgentSkillGroupBindingUpsert)) *AgentSkillGroupBindingUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&AgentSkillGroupBindingUpsert{UpdateSet: update}) })) return u } // SetSkillID sets the "skill_id" field. func (u *AgentSkillGroupBindingUpsertOne) SetSkillID(v uuid.UUID) *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetSkillID(v) }) } // UpdateSkillID sets the "skill_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertOne) UpdateSkillID() *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateSkillID() }) } // SetGroupID sets the "group_id" field. func (u *AgentSkillGroupBindingUpsertOne) SetGroupID(v uuid.UUID) *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetGroupID(v) }) } // UpdateGroupID sets the "group_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertOne) UpdateGroupID() *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateGroupID() }) } // SetCreatedAt sets the "created_at" field. func (u *AgentSkillGroupBindingUpsertOne) SetCreatedAt(v time.Time) *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetCreatedAt(v) }) } // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertOne) UpdateCreatedAt() *AgentSkillGroupBindingUpsertOne { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateCreatedAt() }) } // Exec executes the query. func (u *AgentSkillGroupBindingUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("db: missing options for AgentSkillGroupBindingCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *AgentSkillGroupBindingUpsertOne) 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 *AgentSkillGroupBindingUpsertOne) ID(ctx context.Context) (id uuid.UUID, 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("db: AgentSkillGroupBindingUpsertOne.ID is not supported by MySQL driver. Use AgentSkillGroupBindingUpsertOne.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 *AgentSkillGroupBindingUpsertOne) IDX(ctx context.Context) uuid.UUID { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // AgentSkillGroupBindingCreateBulk is the builder for creating many AgentSkillGroupBinding entities in bulk. type AgentSkillGroupBindingCreateBulk struct { config err error builders []*AgentSkillGroupBindingCreate conflict []sql.ConflictOption } // Save creates the AgentSkillGroupBinding entities in the database. func (_c *AgentSkillGroupBindingCreateBulk) Save(ctx context.Context) ([]*AgentSkillGroupBinding, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*AgentSkillGroupBinding, 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.(*AgentSkillGroupBindingMutation) 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 *AgentSkillGroupBindingCreateBulk) SaveX(ctx context.Context) []*AgentSkillGroupBinding { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AgentSkillGroupBindingCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AgentSkillGroupBindingCreateBulk) 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.AgentSkillGroupBinding.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.AgentSkillGroupBindingUpsert) { // SetSkillID(v+v). // }). // Exec(ctx) func (_c *AgentSkillGroupBindingCreateBulk) OnConflict(opts ...sql.ConflictOption) *AgentSkillGroupBindingUpsertBulk { _c.conflict = opts return &AgentSkillGroupBindingUpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.AgentSkillGroupBinding.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *AgentSkillGroupBindingCreateBulk) OnConflictColumns(columns ...string) *AgentSkillGroupBindingUpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &AgentSkillGroupBindingUpsertBulk{ create: _c, } } // AgentSkillGroupBindingUpsertBulk is the builder for "upsert"-ing // a bulk of AgentSkillGroupBinding nodes. type AgentSkillGroupBindingUpsertBulk struct { create *AgentSkillGroupBindingCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.AgentSkillGroupBinding.Create(). // OnConflict( // sql.ResolveWithNewValues(), // sql.ResolveWith(func(u *sql.UpdateSet) { // u.SetIgnore(agentskillgroupbinding.FieldID) // }), // ). // Exec(ctx) func (u *AgentSkillGroupBindingUpsertBulk) UpdateNewValues() *AgentSkillGroupBindingUpsertBulk { 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(agentskillgroupbinding.FieldID) } } })) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.AgentSkillGroupBinding.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *AgentSkillGroupBindingUpsertBulk) Ignore() *AgentSkillGroupBindingUpsertBulk { 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 *AgentSkillGroupBindingUpsertBulk) DoNothing() *AgentSkillGroupBindingUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the AgentSkillGroupBindingCreateBulk.OnConflict // documentation for more info. func (u *AgentSkillGroupBindingUpsertBulk) Update(set func(*AgentSkillGroupBindingUpsert)) *AgentSkillGroupBindingUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&AgentSkillGroupBindingUpsert{UpdateSet: update}) })) return u } // SetSkillID sets the "skill_id" field. func (u *AgentSkillGroupBindingUpsertBulk) SetSkillID(v uuid.UUID) *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetSkillID(v) }) } // UpdateSkillID sets the "skill_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertBulk) UpdateSkillID() *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateSkillID() }) } // SetGroupID sets the "group_id" field. func (u *AgentSkillGroupBindingUpsertBulk) SetGroupID(v uuid.UUID) *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetGroupID(v) }) } // UpdateGroupID sets the "group_id" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertBulk) UpdateGroupID() *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateGroupID() }) } // SetCreatedAt sets the "created_at" field. func (u *AgentSkillGroupBindingUpsertBulk) SetCreatedAt(v time.Time) *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.SetCreatedAt(v) }) } // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. func (u *AgentSkillGroupBindingUpsertBulk) UpdateCreatedAt() *AgentSkillGroupBindingUpsertBulk { return u.Update(func(s *AgentSkillGroupBindingUpsert) { s.UpdateCreatedAt() }) } // Exec executes the query. func (u *AgentSkillGroupBindingUpsertBulk) 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("db: OnConflict was set for builder %d. Set it on the AgentSkillGroupBindingCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("db: missing options for AgentSkillGroupBindingCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *AgentSkillGroupBindingUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }