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

	_dokument.dokumentDo.UseDB(db, opts...)
	_dokument.dokumentDo.UseModel(&model.Dokument{})

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

	_dokument.fillFieldMap()

	return _dokument
}

type dokument struct {
	dokumentDo

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

	fieldMap map[string]field.Expr
}

func (d dokument) Table(newTableName string) *dokument {
	d.dokumentDo.UseTable(newTableName)
	return d.updateTableName(newTableName)
}

func (d dokument) As(alias string) *dokument {
	d.dokumentDo.DO = *(d.dokumentDo.As(alias).(*gen.DO))
	return d.updateTableName(alias)
}

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

	d.fillFieldMap()

	return d
}

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

func (d *dokument) fillFieldMap() {
	d.fieldMap = make(map[string]field.Expr, 4)
	d.fieldMap["id"] = d.ID
	d.fieldMap["created_at"] = d.CreatedAt
	d.fieldMap["updated_at"] = d.UpdatedAt
	d.fieldMap["deleted_at"] = d.DeletedAt
}

func (d dokument) clone(db *gorm.DB) dokument {
	d.dokumentDo.ReplaceConnPool(db.Statement.ConnPool)
	return d
}

func (d dokument) replaceDB(db *gorm.DB) dokument {
	d.dokumentDo.ReplaceDB(db)
	return d
}

type dokumentDo struct{ gen.DO }

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

func (d dokumentDo) Debug() IDokumentDo {
	return d.withDO(d.DO.Debug())
}

func (d dokumentDo) WithContext(ctx context.Context) IDokumentDo {
	return d.withDO(d.DO.WithContext(ctx))
}

func (d dokumentDo) ReadDB() IDokumentDo {
	return d.Clauses(dbresolver.Read)
}

func (d dokumentDo) WriteDB() IDokumentDo {
	return d.Clauses(dbresolver.Write)
}

func (d dokumentDo) Session(config *gorm.Session) IDokumentDo {
	return d.withDO(d.DO.Session(config))
}

func (d dokumentDo) Clauses(conds ...clause.Expression) IDokumentDo {
	return d.withDO(d.DO.Clauses(conds...))
}

func (d dokumentDo) Returning(value interface{}, columns ...string) IDokumentDo {
	return d.withDO(d.DO.Returning(value, columns...))
}

func (d dokumentDo) Not(conds ...gen.Condition) IDokumentDo {
	return d.withDO(d.DO.Not(conds...))
}

func (d dokumentDo) Or(conds ...gen.Condition) IDokumentDo {
	return d.withDO(d.DO.Or(conds...))
}

func (d dokumentDo) Select(conds ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Select(conds...))
}

func (d dokumentDo) Where(conds ...gen.Condition) IDokumentDo {
	return d.withDO(d.DO.Where(conds...))
}

func (d dokumentDo) Order(conds ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Order(conds...))
}

func (d dokumentDo) Distinct(cols ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Distinct(cols...))
}

func (d dokumentDo) Omit(cols ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Omit(cols...))
}

func (d dokumentDo) Join(table schema.Tabler, on ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Join(table, on...))
}

func (d dokumentDo) LeftJoin(table schema.Tabler, on ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.LeftJoin(table, on...))
}

func (d dokumentDo) RightJoin(table schema.Tabler, on ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.RightJoin(table, on...))
}

func (d dokumentDo) Group(cols ...field.Expr) IDokumentDo {
	return d.withDO(d.DO.Group(cols...))
}

func (d dokumentDo) Having(conds ...gen.Condition) IDokumentDo {
	return d.withDO(d.DO.Having(conds...))
}

func (d dokumentDo) Limit(limit int) IDokumentDo {
	return d.withDO(d.DO.Limit(limit))
}

func (d dokumentDo) Offset(offset int) IDokumentDo {
	return d.withDO(d.DO.Offset(offset))
}

func (d dokumentDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IDokumentDo {
	return d.withDO(d.DO.Scopes(funcs...))
}

func (d dokumentDo) Unscoped() IDokumentDo {
	return d.withDO(d.DO.Unscoped())
}

func (d dokumentDo) Create(values ...*model.Dokument) error {
	if len(values) == 0 {
		return nil
	}
	return d.DO.Create(values)
}

func (d dokumentDo) CreateInBatches(values []*model.Dokument, batchSize int) error {
	return d.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 (d dokumentDo) Save(values ...*model.Dokument) error {
	if len(values) == 0 {
		return nil
	}
	return d.DO.Save(values)
}

func (d dokumentDo) First() (*model.Dokument, error) {
	if result, err := d.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Dokument), nil
	}
}

func (d dokumentDo) Take() (*model.Dokument, error) {
	if result, err := d.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Dokument), nil
	}
}

func (d dokumentDo) Last() (*model.Dokument, error) {
	if result, err := d.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Dokument), nil
	}
}

func (d dokumentDo) Find() ([]*model.Dokument, error) {
	result, err := d.DO.Find()
	return result.([]*model.Dokument), err
}

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

func (d dokumentDo) Attrs(attrs ...field.AssignExpr) IDokumentDo {
	return d.withDO(d.DO.Attrs(attrs...))
}

func (d dokumentDo) Assign(attrs ...field.AssignExpr) IDokumentDo {
	return d.withDO(d.DO.Assign(attrs...))
}

func (d dokumentDo) Joins(fields ...field.RelationField) IDokumentDo {
	for _, _f := range fields {
		d = *d.withDO(d.DO.Joins(_f))
	}
	return &d
}

func (d dokumentDo) Preload(fields ...field.RelationField) IDokumentDo {
	for _, _f := range fields {
		d = *d.withDO(d.DO.Preload(_f))
	}
	return &d
}

func (d dokumentDo) FirstOrInit() (*model.Dokument, error) {
	if result, err := d.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Dokument), nil
	}
}

func (d dokumentDo) FirstOrCreate() (*model.Dokument, error) {
	if result, err := d.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Dokument), nil
	}
}

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

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

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

func (d dokumentDo) Scan(result interface{}) (err error) {
	return d.DO.Scan(result)
}

func (d dokumentDo) Delete(models ...*model.Dokument) (result gen.ResultInfo, err error) {
	return d.DO.Delete(models)
}

func (d *dokumentDo) withDO(do gen.Dao) *dokumentDo {
	d.DO = *do.(*gen.DO)
	return d
}
