// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package dao

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"github.com/bitmagnet-io/bitmagnet/internal/model"
)

func newTorrentFile(db *gorm.DB, opts ...gen.DOOption) torrentFile {
	_torrentFile := torrentFile{}

	_torrentFile.torrentFileDo.UseDB(db, opts...)
	_torrentFile.torrentFileDo.UseModel(&model.TorrentFile{})

	tableName := _torrentFile.torrentFileDo.TableName()
	_torrentFile.ALL = field.NewAsterisk(tableName)
	_torrentFile.InfoHash = field.NewField(tableName, "info_hash")
	_torrentFile.Index = field.NewUint(tableName, "index")
	_torrentFile.Path = field.NewString(tableName, "path")
	_torrentFile.Extension = field.NewString(tableName, "extension")
	_torrentFile.Size = field.NewUint(tableName, "size")
	_torrentFile.CreatedAt = field.NewTime(tableName, "created_at")
	_torrentFile.UpdatedAt = field.NewTime(tableName, "updated_at")

	_torrentFile.fillFieldMap()

	return _torrentFile
}

type torrentFile struct {
	torrentFileDo

	ALL       field.Asterisk
	InfoHash  field.Field
	Index     field.Uint
	Path      field.String
	Extension field.String
	Size      field.Uint
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (t torrentFile) Table(newTableName string) *torrentFile {
	t.torrentFileDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t torrentFile) As(alias string) *torrentFile {
	t.torrentFileDo.DO = *(t.torrentFileDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *torrentFile) updateTableName(table string) *torrentFile {
	t.ALL = field.NewAsterisk(table)
	t.InfoHash = field.NewField(table, "info_hash")
	t.Index = field.NewUint(table, "index")
	t.Path = field.NewString(table, "path")
	t.Extension = field.NewString(table, "extension")
	t.Size = field.NewUint(table, "size")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *torrentFile) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := t.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (t *torrentFile) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 7)
	t.fieldMap["info_hash"] = t.InfoHash
	t.fieldMap["index"] = t.Index
	t.fieldMap["path"] = t.Path
	t.fieldMap["extension"] = t.Extension
	t.fieldMap["size"] = t.Size
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

func (t torrentFile) clone(db *gorm.DB) torrentFile {
	t.torrentFileDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t torrentFile) replaceDB(db *gorm.DB) torrentFile {
	t.torrentFileDo.ReplaceDB(db)
	return t
}

type torrentFileDo struct{ gen.DO }

type ITorrentFileDo interface {
	gen.SubQuery
	Debug() ITorrentFileDo
	WithContext(ctx context.Context) ITorrentFileDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ITorrentFileDo
	WriteDB() ITorrentFileDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ITorrentFileDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ITorrentFileDo
	Not(conds ...gen.Condition) ITorrentFileDo
	Or(conds ...gen.Condition) ITorrentFileDo
	Select(conds ...field.Expr) ITorrentFileDo
	Where(conds ...gen.Condition) ITorrentFileDo
	Order(conds ...field.Expr) ITorrentFileDo
	Distinct(cols ...field.Expr) ITorrentFileDo
	Omit(cols ...field.Expr) ITorrentFileDo
	Join(table schema.Tabler, on ...field.Expr) ITorrentFileDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ITorrentFileDo
	RightJoin(table schema.Tabler, on ...field.Expr) ITorrentFileDo
	Group(cols ...field.Expr) ITorrentFileDo
	Having(conds ...gen.Condition) ITorrentFileDo
	Limit(limit int) ITorrentFileDo
	Offset(offset int) ITorrentFileDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentFileDo
	Unscoped() ITorrentFileDo
	Create(values ...*model.TorrentFile) error
	CreateInBatches(values []*model.TorrentFile, batchSize int) error
	Save(values ...*model.TorrentFile) error
	First() (*model.TorrentFile, error)
	Take() (*model.TorrentFile, error)
	Last() (*model.TorrentFile, error)
	Find() ([]*model.TorrentFile, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.TorrentFile, err error)
	FindInBatches(result *[]*model.TorrentFile, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.TorrentFile) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) ITorrentFileDo
	Assign(attrs ...field.AssignExpr) ITorrentFileDo
	Joins(fields ...field.RelationField) ITorrentFileDo
	Preload(fields ...field.RelationField) ITorrentFileDo
	FirstOrInit() (*model.TorrentFile, error)
	FirstOrCreate() (*model.TorrentFile, error)
	FindByPage(offset int, limit int) (result []*model.TorrentFile, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) ITorrentFileDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (t torrentFileDo) Debug() ITorrentFileDo {
	return t.withDO(t.DO.Debug())
}

func (t torrentFileDo) WithContext(ctx context.Context) ITorrentFileDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t torrentFileDo) ReadDB() ITorrentFileDo {
	return t.Clauses(dbresolver.Read)
}

func (t torrentFileDo) WriteDB() ITorrentFileDo {
	return t.Clauses(dbresolver.Write)
}

func (t torrentFileDo) Session(config *gorm.Session) ITorrentFileDo {
	return t.withDO(t.DO.Session(config))
}

func (t torrentFileDo) Clauses(conds ...clause.Expression) ITorrentFileDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t torrentFileDo) Returning(value interface{}, columns ...string) ITorrentFileDo {
	return t.withDO(t.DO.Returning(value, columns...))
}

func (t torrentFileDo) Not(conds ...gen.Condition) ITorrentFileDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t torrentFileDo) Or(conds ...gen.Condition) ITorrentFileDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t torrentFileDo) Select(conds ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t torrentFileDo) Where(conds ...gen.Condition) ITorrentFileDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t torrentFileDo) Order(conds ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t torrentFileDo) Distinct(cols ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t torrentFileDo) Omit(cols ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Omit(cols...))
}

func (t torrentFileDo) Join(table schema.Tabler, on ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Join(table, on...))
}

func (t torrentFileDo) LeftJoin(table schema.Tabler, on ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.LeftJoin(table, on...))
}

func (t torrentFileDo) RightJoin(table schema.Tabler, on ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.RightJoin(table, on...))
}

func (t torrentFileDo) Group(cols ...field.Expr) ITorrentFileDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t torrentFileDo) Having(conds ...gen.Condition) ITorrentFileDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t torrentFileDo) Limit(limit int) ITorrentFileDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t torrentFileDo) Offset(offset int) ITorrentFileDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t torrentFileDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentFileDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t torrentFileDo) Unscoped() ITorrentFileDo {
	return t.withDO(t.DO.Unscoped())
}

func (t torrentFileDo) Create(values ...*model.TorrentFile) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t torrentFileDo) CreateInBatches(values []*model.TorrentFile, batchSize int) error {
	return t.DO.CreateInBatches(values, batchSize)
}

// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (t torrentFileDo) Save(values ...*model.TorrentFile) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t torrentFileDo) First() (*model.TorrentFile, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.TorrentFile), nil
	}
}

func (t torrentFileDo) Take() (*model.TorrentFile, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.TorrentFile), nil
	}
}

func (t torrentFileDo) Last() (*model.TorrentFile, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.TorrentFile), nil
	}
}

func (t torrentFileDo) Find() ([]*model.TorrentFile, error) {
	result, err := t.DO.Find()
	return result.([]*model.TorrentFile), err
}

func (t torrentFileDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.TorrentFile, err error) {
	buf := make([]*model.TorrentFile, 0, batchSize)
	err = t.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
		defer func() { results = append(results, buf...) }()
		return fc(tx, batch)
	})
	return results, err
}

func (t torrentFileDo) FindInBatches(result *[]*model.TorrentFile, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return t.DO.FindInBatches(result, batchSize, fc)
}

func (t torrentFileDo) Attrs(attrs ...field.AssignExpr) ITorrentFileDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t torrentFileDo) Assign(attrs ...field.AssignExpr) ITorrentFileDo {
	return t.withDO(t.DO.Assign(attrs...))
}

func (t torrentFileDo) Joins(fields ...field.RelationField) ITorrentFileDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Joins(_f))
	}
	return &t
}

func (t torrentFileDo) Preload(fields ...field.RelationField) ITorrentFileDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Preload(_f))
	}
	return &t
}

func (t torrentFileDo) FirstOrInit() (*model.TorrentFile, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.TorrentFile), nil
	}
}

func (t torrentFileDo) FirstOrCreate() (*model.TorrentFile, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.TorrentFile), nil
	}
}

func (t torrentFileDo) FindByPage(offset int, limit int) (result []*model.TorrentFile, count int64, err error) {
	result, err = t.Offset(offset).Limit(limit).Find()
	if err != nil {
		return
	}

	if size := len(result); 0 < limit && 0 < size && size < limit {
		count = int64(size + offset)
		return
	}

	count, err = t.Offset(-1).Limit(-1).Count()
	return
}

func (t torrentFileDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = t.Count()
	if err != nil {
		return
	}

	err = t.Offset(offset).Limit(limit).Scan(result)
	return
}

func (t torrentFileDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t torrentFileDo) Delete(models ...*model.TorrentFile) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *torrentFileDo) withDO(do gen.Dao) *torrentFileDo {
	t.DO = *do.(*gen.DO)
	return t
}
