// 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 newTorrentTag(db *gorm.DB, opts ...gen.DOOption) torrentTag {
	_torrentTag := torrentTag{}

	_torrentTag.torrentTagDo.UseDB(db, opts...)
	_torrentTag.torrentTagDo.UseModel(&model.TorrentTag{})

	tableName := _torrentTag.torrentTagDo.TableName()
	_torrentTag.ALL = field.NewAsterisk(tableName)
	_torrentTag.InfoHash = field.NewField(tableName, "info_hash")
	_torrentTag.Name = field.NewString(tableName, "name")
	_torrentTag.CreatedAt = field.NewTime(tableName, "created_at")
	_torrentTag.UpdatedAt = field.NewTime(tableName, "updated_at")

	_torrentTag.fillFieldMap()

	return _torrentTag
}

type torrentTag struct {
	torrentTagDo

	ALL       field.Asterisk
	InfoHash  field.Field
	Name      field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (t torrentTag) Table(newTableName string) *torrentTag {
	t.torrentTagDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *torrentTag) updateTableName(table string) *torrentTag {
	t.ALL = field.NewAsterisk(table)
	t.InfoHash = field.NewField(table, "info_hash")
	t.Name = field.NewString(table, "name")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *torrentTag) 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 *torrentTag) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 4)
	t.fieldMap["info_hash"] = t.InfoHash
	t.fieldMap["name"] = t.Name
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

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

func (t torrentTag) replaceDB(db *gorm.DB) torrentTag {
	t.torrentTagDo.ReplaceDB(db)
	return t
}

type torrentTagDo struct{ gen.DO }

type ITorrentTagDo interface {
	gen.SubQuery
	Debug() ITorrentTagDo
	WithContext(ctx context.Context) ITorrentTagDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ITorrentTagDo
	WriteDB() ITorrentTagDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ITorrentTagDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ITorrentTagDo
	Not(conds ...gen.Condition) ITorrentTagDo
	Or(conds ...gen.Condition) ITorrentTagDo
	Select(conds ...field.Expr) ITorrentTagDo
	Where(conds ...gen.Condition) ITorrentTagDo
	Order(conds ...field.Expr) ITorrentTagDo
	Distinct(cols ...field.Expr) ITorrentTagDo
	Omit(cols ...field.Expr) ITorrentTagDo
	Join(table schema.Tabler, on ...field.Expr) ITorrentTagDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ITorrentTagDo
	RightJoin(table schema.Tabler, on ...field.Expr) ITorrentTagDo
	Group(cols ...field.Expr) ITorrentTagDo
	Having(conds ...gen.Condition) ITorrentTagDo
	Limit(limit int) ITorrentTagDo
	Offset(offset int) ITorrentTagDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentTagDo
	Unscoped() ITorrentTagDo
	Create(values ...*model.TorrentTag) error
	CreateInBatches(values []*model.TorrentTag, batchSize int) error
	Save(values ...*model.TorrentTag) error
	First() (*model.TorrentTag, error)
	Take() (*model.TorrentTag, error)
	Last() (*model.TorrentTag, error)
	Find() ([]*model.TorrentTag, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.TorrentTag, err error)
	FindInBatches(result *[]*model.TorrentTag, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.TorrentTag) (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) ITorrentTagDo
	Assign(attrs ...field.AssignExpr) ITorrentTagDo
	Joins(fields ...field.RelationField) ITorrentTagDo
	Preload(fields ...field.RelationField) ITorrentTagDo
	FirstOrInit() (*model.TorrentTag, error)
	FirstOrCreate() (*model.TorrentTag, error)
	FindByPage(offset int, limit int) (result []*model.TorrentTag, 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) ITorrentTagDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (t torrentTagDo) Debug() ITorrentTagDo {
	return t.withDO(t.DO.Debug())
}

func (t torrentTagDo) WithContext(ctx context.Context) ITorrentTagDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t torrentTagDo) ReadDB() ITorrentTagDo {
	return t.Clauses(dbresolver.Read)
}

func (t torrentTagDo) WriteDB() ITorrentTagDo {
	return t.Clauses(dbresolver.Write)
}

func (t torrentTagDo) Session(config *gorm.Session) ITorrentTagDo {
	return t.withDO(t.DO.Session(config))
}

func (t torrentTagDo) Clauses(conds ...clause.Expression) ITorrentTagDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t torrentTagDo) Not(conds ...gen.Condition) ITorrentTagDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t torrentTagDo) Or(conds ...gen.Condition) ITorrentTagDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t torrentTagDo) Select(conds ...field.Expr) ITorrentTagDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t torrentTagDo) Where(conds ...gen.Condition) ITorrentTagDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t torrentTagDo) Order(conds ...field.Expr) ITorrentTagDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t torrentTagDo) Distinct(cols ...field.Expr) ITorrentTagDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t torrentTagDo) Omit(cols ...field.Expr) ITorrentTagDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t torrentTagDo) Group(cols ...field.Expr) ITorrentTagDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t torrentTagDo) Having(conds ...gen.Condition) ITorrentTagDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t torrentTagDo) Limit(limit int) ITorrentTagDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t torrentTagDo) Offset(offset int) ITorrentTagDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t torrentTagDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentTagDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t torrentTagDo) Unscoped() ITorrentTagDo {
	return t.withDO(t.DO.Unscoped())
}

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

func (t torrentTagDo) CreateInBatches(values []*model.TorrentTag, 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 torrentTagDo) Save(values ...*model.TorrentTag) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

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

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

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

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

func (t torrentTagDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.TorrentTag, err error) {
	buf := make([]*model.TorrentTag, 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 torrentTagDo) FindInBatches(result *[]*model.TorrentTag, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return t.DO.FindInBatches(result, batchSize, fc)
}

func (t torrentTagDo) Attrs(attrs ...field.AssignExpr) ITorrentTagDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t torrentTagDo) Assign(attrs ...field.AssignExpr) ITorrentTagDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

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

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

func (t torrentTagDo) FindByPage(offset int, limit int) (result []*model.TorrentTag, 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 torrentTagDo) 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 torrentTagDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

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

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