Files
sub2api/backend/ent/sorausagestat_update.go
yangjianbo 13262a5698 feat(sora): 新增 Sora 平台支持并修复高危安全和性能问题
新增功能:
- 新增 Sora 账号管理和 OAuth 认证
- 新增 Sora 视频/图片生成 API 网关
- 新增 Sora 任务调度和缓存机制
- 新增 Sora 使用统计和计费支持
- 前端增加 Sora 平台配置界面

安全修复(代码审核):
- [SEC-001] 限制媒体下载响应体大小(图片 20MB、视频 200MB),防止 DoS 攻击
- [SEC-002] 限制 SDK API 响应大小(1MB),防止内存耗尽
- [SEC-003] 修复 SSRF 风险,添加 URL 验证并强制使用代理配置

BUG 修复(代码审核):
- [BUG-001] 修复 for 循环内 defer 累积导致的资源泄漏
- [BUG-002] 修复图片并发槽位获取失败时已持有锁未释放的永久泄漏

性能优化(代码审核):
- [PERF-001] 添加 Sentinel Token 缓存(3 分钟有效期),减少 PoW 计算开销

技术细节:
- 使用 io.LimitReader 限制所有外部输入的大小
- 添加 urlvalidator 验证防止 SSRF 攻击
- 使用 sync.Map 实现线程安全的包级缓存
- 优化并发槽位管理,添加 releaseAll 模式防止泄漏

影响范围:
- 后端:新增 Sora 相关数据模型、服务、网关和管理接口
- 前端:新增 Sora 平台配置、账号管理和监控界面
- 配置:新增 Sora 相关配置项和环境变量

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-29 16:18:38 +08:00

749 lines
24 KiB
Go

// 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/schema/field"
"github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/sorausagestat"
)
// SoraUsageStatUpdate is the builder for updating SoraUsageStat entities.
type SoraUsageStatUpdate struct {
config
hooks []Hook
mutation *SoraUsageStatMutation
}
// Where appends a list predicates to the SoraUsageStatUpdate builder.
func (_u *SoraUsageStatUpdate) Where(ps ...predicate.SoraUsageStat) *SoraUsageStatUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetUpdatedAt sets the "updated_at" field.
func (_u *SoraUsageStatUpdate) SetUpdatedAt(v time.Time) *SoraUsageStatUpdate {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *SoraUsageStatUpdate) SetAccountID(v int64) *SoraUsageStatUpdate {
_u.mutation.ResetAccountID()
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableAccountID(v *int64) *SoraUsageStatUpdate {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// AddAccountID adds value to the "account_id" field.
func (_u *SoraUsageStatUpdate) AddAccountID(v int64) *SoraUsageStatUpdate {
_u.mutation.AddAccountID(v)
return _u
}
// SetImageCount sets the "image_count" field.
func (_u *SoraUsageStatUpdate) SetImageCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetImageCount()
_u.mutation.SetImageCount(v)
return _u
}
// SetNillableImageCount sets the "image_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableImageCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetImageCount(*v)
}
return _u
}
// AddImageCount adds value to the "image_count" field.
func (_u *SoraUsageStatUpdate) AddImageCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddImageCount(v)
return _u
}
// SetVideoCount sets the "video_count" field.
func (_u *SoraUsageStatUpdate) SetVideoCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetVideoCount()
_u.mutation.SetVideoCount(v)
return _u
}
// SetNillableVideoCount sets the "video_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableVideoCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetVideoCount(*v)
}
return _u
}
// AddVideoCount adds value to the "video_count" field.
func (_u *SoraUsageStatUpdate) AddVideoCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddVideoCount(v)
return _u
}
// SetErrorCount sets the "error_count" field.
func (_u *SoraUsageStatUpdate) SetErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetErrorCount()
_u.mutation.SetErrorCount(v)
return _u
}
// SetNillableErrorCount sets the "error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableErrorCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetErrorCount(*v)
}
return _u
}
// AddErrorCount adds value to the "error_count" field.
func (_u *SoraUsageStatUpdate) AddErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddErrorCount(v)
return _u
}
// SetLastErrorAt sets the "last_error_at" field.
func (_u *SoraUsageStatUpdate) SetLastErrorAt(v time.Time) *SoraUsageStatUpdate {
_u.mutation.SetLastErrorAt(v)
return _u
}
// SetNillableLastErrorAt sets the "last_error_at" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableLastErrorAt(v *time.Time) *SoraUsageStatUpdate {
if v != nil {
_u.SetLastErrorAt(*v)
}
return _u
}
// ClearLastErrorAt clears the value of the "last_error_at" field.
func (_u *SoraUsageStatUpdate) ClearLastErrorAt() *SoraUsageStatUpdate {
_u.mutation.ClearLastErrorAt()
return _u
}
// SetTodayImageCount sets the "today_image_count" field.
func (_u *SoraUsageStatUpdate) SetTodayImageCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetTodayImageCount()
_u.mutation.SetTodayImageCount(v)
return _u
}
// SetNillableTodayImageCount sets the "today_image_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableTodayImageCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetTodayImageCount(*v)
}
return _u
}
// AddTodayImageCount adds value to the "today_image_count" field.
func (_u *SoraUsageStatUpdate) AddTodayImageCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddTodayImageCount(v)
return _u
}
// SetTodayVideoCount sets the "today_video_count" field.
func (_u *SoraUsageStatUpdate) SetTodayVideoCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetTodayVideoCount()
_u.mutation.SetTodayVideoCount(v)
return _u
}
// SetNillableTodayVideoCount sets the "today_video_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableTodayVideoCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetTodayVideoCount(*v)
}
return _u
}
// AddTodayVideoCount adds value to the "today_video_count" field.
func (_u *SoraUsageStatUpdate) AddTodayVideoCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddTodayVideoCount(v)
return _u
}
// SetTodayErrorCount sets the "today_error_count" field.
func (_u *SoraUsageStatUpdate) SetTodayErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetTodayErrorCount()
_u.mutation.SetTodayErrorCount(v)
return _u
}
// SetNillableTodayErrorCount sets the "today_error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableTodayErrorCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetTodayErrorCount(*v)
}
return _u
}
// AddTodayErrorCount adds value to the "today_error_count" field.
func (_u *SoraUsageStatUpdate) AddTodayErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddTodayErrorCount(v)
return _u
}
// SetTodayDate sets the "today_date" field.
func (_u *SoraUsageStatUpdate) SetTodayDate(v time.Time) *SoraUsageStatUpdate {
_u.mutation.SetTodayDate(v)
return _u
}
// SetNillableTodayDate sets the "today_date" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableTodayDate(v *time.Time) *SoraUsageStatUpdate {
if v != nil {
_u.SetTodayDate(*v)
}
return _u
}
// ClearTodayDate clears the value of the "today_date" field.
func (_u *SoraUsageStatUpdate) ClearTodayDate() *SoraUsageStatUpdate {
_u.mutation.ClearTodayDate()
return _u
}
// SetConsecutiveErrorCount sets the "consecutive_error_count" field.
func (_u *SoraUsageStatUpdate) SetConsecutiveErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.ResetConsecutiveErrorCount()
_u.mutation.SetConsecutiveErrorCount(v)
return _u
}
// SetNillableConsecutiveErrorCount sets the "consecutive_error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdate) SetNillableConsecutiveErrorCount(v *int) *SoraUsageStatUpdate {
if v != nil {
_u.SetConsecutiveErrorCount(*v)
}
return _u
}
// AddConsecutiveErrorCount adds value to the "consecutive_error_count" field.
func (_u *SoraUsageStatUpdate) AddConsecutiveErrorCount(v int) *SoraUsageStatUpdate {
_u.mutation.AddConsecutiveErrorCount(v)
return _u
}
// Mutation returns the SoraUsageStatMutation object of the builder.
func (_u *SoraUsageStatUpdate) Mutation() *SoraUsageStatMutation {
return _u.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *SoraUsageStatUpdate) Save(ctx context.Context) (int, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *SoraUsageStatUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *SoraUsageStatUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *SoraUsageStatUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_u *SoraUsageStatUpdate) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := sorausagestat.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
func (_u *SoraUsageStatUpdate) sqlSave(ctx context.Context) (_node int, err error) {
_spec := sqlgraph.NewUpdateSpec(sorausagestat.Table, sorausagestat.Columns, sqlgraph.NewFieldSpec(sorausagestat.FieldID, field.TypeInt64))
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.UpdatedAt(); ok {
_spec.SetField(sorausagestat.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.AccountID(); ok {
_spec.SetField(sorausagestat.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedAccountID(); ok {
_spec.AddField(sorausagestat.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.ImageCount(); ok {
_spec.SetField(sorausagestat.FieldImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedImageCount(); ok {
_spec.AddField(sorausagestat.FieldImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.VideoCount(); ok {
_spec.SetField(sorausagestat.FieldVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedVideoCount(); ok {
_spec.AddField(sorausagestat.FieldVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.ErrorCount(); ok {
_spec.SetField(sorausagestat.FieldErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedErrorCount(); ok {
_spec.AddField(sorausagestat.FieldErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.LastErrorAt(); ok {
_spec.SetField(sorausagestat.FieldLastErrorAt, field.TypeTime, value)
}
if _u.mutation.LastErrorAtCleared() {
_spec.ClearField(sorausagestat.FieldLastErrorAt, field.TypeTime)
}
if value, ok := _u.mutation.TodayImageCount(); ok {
_spec.SetField(sorausagestat.FieldTodayImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayImageCount(); ok {
_spec.AddField(sorausagestat.FieldTodayImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayVideoCount(); ok {
_spec.SetField(sorausagestat.FieldTodayVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayVideoCount(); ok {
_spec.AddField(sorausagestat.FieldTodayVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayErrorCount(); ok {
_spec.SetField(sorausagestat.FieldTodayErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayErrorCount(); ok {
_spec.AddField(sorausagestat.FieldTodayErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayDate(); ok {
_spec.SetField(sorausagestat.FieldTodayDate, field.TypeTime, value)
}
if _u.mutation.TodayDateCleared() {
_spec.ClearField(sorausagestat.FieldTodayDate, field.TypeTime)
}
if value, ok := _u.mutation.ConsecutiveErrorCount(); ok {
_spec.SetField(sorausagestat.FieldConsecutiveErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedConsecutiveErrorCount(); ok {
_spec.AddField(sorausagestat.FieldConsecutiveErrorCount, field.TypeInt, value)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{sorausagestat.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// SoraUsageStatUpdateOne is the builder for updating a single SoraUsageStat entity.
type SoraUsageStatUpdateOne struct {
config
fields []string
hooks []Hook
mutation *SoraUsageStatMutation
}
// SetUpdatedAt sets the "updated_at" field.
func (_u *SoraUsageStatUpdateOne) SetUpdatedAt(v time.Time) *SoraUsageStatUpdateOne {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *SoraUsageStatUpdateOne) SetAccountID(v int64) *SoraUsageStatUpdateOne {
_u.mutation.ResetAccountID()
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableAccountID(v *int64) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// AddAccountID adds value to the "account_id" field.
func (_u *SoraUsageStatUpdateOne) AddAccountID(v int64) *SoraUsageStatUpdateOne {
_u.mutation.AddAccountID(v)
return _u
}
// SetImageCount sets the "image_count" field.
func (_u *SoraUsageStatUpdateOne) SetImageCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetImageCount()
_u.mutation.SetImageCount(v)
return _u
}
// SetNillableImageCount sets the "image_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableImageCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetImageCount(*v)
}
return _u
}
// AddImageCount adds value to the "image_count" field.
func (_u *SoraUsageStatUpdateOne) AddImageCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddImageCount(v)
return _u
}
// SetVideoCount sets the "video_count" field.
func (_u *SoraUsageStatUpdateOne) SetVideoCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetVideoCount()
_u.mutation.SetVideoCount(v)
return _u
}
// SetNillableVideoCount sets the "video_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableVideoCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetVideoCount(*v)
}
return _u
}
// AddVideoCount adds value to the "video_count" field.
func (_u *SoraUsageStatUpdateOne) AddVideoCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddVideoCount(v)
return _u
}
// SetErrorCount sets the "error_count" field.
func (_u *SoraUsageStatUpdateOne) SetErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetErrorCount()
_u.mutation.SetErrorCount(v)
return _u
}
// SetNillableErrorCount sets the "error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableErrorCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetErrorCount(*v)
}
return _u
}
// AddErrorCount adds value to the "error_count" field.
func (_u *SoraUsageStatUpdateOne) AddErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddErrorCount(v)
return _u
}
// SetLastErrorAt sets the "last_error_at" field.
func (_u *SoraUsageStatUpdateOne) SetLastErrorAt(v time.Time) *SoraUsageStatUpdateOne {
_u.mutation.SetLastErrorAt(v)
return _u
}
// SetNillableLastErrorAt sets the "last_error_at" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableLastErrorAt(v *time.Time) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetLastErrorAt(*v)
}
return _u
}
// ClearLastErrorAt clears the value of the "last_error_at" field.
func (_u *SoraUsageStatUpdateOne) ClearLastErrorAt() *SoraUsageStatUpdateOne {
_u.mutation.ClearLastErrorAt()
return _u
}
// SetTodayImageCount sets the "today_image_count" field.
func (_u *SoraUsageStatUpdateOne) SetTodayImageCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetTodayImageCount()
_u.mutation.SetTodayImageCount(v)
return _u
}
// SetNillableTodayImageCount sets the "today_image_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableTodayImageCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetTodayImageCount(*v)
}
return _u
}
// AddTodayImageCount adds value to the "today_image_count" field.
func (_u *SoraUsageStatUpdateOne) AddTodayImageCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddTodayImageCount(v)
return _u
}
// SetTodayVideoCount sets the "today_video_count" field.
func (_u *SoraUsageStatUpdateOne) SetTodayVideoCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetTodayVideoCount()
_u.mutation.SetTodayVideoCount(v)
return _u
}
// SetNillableTodayVideoCount sets the "today_video_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableTodayVideoCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetTodayVideoCount(*v)
}
return _u
}
// AddTodayVideoCount adds value to the "today_video_count" field.
func (_u *SoraUsageStatUpdateOne) AddTodayVideoCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddTodayVideoCount(v)
return _u
}
// SetTodayErrorCount sets the "today_error_count" field.
func (_u *SoraUsageStatUpdateOne) SetTodayErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetTodayErrorCount()
_u.mutation.SetTodayErrorCount(v)
return _u
}
// SetNillableTodayErrorCount sets the "today_error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableTodayErrorCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetTodayErrorCount(*v)
}
return _u
}
// AddTodayErrorCount adds value to the "today_error_count" field.
func (_u *SoraUsageStatUpdateOne) AddTodayErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddTodayErrorCount(v)
return _u
}
// SetTodayDate sets the "today_date" field.
func (_u *SoraUsageStatUpdateOne) SetTodayDate(v time.Time) *SoraUsageStatUpdateOne {
_u.mutation.SetTodayDate(v)
return _u
}
// SetNillableTodayDate sets the "today_date" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableTodayDate(v *time.Time) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetTodayDate(*v)
}
return _u
}
// ClearTodayDate clears the value of the "today_date" field.
func (_u *SoraUsageStatUpdateOne) ClearTodayDate() *SoraUsageStatUpdateOne {
_u.mutation.ClearTodayDate()
return _u
}
// SetConsecutiveErrorCount sets the "consecutive_error_count" field.
func (_u *SoraUsageStatUpdateOne) SetConsecutiveErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.ResetConsecutiveErrorCount()
_u.mutation.SetConsecutiveErrorCount(v)
return _u
}
// SetNillableConsecutiveErrorCount sets the "consecutive_error_count" field if the given value is not nil.
func (_u *SoraUsageStatUpdateOne) SetNillableConsecutiveErrorCount(v *int) *SoraUsageStatUpdateOne {
if v != nil {
_u.SetConsecutiveErrorCount(*v)
}
return _u
}
// AddConsecutiveErrorCount adds value to the "consecutive_error_count" field.
func (_u *SoraUsageStatUpdateOne) AddConsecutiveErrorCount(v int) *SoraUsageStatUpdateOne {
_u.mutation.AddConsecutiveErrorCount(v)
return _u
}
// Mutation returns the SoraUsageStatMutation object of the builder.
func (_u *SoraUsageStatUpdateOne) Mutation() *SoraUsageStatMutation {
return _u.mutation
}
// Where appends a list predicates to the SoraUsageStatUpdate builder.
func (_u *SoraUsageStatUpdateOne) Where(ps ...predicate.SoraUsageStat) *SoraUsageStatUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *SoraUsageStatUpdateOne) Select(field string, fields ...string) *SoraUsageStatUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated SoraUsageStat entity.
func (_u *SoraUsageStatUpdateOne) Save(ctx context.Context) (*SoraUsageStat, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *SoraUsageStatUpdateOne) SaveX(ctx context.Context) *SoraUsageStat {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *SoraUsageStatUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *SoraUsageStatUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_u *SoraUsageStatUpdateOne) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := sorausagestat.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
func (_u *SoraUsageStatUpdateOne) sqlSave(ctx context.Context) (_node *SoraUsageStat, err error) {
_spec := sqlgraph.NewUpdateSpec(sorausagestat.Table, sorausagestat.Columns, sqlgraph.NewFieldSpec(sorausagestat.FieldID, field.TypeInt64))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SoraUsageStat.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, sorausagestat.FieldID)
for _, f := range fields {
if !sorausagestat.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != sorausagestat.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.UpdatedAt(); ok {
_spec.SetField(sorausagestat.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.AccountID(); ok {
_spec.SetField(sorausagestat.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedAccountID(); ok {
_spec.AddField(sorausagestat.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.ImageCount(); ok {
_spec.SetField(sorausagestat.FieldImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedImageCount(); ok {
_spec.AddField(sorausagestat.FieldImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.VideoCount(); ok {
_spec.SetField(sorausagestat.FieldVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedVideoCount(); ok {
_spec.AddField(sorausagestat.FieldVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.ErrorCount(); ok {
_spec.SetField(sorausagestat.FieldErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedErrorCount(); ok {
_spec.AddField(sorausagestat.FieldErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.LastErrorAt(); ok {
_spec.SetField(sorausagestat.FieldLastErrorAt, field.TypeTime, value)
}
if _u.mutation.LastErrorAtCleared() {
_spec.ClearField(sorausagestat.FieldLastErrorAt, field.TypeTime)
}
if value, ok := _u.mutation.TodayImageCount(); ok {
_spec.SetField(sorausagestat.FieldTodayImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayImageCount(); ok {
_spec.AddField(sorausagestat.FieldTodayImageCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayVideoCount(); ok {
_spec.SetField(sorausagestat.FieldTodayVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayVideoCount(); ok {
_spec.AddField(sorausagestat.FieldTodayVideoCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayErrorCount(); ok {
_spec.SetField(sorausagestat.FieldTodayErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedTodayErrorCount(); ok {
_spec.AddField(sorausagestat.FieldTodayErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.TodayDate(); ok {
_spec.SetField(sorausagestat.FieldTodayDate, field.TypeTime, value)
}
if _u.mutation.TodayDateCleared() {
_spec.ClearField(sorausagestat.FieldTodayDate, field.TypeTime)
}
if value, ok := _u.mutation.ConsecutiveErrorCount(); ok {
_spec.SetField(sorausagestat.FieldConsecutiveErrorCount, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedConsecutiveErrorCount(); ok {
_spec.AddField(sorausagestat.FieldConsecutiveErrorCount, field.TypeInt, value)
}
_node = &SoraUsageStat{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{sorausagestat.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}