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

	_torrentPieces.torrentPiecesDo.UseDB(db, opts...)
	_torrentPieces.torrentPiecesDo.UseModel(&model.TorrentPieces{})

	tableName := _torrentPieces.torrentPiecesDo.TableName()
	_torrentPieces.ALL = field.NewAsterisk(tableName)
	_torrentPieces.InfoHash = field.NewField(tableName, "info_hash")
	_torrentPieces.PieceLength = field.NewInt64(tableName, "piece_length")
	_torrentPieces.Pieces = field.NewBytes(tableName, "pieces")
	_torrentPieces.CreatedAt = field.NewTime(tableName, "created_at")

	_torrentPieces.fillFieldMap()

	return _torrentPieces
}

type torrentPieces struct {
	torrentPiecesDo

	ALL         field.Asterisk
	InfoHash    field.Field
	PieceLength field.Int64
	Pieces      field.Bytes
	CreatedAt   field.Time

	fieldMap map[string]field.Expr
}

func (t torrentPieces) Table(newTableName string) *torrentPieces {
	t.torrentPiecesDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *torrentPieces) updateTableName(table string) *torrentPieces {
	t.ALL = field.NewAsterisk(table)
	t.InfoHash = field.NewField(table, "info_hash")
	t.PieceLength = field.NewInt64(table, "piece_length")
	t.Pieces = field.NewBytes(table, "pieces")
	t.CreatedAt = field.NewTime(table, "created_at")

	t.fillFieldMap()

	return t
}

func (t *torrentPieces) 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 *torrentPieces) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 4)
	t.fieldMap["info_hash"] = t.InfoHash
	t.fieldMap["piece_length"] = t.PieceLength
	t.fieldMap["pieces"] = t.Pieces
	t.fieldMap["created_at"] = t.CreatedAt
}

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

func (t torrentPieces) replaceDB(db *gorm.DB) torrentPieces {
	t.torrentPiecesDo.ReplaceDB(db)
	return t
}

type torrentPiecesDo struct{ gen.DO }

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

func (t torrentPiecesDo) Debug() ITorrentPiecesDo {
	return t.withDO(t.DO.Debug())
}

func (t torrentPiecesDo) WithContext(ctx context.Context) ITorrentPiecesDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t torrentPiecesDo) ReadDB() ITorrentPiecesDo {
	return t.Clauses(dbresolver.Read)
}

func (t torrentPiecesDo) WriteDB() ITorrentPiecesDo {
	return t.Clauses(dbresolver.Write)
}

func (t torrentPiecesDo) Session(config *gorm.Session) ITorrentPiecesDo {
	return t.withDO(t.DO.Session(config))
}

func (t torrentPiecesDo) Clauses(conds ...clause.Expression) ITorrentPiecesDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t torrentPiecesDo) Not(conds ...gen.Condition) ITorrentPiecesDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t torrentPiecesDo) Or(conds ...gen.Condition) ITorrentPiecesDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t torrentPiecesDo) Select(conds ...field.Expr) ITorrentPiecesDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t torrentPiecesDo) Where(conds ...gen.Condition) ITorrentPiecesDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t torrentPiecesDo) Order(conds ...field.Expr) ITorrentPiecesDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t torrentPiecesDo) Distinct(cols ...field.Expr) ITorrentPiecesDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t torrentPiecesDo) Omit(cols ...field.Expr) ITorrentPiecesDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t torrentPiecesDo) Group(cols ...field.Expr) ITorrentPiecesDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t torrentPiecesDo) Having(conds ...gen.Condition) ITorrentPiecesDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t torrentPiecesDo) Limit(limit int) ITorrentPiecesDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t torrentPiecesDo) Offset(offset int) ITorrentPiecesDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t torrentPiecesDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITorrentPiecesDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t torrentPiecesDo) Unscoped() ITorrentPiecesDo {
	return t.withDO(t.DO.Unscoped())
}

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

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

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

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

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

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

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

func (t torrentPiecesDo) Attrs(attrs ...field.AssignExpr) ITorrentPiecesDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t torrentPiecesDo) Assign(attrs ...field.AssignExpr) ITorrentPiecesDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

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

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

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

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

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