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

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

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

	"gorm.io/plugin/dbresolver"

	"git.kocoder.xyz/kocoded/vt/model"
)

func newProjekt(db *gorm.DB, opts ...gen.DOOption) projekt {
	_projekt := projekt{}

	_projekt.projektDo.UseDB(db, opts...)
	_projekt.projektDo.UseModel(&model.Projekt{})

	tableName := _projekt.projektDo.TableName()
	_projekt.ALL = field.NewAsterisk(tableName)
	_projekt.ID = field.NewUint(tableName, "id")
	_projekt.CreatedAt = field.NewTime(tableName, "created_at")
	_projekt.UpdatedAt = field.NewTime(tableName, "updated_at")
	_projekt.DeletedAt = field.NewField(tableName, "deleted_at")

	_projekt.fillFieldMap()

	return _projekt
}

type projekt struct {
	projektDo

	ALL       field.Asterisk
	ID        field.Uint
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (p projekt) Table(newTableName string) *projekt {
	p.projektDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *projekt) updateTableName(table string) *projekt {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewUint(table, "id")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.DeletedAt = field.NewField(table, "deleted_at")

	p.fillFieldMap()

	return p
}

func (p *projekt) 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 *projekt) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 4)
	p.fieldMap["id"] = p.ID
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["deleted_at"] = p.DeletedAt
}

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

func (p projekt) replaceDB(db *gorm.DB) projekt {
	p.projektDo.ReplaceDB(db)
	return p
}

type projektDo struct{ gen.DO }

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

func (p projektDo) Debug() IProjektDo {
	return p.withDO(p.DO.Debug())
}

func (p projektDo) WithContext(ctx context.Context) IProjektDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p projektDo) ReadDB() IProjektDo {
	return p.Clauses(dbresolver.Read)
}

func (p projektDo) WriteDB() IProjektDo {
	return p.Clauses(dbresolver.Write)
}

func (p projektDo) Session(config *gorm.Session) IProjektDo {
	return p.withDO(p.DO.Session(config))
}

func (p projektDo) Clauses(conds ...clause.Expression) IProjektDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p projektDo) Not(conds ...gen.Condition) IProjektDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p projektDo) Or(conds ...gen.Condition) IProjektDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p projektDo) Select(conds ...field.Expr) IProjektDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p projektDo) Where(conds ...gen.Condition) IProjektDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p projektDo) Order(conds ...field.Expr) IProjektDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p projektDo) Distinct(cols ...field.Expr) IProjektDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p projektDo) Omit(cols ...field.Expr) IProjektDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p projektDo) Group(cols ...field.Expr) IProjektDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p projektDo) Having(conds ...gen.Condition) IProjektDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p projektDo) Limit(limit int) IProjektDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p projektDo) Offset(offset int) IProjektDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p projektDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IProjektDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p projektDo) Unscoped() IProjektDo {
	return p.withDO(p.DO.Unscoped())
}

func (p projektDo) Create(values ...*model.Projekt) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p projektDo) First() (*model.Projekt, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Projekt), nil
	}
}

func (p projektDo) Take() (*model.Projekt, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Projekt), nil
	}
}

func (p projektDo) Last() (*model.Projekt, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Projekt), nil
	}
}

func (p projektDo) Find() ([]*model.Projekt, error) {
	result, err := p.DO.Find()
	return result.([]*model.Projekt), err
}

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

func (p projektDo) Attrs(attrs ...field.AssignExpr) IProjektDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p projektDo) Assign(attrs ...field.AssignExpr) IProjektDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p projektDo) FirstOrInit() (*model.Projekt, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Projekt), nil
	}
}

func (p projektDo) FirstOrCreate() (*model.Projekt, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Projekt), nil
	}
}

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

func (p projektDo) Delete(models ...*model.Projekt) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

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