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

	_torrentSource.torrentSourceDo.UseDB(db, opts...)
	_torrentSource.torrentSourceDo.UseModel(&model.TorrentSource{})

	tableName := _torrentSource.torrentSourceDo.TableName()
	_torrentSource.ALL = field.NewAsterisk(tableName)
	_torrentSource.Key = field.NewString(tableName, "key")
	_torrentSource.Name = field.NewString(tableName, "name")
	_torrentSource.CreatedAt = field.NewTime(tableName, "created_at")
	_torrentSource.UpdatedAt = field.NewTime(tableName, "updated_at")

	_torrentSource.fillFieldMap()

	return _torrentSource
}

type torrentSource struct {
	torrentSourceDo

	ALL       field.Asterisk
	Key       field.String
	Name      field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (t torrentSource) Table(newTableName string) *torrentSource {
	t.torrentSourceDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *torrentSource) updateTableName(table string) *torrentSource {
	t.ALL = field.NewAsterisk(table)
	t.Key = field.NewString(table, "key")
	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 *torrentSource) 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 *torrentSource) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 4)
	t.fieldMap["key"] = t.Key
	t.fieldMap["name"] = t.Name
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

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

func (t torrentSource) replaceDB(db *gorm.DB) torrentSource {
	t.torrentSourceDo.ReplaceDB(db)
	return t
}

type torrentSourceDo struct{ gen.DO }

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

func (t torrentSourceDo) Debug() ITorrentSourceDo {
	return t.withDO(t.DO.Debug())
}

func (t torrentSourceDo) WithContext(ctx context.Context) ITorrentSourceDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t torrentSourceDo) ReadDB() ITorrentSourceDo {
	return t.Clauses(dbresolver.Read)
}

func (t torrentSourceDo) WriteDB() ITorrentSourceDo {
	return t.Clauses(dbresolver.Write)
}

func (t torrentSourceDo) Session(config *gorm.Session) ITorrentSourceDo {
	return t.withDO(t.DO.Session(config))
}

func (t torrentSourceDo) Clauses(conds ...clause.Expression) ITorrentSourceDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t torrentSourceDo) Not(conds ...gen.Condition) ITorrentSourceDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t torrentSourceDo) Or(conds ...gen.Condition) ITorrentSourceDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t torrentSourceDo) Select(conds ...field.Expr) ITorrentSourceDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t torrentSourceDo) Where(conds ...gen.Condition) ITorrentSourceDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t torrentSourceDo) Order(conds ...field.Expr) ITorrentSourceDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t torrentSourceDo) Distinct(cols ...field.Expr) ITorrentSourceDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t torrentSourceDo) Omit(cols ...field.Expr) ITorrentSourceDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t torrentSourceDo) Group(cols ...field.Expr) ITorrentSourceDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t torrentSourceDo) Having(conds ...gen.Condition) ITorrentSourceDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t torrentSourceDo) Limit(limit int) ITorrentSourceDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t torrentSourceDo) Offset(offset int) ITorrentSourceDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t torrentSourceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentSourceDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t torrentSourceDo) Unscoped() ITorrentSourceDo {
	return t.withDO(t.DO.Unscoped())
}

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

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

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

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

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

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

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

func (t torrentSourceDo) Attrs(attrs ...field.AssignExpr) ITorrentSourceDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t torrentSourceDo) Assign(attrs ...field.AssignExpr) ITorrentSourceDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

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

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

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

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

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