// 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 query

import (
	"context"
	"database/sql"
	"strings"

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

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

	"gorm.io/plugin/dbresolver"

	"canguidev/shelfy/internal/models"
)

func newPathDownload(db *gorm.DB, opts ...gen.DOOption) pathDownload {
	_pathDownload := pathDownload{}

	_pathDownload.pathDownloadDo.UseDB(db, opts...)
	_pathDownload.pathDownloadDo.UseModel(&models.PathDownload{})

	tableName := _pathDownload.pathDownloadDo.TableName()
	_pathDownload.ALL = field.NewAsterisk(tableName)
	_pathDownload.ID = field.NewInt64(tableName, "id")
	_pathDownload.Path = field.NewString(tableName, "path")
	_pathDownload.PathName = field.NewString(tableName, "path_name")

	_pathDownload.fillFieldMap()

	return _pathDownload
}

type pathDownload struct {
	pathDownloadDo

	ALL      field.Asterisk
	ID       field.Int64
	Path     field.String
	PathName field.String

	fieldMap map[string]field.Expr
}

func (p pathDownload) Table(newTableName string) *pathDownload {
	p.pathDownloadDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p pathDownload) As(alias string) *pathDownload {
	p.pathDownloadDo.DO = *(p.pathDownloadDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *pathDownload) updateTableName(table string) *pathDownload {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.Path = field.NewString(table, "path")
	p.PathName = field.NewString(table, "path_name")

	p.fillFieldMap()

	return p
}

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

func (p *pathDownload) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 3)
	p.fieldMap["id"] = p.ID
	p.fieldMap["path"] = p.Path
	p.fieldMap["path_name"] = p.PathName
}

func (p pathDownload) clone(db *gorm.DB) pathDownload {
	p.pathDownloadDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p pathDownload) replaceDB(db *gorm.DB) pathDownload {
	p.pathDownloadDo.ReplaceDB(db)
	return p
}

type pathDownloadDo struct{ gen.DO }

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

	FilterWithNameAndRole(name string, role string) (result []models.PathDownload, err error)
}

// SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
func (p pathDownloadDo) FilterWithNameAndRole(name string, role string) (result []models.PathDownload, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, name)
	generateSQL.WriteString("SELECT * FROM path_downloads WHERE name = ? ")
	if role != "" {
		params = append(params, role)
		generateSQL.WriteString("AND role = ? ")
	}

	var executeSQL *gorm.DB
	executeSQL = p.UnderlyingDB().Raw(generateSQL.String(), params...).Find(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (p pathDownloadDo) Debug() IPathDownloadDo {
	return p.withDO(p.DO.Debug())
}

func (p pathDownloadDo) WithContext(ctx context.Context) IPathDownloadDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p pathDownloadDo) ReadDB() IPathDownloadDo {
	return p.Clauses(dbresolver.Read)
}

func (p pathDownloadDo) WriteDB() IPathDownloadDo {
	return p.Clauses(dbresolver.Write)
}

func (p pathDownloadDo) Session(config *gorm.Session) IPathDownloadDo {
	return p.withDO(p.DO.Session(config))
}

func (p pathDownloadDo) Clauses(conds ...clause.Expression) IPathDownloadDo {
	return p.withDO(p.DO.Clauses(conds...))
}

func (p pathDownloadDo) Returning(value interface{}, columns ...string) IPathDownloadDo {
	return p.withDO(p.DO.Returning(value, columns...))
}

func (p pathDownloadDo) Not(conds ...gen.Condition) IPathDownloadDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p pathDownloadDo) Or(conds ...gen.Condition) IPathDownloadDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p pathDownloadDo) Select(conds ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p pathDownloadDo) Where(conds ...gen.Condition) IPathDownloadDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p pathDownloadDo) Order(conds ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p pathDownloadDo) Distinct(cols ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p pathDownloadDo) Omit(cols ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Omit(cols...))
}

func (p pathDownloadDo) Join(table schema.Tabler, on ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Join(table, on...))
}

func (p pathDownloadDo) LeftJoin(table schema.Tabler, on ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.LeftJoin(table, on...))
}

func (p pathDownloadDo) RightJoin(table schema.Tabler, on ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.RightJoin(table, on...))
}

func (p pathDownloadDo) Group(cols ...field.Expr) IPathDownloadDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p pathDownloadDo) Having(conds ...gen.Condition) IPathDownloadDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p pathDownloadDo) Limit(limit int) IPathDownloadDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p pathDownloadDo) Offset(offset int) IPathDownloadDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p pathDownloadDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPathDownloadDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p pathDownloadDo) Unscoped() IPathDownloadDo {
	return p.withDO(p.DO.Unscoped())
}

func (p pathDownloadDo) Create(values ...*models.PathDownload) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

func (p pathDownloadDo) CreateInBatches(values []*models.PathDownload, batchSize int) error {
	return p.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 (p pathDownloadDo) Save(values ...*models.PathDownload) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Save(values)
}

func (p pathDownloadDo) First() (*models.PathDownload, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.PathDownload), nil
	}
}

func (p pathDownloadDo) Take() (*models.PathDownload, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.PathDownload), nil
	}
}

func (p pathDownloadDo) Last() (*models.PathDownload, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.PathDownload), nil
	}
}

func (p pathDownloadDo) Find() ([]*models.PathDownload, error) {
	result, err := p.DO.Find()
	return result.([]*models.PathDownload), err
}

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

func (p pathDownloadDo) Attrs(attrs ...field.AssignExpr) IPathDownloadDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p pathDownloadDo) Assign(attrs ...field.AssignExpr) IPathDownloadDo {
	return p.withDO(p.DO.Assign(attrs...))
}

func (p pathDownloadDo) Joins(fields ...field.RelationField) IPathDownloadDo {
	for _, _f := range fields {
		p = *p.withDO(p.DO.Joins(_f))
	}
	return &p
}

func (p pathDownloadDo) Preload(fields ...field.RelationField) IPathDownloadDo {
	for _, _f := range fields {
		p = *p.withDO(p.DO.Preload(_f))
	}
	return &p
}

func (p pathDownloadDo) FirstOrInit() (*models.PathDownload, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.PathDownload), nil
	}
}

func (p pathDownloadDo) FirstOrCreate() (*models.PathDownload, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.PathDownload), nil
	}
}

func (p pathDownloadDo) FindByPage(offset int, limit int) (result []*models.PathDownload, count int64, err error) {
	result, err = p.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 = p.Offset(-1).Limit(-1).Count()
	return
}

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

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

func (p pathDownloadDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p pathDownloadDo) Delete(models ...*models.PathDownload) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *pathDownloadDo) withDO(do gen.Dao) *pathDownloadDo {
	p.DO = *do.(*gen.DO)
	return p
}
