Files
MonkeyCode/backend/db/taskusagestat_create.go
yokowuandClaude Opus 4.6 f7dd05aeb3 feat: 添加任务 token 用量统计数据层
新增 task_usage_stats 表及对应的 Ent Schema,实现 Stat/StatByIDs
repo 方法的真实数据库查询,替换原有的空实现。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 19:20:41 +08:00

948 lines
29 KiB
Go

// 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/taskusagestat"
"github.com/google/uuid"
)
// TaskUsageStatCreate is the builder for creating a TaskUsageStat entity.
type TaskUsageStatCreate struct {
config
mutation *TaskUsageStatMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetTaskID sets the "task_id" field.
func (_c *TaskUsageStatCreate) SetTaskID(v uuid.UUID) *TaskUsageStatCreate {
_c.mutation.SetTaskID(v)
return _c
}
// SetUserID sets the "user_id" field.
func (_c *TaskUsageStatCreate) SetUserID(v uuid.UUID) *TaskUsageStatCreate {
_c.mutation.SetUserID(v)
return _c
}
// SetModel sets the "model" field.
func (_c *TaskUsageStatCreate) SetModel(v string) *TaskUsageStatCreate {
_c.mutation.SetModel(v)
return _c
}
// SetNillableModel sets the "model" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableModel(v *string) *TaskUsageStatCreate {
if v != nil {
_c.SetModel(*v)
}
return _c
}
// SetInputTokens sets the "input_tokens" field.
func (_c *TaskUsageStatCreate) SetInputTokens(v int64) *TaskUsageStatCreate {
_c.mutation.SetInputTokens(v)
return _c
}
// SetNillableInputTokens sets the "input_tokens" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableInputTokens(v *int64) *TaskUsageStatCreate {
if v != nil {
_c.SetInputTokens(*v)
}
return _c
}
// SetOutputTokens sets the "output_tokens" field.
func (_c *TaskUsageStatCreate) SetOutputTokens(v int64) *TaskUsageStatCreate {
_c.mutation.SetOutputTokens(v)
return _c
}
// SetNillableOutputTokens sets the "output_tokens" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableOutputTokens(v *int64) *TaskUsageStatCreate {
if v != nil {
_c.SetOutputTokens(*v)
}
return _c
}
// SetTotalTokens sets the "total_tokens" field.
func (_c *TaskUsageStatCreate) SetTotalTokens(v int64) *TaskUsageStatCreate {
_c.mutation.SetTotalTokens(v)
return _c
}
// SetNillableTotalTokens sets the "total_tokens" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableTotalTokens(v *int64) *TaskUsageStatCreate {
if v != nil {
_c.SetTotalTokens(*v)
}
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *TaskUsageStatCreate) SetCreatedAt(v time.Time) *TaskUsageStatCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableCreatedAt(v *time.Time) *TaskUsageStatCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *TaskUsageStatCreate) SetID(v uuid.UUID) *TaskUsageStatCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *TaskUsageStatCreate) SetNillableID(v *uuid.UUID) *TaskUsageStatCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// Mutation returns the TaskUsageStatMutation object of the builder.
func (_c *TaskUsageStatCreate) Mutation() *TaskUsageStatMutation {
return _c.mutation
}
// Save creates the TaskUsageStat in the database.
func (_c *TaskUsageStatCreate) Save(ctx context.Context) (*TaskUsageStat, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *TaskUsageStatCreate) SaveX(ctx context.Context) *TaskUsageStat {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TaskUsageStatCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TaskUsageStatCreate) 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 *TaskUsageStatCreate) defaults() {
if _, ok := _c.mutation.Model(); !ok {
v := taskusagestat.DefaultModel
_c.mutation.SetModel(v)
}
if _, ok := _c.mutation.InputTokens(); !ok {
v := taskusagestat.DefaultInputTokens
_c.mutation.SetInputTokens(v)
}
if _, ok := _c.mutation.OutputTokens(); !ok {
v := taskusagestat.DefaultOutputTokens
_c.mutation.SetOutputTokens(v)
}
if _, ok := _c.mutation.TotalTokens(); !ok {
v := taskusagestat.DefaultTotalTokens
_c.mutation.SetTotalTokens(v)
}
if _, ok := _c.mutation.CreatedAt(); !ok {
v := taskusagestat.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := taskusagestat.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *TaskUsageStatCreate) check() error {
if _, ok := _c.mutation.TaskID(); !ok {
return &ValidationError{Name: "task_id", err: errors.New(`db: missing required field "TaskUsageStat.task_id"`)}
}
if _, ok := _c.mutation.UserID(); !ok {
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "TaskUsageStat.user_id"`)}
}
if _, ok := _c.mutation.Model(); !ok {
return &ValidationError{Name: "model", err: errors.New(`db: missing required field "TaskUsageStat.model"`)}
}
if _, ok := _c.mutation.InputTokens(); !ok {
return &ValidationError{Name: "input_tokens", err: errors.New(`db: missing required field "TaskUsageStat.input_tokens"`)}
}
if _, ok := _c.mutation.OutputTokens(); !ok {
return &ValidationError{Name: "output_tokens", err: errors.New(`db: missing required field "TaskUsageStat.output_tokens"`)}
}
if _, ok := _c.mutation.TotalTokens(); !ok {
return &ValidationError{Name: "total_tokens", err: errors.New(`db: missing required field "TaskUsageStat.total_tokens"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "TaskUsageStat.created_at"`)}
}
return nil
}
func (_c *TaskUsageStatCreate) sqlSave(ctx context.Context) (*TaskUsageStat, 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 *TaskUsageStatCreate) createSpec() (*TaskUsageStat, *sqlgraph.CreateSpec) {
var (
_node = &TaskUsageStat{config: _c.config}
_spec = sqlgraph.NewCreateSpec(taskusagestat.Table, sqlgraph.NewFieldSpec(taskusagestat.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.TaskID(); ok {
_spec.SetField(taskusagestat.FieldTaskID, field.TypeUUID, value)
_node.TaskID = value
}
if value, ok := _c.mutation.UserID(); ok {
_spec.SetField(taskusagestat.FieldUserID, field.TypeUUID, value)
_node.UserID = value
}
if value, ok := _c.mutation.Model(); ok {
_spec.SetField(taskusagestat.FieldModel, field.TypeString, value)
_node.Model = value
}
if value, ok := _c.mutation.InputTokens(); ok {
_spec.SetField(taskusagestat.FieldInputTokens, field.TypeInt64, value)
_node.InputTokens = value
}
if value, ok := _c.mutation.OutputTokens(); ok {
_spec.SetField(taskusagestat.FieldOutputTokens, field.TypeInt64, value)
_node.OutputTokens = value
}
if value, ok := _c.mutation.TotalTokens(); ok {
_spec.SetField(taskusagestat.FieldTotalTokens, field.TypeInt64, value)
_node.TotalTokens = value
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(taskusagestat.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.TaskUsageStat.Create().
// SetTaskID(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.TaskUsageStatUpsert) {
// SetTaskID(v+v).
// }).
// Exec(ctx)
func (_c *TaskUsageStatCreate) OnConflict(opts ...sql.ConflictOption) *TaskUsageStatUpsertOne {
_c.conflict = opts
return &TaskUsageStatUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TaskUsageStat.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TaskUsageStatCreate) OnConflictColumns(columns ...string) *TaskUsageStatUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TaskUsageStatUpsertOne{
create: _c,
}
}
type (
// TaskUsageStatUpsertOne is the builder for "upsert"-ing
// one TaskUsageStat node.
TaskUsageStatUpsertOne struct {
create *TaskUsageStatCreate
}
// TaskUsageStatUpsert is the "OnConflict" setter.
TaskUsageStatUpsert struct {
*sql.UpdateSet
}
)
// SetTaskID sets the "task_id" field.
func (u *TaskUsageStatUpsert) SetTaskID(v uuid.UUID) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldTaskID, v)
return u
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateTaskID() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldTaskID)
return u
}
// SetUserID sets the "user_id" field.
func (u *TaskUsageStatUpsert) SetUserID(v uuid.UUID) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldUserID, v)
return u
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateUserID() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldUserID)
return u
}
// SetModel sets the "model" field.
func (u *TaskUsageStatUpsert) SetModel(v string) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldModel, v)
return u
}
// UpdateModel sets the "model" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateModel() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldModel)
return u
}
// SetInputTokens sets the "input_tokens" field.
func (u *TaskUsageStatUpsert) SetInputTokens(v int64) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldInputTokens, v)
return u
}
// UpdateInputTokens sets the "input_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateInputTokens() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldInputTokens)
return u
}
// AddInputTokens adds v to the "input_tokens" field.
func (u *TaskUsageStatUpsert) AddInputTokens(v int64) *TaskUsageStatUpsert {
u.Add(taskusagestat.FieldInputTokens, v)
return u
}
// SetOutputTokens sets the "output_tokens" field.
func (u *TaskUsageStatUpsert) SetOutputTokens(v int64) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldOutputTokens, v)
return u
}
// UpdateOutputTokens sets the "output_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateOutputTokens() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldOutputTokens)
return u
}
// AddOutputTokens adds v to the "output_tokens" field.
func (u *TaskUsageStatUpsert) AddOutputTokens(v int64) *TaskUsageStatUpsert {
u.Add(taskusagestat.FieldOutputTokens, v)
return u
}
// SetTotalTokens sets the "total_tokens" field.
func (u *TaskUsageStatUpsert) SetTotalTokens(v int64) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldTotalTokens, v)
return u
}
// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateTotalTokens() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.FieldTotalTokens)
return u
}
// AddTotalTokens adds v to the "total_tokens" field.
func (u *TaskUsageStatUpsert) AddTotalTokens(v int64) *TaskUsageStatUpsert {
u.Add(taskusagestat.FieldTotalTokens, v)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUsageStatUpsert) SetCreatedAt(v time.Time) *TaskUsageStatUpsert {
u.Set(taskusagestat.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUsageStatUpsert) UpdateCreatedAt() *TaskUsageStatUpsert {
u.SetExcluded(taskusagestat.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.TaskUsageStat.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(taskusagestat.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TaskUsageStatUpsertOne) UpdateNewValues() *TaskUsageStatUpsertOne {
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(taskusagestat.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TaskUsageStat.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TaskUsageStatUpsertOne) Ignore() *TaskUsageStatUpsertOne {
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 *TaskUsageStatUpsertOne) DoNothing() *TaskUsageStatUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TaskUsageStatCreate.OnConflict
// documentation for more info.
func (u *TaskUsageStatUpsertOne) Update(set func(*TaskUsageStatUpsert)) *TaskUsageStatUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TaskUsageStatUpsert{UpdateSet: update})
}))
return u
}
// SetTaskID sets the "task_id" field.
func (u *TaskUsageStatUpsertOne) SetTaskID(v uuid.UUID) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetTaskID(v)
})
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateTaskID() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateTaskID()
})
}
// SetUserID sets the "user_id" field.
func (u *TaskUsageStatUpsertOne) SetUserID(v uuid.UUID) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetUserID(v)
})
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateUserID() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateUserID()
})
}
// SetModel sets the "model" field.
func (u *TaskUsageStatUpsertOne) SetModel(v string) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetModel(v)
})
}
// UpdateModel sets the "model" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateModel() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateModel()
})
}
// SetInputTokens sets the "input_tokens" field.
func (u *TaskUsageStatUpsertOne) SetInputTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetInputTokens(v)
})
}
// AddInputTokens adds v to the "input_tokens" field.
func (u *TaskUsageStatUpsertOne) AddInputTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddInputTokens(v)
})
}
// UpdateInputTokens sets the "input_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateInputTokens() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateInputTokens()
})
}
// SetOutputTokens sets the "output_tokens" field.
func (u *TaskUsageStatUpsertOne) SetOutputTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetOutputTokens(v)
})
}
// AddOutputTokens adds v to the "output_tokens" field.
func (u *TaskUsageStatUpsertOne) AddOutputTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddOutputTokens(v)
})
}
// UpdateOutputTokens sets the "output_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateOutputTokens() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateOutputTokens()
})
}
// SetTotalTokens sets the "total_tokens" field.
func (u *TaskUsageStatUpsertOne) SetTotalTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetTotalTokens(v)
})
}
// AddTotalTokens adds v to the "total_tokens" field.
func (u *TaskUsageStatUpsertOne) AddTotalTokens(v int64) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddTotalTokens(v)
})
}
// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateTotalTokens() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateTotalTokens()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUsageStatUpsertOne) SetCreatedAt(v time.Time) *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUsageStatUpsertOne) UpdateCreatedAt() *TaskUsageStatUpsertOne {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TaskUsageStatUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TaskUsageStatCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TaskUsageStatUpsertOne) 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 *TaskUsageStatUpsertOne) 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: TaskUsageStatUpsertOne.ID is not supported by MySQL driver. Use TaskUsageStatUpsertOne.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 *TaskUsageStatUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// TaskUsageStatCreateBulk is the builder for creating many TaskUsageStat entities in bulk.
type TaskUsageStatCreateBulk struct {
config
err error
builders []*TaskUsageStatCreate
conflict []sql.ConflictOption
}
// Save creates the TaskUsageStat entities in the database.
func (_c *TaskUsageStatCreateBulk) Save(ctx context.Context) ([]*TaskUsageStat, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*TaskUsageStat, 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.(*TaskUsageStatMutation)
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 *TaskUsageStatCreateBulk) SaveX(ctx context.Context) []*TaskUsageStat {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TaskUsageStatCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TaskUsageStatCreateBulk) 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.TaskUsageStat.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.TaskUsageStatUpsert) {
// SetTaskID(v+v).
// }).
// Exec(ctx)
func (_c *TaskUsageStatCreateBulk) OnConflict(opts ...sql.ConflictOption) *TaskUsageStatUpsertBulk {
_c.conflict = opts
return &TaskUsageStatUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TaskUsageStat.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TaskUsageStatCreateBulk) OnConflictColumns(columns ...string) *TaskUsageStatUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TaskUsageStatUpsertBulk{
create: _c,
}
}
// TaskUsageStatUpsertBulk is the builder for "upsert"-ing
// a bulk of TaskUsageStat nodes.
type TaskUsageStatUpsertBulk struct {
create *TaskUsageStatCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.TaskUsageStat.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(taskusagestat.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TaskUsageStatUpsertBulk) UpdateNewValues() *TaskUsageStatUpsertBulk {
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(taskusagestat.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TaskUsageStat.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TaskUsageStatUpsertBulk) Ignore() *TaskUsageStatUpsertBulk {
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 *TaskUsageStatUpsertBulk) DoNothing() *TaskUsageStatUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TaskUsageStatCreateBulk.OnConflict
// documentation for more info.
func (u *TaskUsageStatUpsertBulk) Update(set func(*TaskUsageStatUpsert)) *TaskUsageStatUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TaskUsageStatUpsert{UpdateSet: update})
}))
return u
}
// SetTaskID sets the "task_id" field.
func (u *TaskUsageStatUpsertBulk) SetTaskID(v uuid.UUID) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetTaskID(v)
})
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateTaskID() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateTaskID()
})
}
// SetUserID sets the "user_id" field.
func (u *TaskUsageStatUpsertBulk) SetUserID(v uuid.UUID) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetUserID(v)
})
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateUserID() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateUserID()
})
}
// SetModel sets the "model" field.
func (u *TaskUsageStatUpsertBulk) SetModel(v string) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetModel(v)
})
}
// UpdateModel sets the "model" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateModel() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateModel()
})
}
// SetInputTokens sets the "input_tokens" field.
func (u *TaskUsageStatUpsertBulk) SetInputTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetInputTokens(v)
})
}
// AddInputTokens adds v to the "input_tokens" field.
func (u *TaskUsageStatUpsertBulk) AddInputTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddInputTokens(v)
})
}
// UpdateInputTokens sets the "input_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateInputTokens() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateInputTokens()
})
}
// SetOutputTokens sets the "output_tokens" field.
func (u *TaskUsageStatUpsertBulk) SetOutputTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetOutputTokens(v)
})
}
// AddOutputTokens adds v to the "output_tokens" field.
func (u *TaskUsageStatUpsertBulk) AddOutputTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddOutputTokens(v)
})
}
// UpdateOutputTokens sets the "output_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateOutputTokens() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateOutputTokens()
})
}
// SetTotalTokens sets the "total_tokens" field.
func (u *TaskUsageStatUpsertBulk) SetTotalTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetTotalTokens(v)
})
}
// AddTotalTokens adds v to the "total_tokens" field.
func (u *TaskUsageStatUpsertBulk) AddTotalTokens(v int64) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.AddTotalTokens(v)
})
}
// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateTotalTokens() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateTotalTokens()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUsageStatUpsertBulk) SetCreatedAt(v time.Time) *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUsageStatUpsertBulk) UpdateCreatedAt() *TaskUsageStatUpsertBulk {
return u.Update(func(s *TaskUsageStatUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TaskUsageStatUpsertBulk) 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 TaskUsageStatCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TaskUsageStatCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TaskUsageStatUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}