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

	_torrentsTorrentSource.torrentsTorrentSourceDo.UseDB(db, opts...)
	_torrentsTorrentSource.torrentsTorrentSourceDo.UseModel(&model.TorrentsTorrentSource{})

	tableName := _torrentsTorrentSource.torrentsTorrentSourceDo.TableName()
	_torrentsTorrentSource.ALL = field.NewAsterisk(tableName)
	_torrentsTorrentSource.Source = field.NewString(tableName, "source")
	_torrentsTorrentSource.InfoHash = field.NewField(tableName, "info_hash")
	_torrentsTorrentSource.ImportID = field.NewField(tableName, "import_id")
	_torrentsTorrentSource.Seeders = field.NewField(tableName, "seeders")
	_torrentsTorrentSource.Leechers = field.NewField(tableName, "leechers")
	_torrentsTorrentSource.PublishedAt = field.NewField(tableName, "published_at")
	_torrentsTorrentSource.CreatedAt = field.NewTime(tableName, "created_at")
	_torrentsTorrentSource.UpdatedAt = field.NewTime(tableName, "updated_at")
	_torrentsTorrentSource.TorrentSource = torrentsTorrentSourceHasOneTorrentSource{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("TorrentSource", "model.TorrentSource"),
	}

	_torrentsTorrentSource.fillFieldMap()

	return _torrentsTorrentSource
}

type torrentsTorrentSource struct {
	torrentsTorrentSourceDo

	ALL           field.Asterisk
	Source        field.String
	InfoHash      field.Field
	ImportID      field.Field
	Seeders       field.Field
	Leechers      field.Field
	PublishedAt   field.Field
	CreatedAt     field.Time
	UpdatedAt     field.Time
	TorrentSource torrentsTorrentSourceHasOneTorrentSource

	fieldMap map[string]field.Expr
}

func (t torrentsTorrentSource) Table(newTableName string) *torrentsTorrentSource {
	t.torrentsTorrentSourceDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *torrentsTorrentSource) updateTableName(table string) *torrentsTorrentSource {
	t.ALL = field.NewAsterisk(table)
	t.Source = field.NewString(table, "source")
	t.InfoHash = field.NewField(table, "info_hash")
	t.ImportID = field.NewField(table, "import_id")
	t.Seeders = field.NewField(table, "seeders")
	t.Leechers = field.NewField(table, "leechers")
	t.PublishedAt = field.NewField(table, "published_at")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *torrentsTorrentSource) 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 *torrentsTorrentSource) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 9)
	t.fieldMap["source"] = t.Source
	t.fieldMap["info_hash"] = t.InfoHash
	t.fieldMap["import_id"] = t.ImportID
	t.fieldMap["seeders"] = t.Seeders
	t.fieldMap["leechers"] = t.Leechers
	t.fieldMap["published_at"] = t.PublishedAt
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt

}

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

func (t torrentsTorrentSource) replaceDB(db *gorm.DB) torrentsTorrentSource {
	t.torrentsTorrentSourceDo.ReplaceDB(db)
	return t
}

type torrentsTorrentSourceHasOneTorrentSource struct {
	db *gorm.DB

	field.RelationField
}

func (a torrentsTorrentSourceHasOneTorrentSource) Where(conds ...field.Expr) *torrentsTorrentSourceHasOneTorrentSource {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a torrentsTorrentSourceHasOneTorrentSource) WithContext(ctx context.Context) *torrentsTorrentSourceHasOneTorrentSource {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a torrentsTorrentSourceHasOneTorrentSource) Session(session *gorm.Session) *torrentsTorrentSourceHasOneTorrentSource {
	a.db = a.db.Session(session)
	return &a
}

func (a torrentsTorrentSourceHasOneTorrentSource) Model(m *model.TorrentsTorrentSource) *torrentsTorrentSourceHasOneTorrentSourceTx {
	return &torrentsTorrentSourceHasOneTorrentSourceTx{a.db.Model(m).Association(a.Name())}
}

type torrentsTorrentSourceHasOneTorrentSourceTx struct{ tx *gorm.Association }

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Find() (result *model.TorrentSource, err error) {
	return result, a.tx.Find(&result)
}

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Append(values ...*model.TorrentSource) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Replace(values ...*model.TorrentSource) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Delete(values ...*model.TorrentSource) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Clear() error {
	return a.tx.Clear()
}

func (a torrentsTorrentSourceHasOneTorrentSourceTx) Count() int64 {
	return a.tx.Count()
}

type torrentsTorrentSourceDo struct{ gen.DO }

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

func (t torrentsTorrentSourceDo) Debug() ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Debug())
}

func (t torrentsTorrentSourceDo) WithContext(ctx context.Context) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t torrentsTorrentSourceDo) ReadDB() ITorrentsTorrentSourceDo {
	return t.Clauses(dbresolver.Read)
}

func (t torrentsTorrentSourceDo) WriteDB() ITorrentsTorrentSourceDo {
	return t.Clauses(dbresolver.Write)
}

func (t torrentsTorrentSourceDo) Session(config *gorm.Session) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Session(config))
}

func (t torrentsTorrentSourceDo) Clauses(conds ...clause.Expression) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t torrentsTorrentSourceDo) Not(conds ...gen.Condition) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t torrentsTorrentSourceDo) Or(conds ...gen.Condition) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t torrentsTorrentSourceDo) Select(conds ...field.Expr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t torrentsTorrentSourceDo) Where(conds ...gen.Condition) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t torrentsTorrentSourceDo) Order(conds ...field.Expr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t torrentsTorrentSourceDo) Distinct(cols ...field.Expr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t torrentsTorrentSourceDo) Omit(cols ...field.Expr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t torrentsTorrentSourceDo) Group(cols ...field.Expr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t torrentsTorrentSourceDo) Having(conds ...gen.Condition) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t torrentsTorrentSourceDo) Limit(limit int) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t torrentsTorrentSourceDo) Offset(offset int) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t torrentsTorrentSourceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t torrentsTorrentSourceDo) Unscoped() ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Unscoped())
}

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

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

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

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

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

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

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

func (t torrentsTorrentSourceDo) Attrs(attrs ...field.AssignExpr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t torrentsTorrentSourceDo) Assign(attrs ...field.AssignExpr) ITorrentsTorrentSourceDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

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

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

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

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

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