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

	_metadataSource.metadataSourceDo.UseDB(db, opts...)
	_metadataSource.metadataSourceDo.UseModel(&model.MetadataSource{})

	tableName := _metadataSource.metadataSourceDo.TableName()
	_metadataSource.ALL = field.NewAsterisk(tableName)
	_metadataSource.Key = field.NewString(tableName, "key")
	_metadataSource.Name = field.NewString(tableName, "name")
	_metadataSource.CreatedAt = field.NewTime(tableName, "created_at")
	_metadataSource.UpdatedAt = field.NewTime(tableName, "updated_at")

	_metadataSource.fillFieldMap()

	return _metadataSource
}

type metadataSource struct {
	metadataSourceDo

	ALL       field.Asterisk
	Key       field.String
	Name      field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (m metadataSource) Table(newTableName string) *metadataSource {
	m.metadataSourceDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m metadataSource) As(alias string) *metadataSource {
	m.metadataSourceDo.DO = *(m.metadataSourceDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *metadataSource) updateTableName(table string) *metadataSource {
	m.ALL = field.NewAsterisk(table)
	m.Key = field.NewString(table, "key")
	m.Name = field.NewString(table, "name")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")

	m.fillFieldMap()

	return m
}

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

func (m *metadataSource) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 4)
	m.fieldMap["key"] = m.Key
	m.fieldMap["name"] = m.Name
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
}

func (m metadataSource) clone(db *gorm.DB) metadataSource {
	m.metadataSourceDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m metadataSource) replaceDB(db *gorm.DB) metadataSource {
	m.metadataSourceDo.ReplaceDB(db)
	return m
}

type metadataSourceDo struct{ gen.DO }

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

func (m metadataSourceDo) Debug() IMetadataSourceDo {
	return m.withDO(m.DO.Debug())
}

func (m metadataSourceDo) WithContext(ctx context.Context) IMetadataSourceDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m metadataSourceDo) ReadDB() IMetadataSourceDo {
	return m.Clauses(dbresolver.Read)
}

func (m metadataSourceDo) WriteDB() IMetadataSourceDo {
	return m.Clauses(dbresolver.Write)
}

func (m metadataSourceDo) Session(config *gorm.Session) IMetadataSourceDo {
	return m.withDO(m.DO.Session(config))
}

func (m metadataSourceDo) Clauses(conds ...clause.Expression) IMetadataSourceDo {
	return m.withDO(m.DO.Clauses(conds...))
}

func (m metadataSourceDo) Returning(value interface{}, columns ...string) IMetadataSourceDo {
	return m.withDO(m.DO.Returning(value, columns...))
}

func (m metadataSourceDo) Not(conds ...gen.Condition) IMetadataSourceDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m metadataSourceDo) Or(conds ...gen.Condition) IMetadataSourceDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m metadataSourceDo) Select(conds ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m metadataSourceDo) Where(conds ...gen.Condition) IMetadataSourceDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m metadataSourceDo) Order(conds ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m metadataSourceDo) Distinct(cols ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m metadataSourceDo) Omit(cols ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Omit(cols...))
}

func (m metadataSourceDo) Join(table schema.Tabler, on ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Join(table, on...))
}

func (m metadataSourceDo) LeftJoin(table schema.Tabler, on ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.LeftJoin(table, on...))
}

func (m metadataSourceDo) RightJoin(table schema.Tabler, on ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.RightJoin(table, on...))
}

func (m metadataSourceDo) Group(cols ...field.Expr) IMetadataSourceDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m metadataSourceDo) Having(conds ...gen.Condition) IMetadataSourceDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m metadataSourceDo) Limit(limit int) IMetadataSourceDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m metadataSourceDo) Offset(offset int) IMetadataSourceDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m metadataSourceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMetadataSourceDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m metadataSourceDo) Unscoped() IMetadataSourceDo {
	return m.withDO(m.DO.Unscoped())
}

func (m metadataSourceDo) Create(values ...*model.MetadataSource) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m metadataSourceDo) First() (*model.MetadataSource, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.MetadataSource), nil
	}
}

func (m metadataSourceDo) Take() (*model.MetadataSource, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.MetadataSource), nil
	}
}

func (m metadataSourceDo) Last() (*model.MetadataSource, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.MetadataSource), nil
	}
}

func (m metadataSourceDo) Find() ([]*model.MetadataSource, error) {
	result, err := m.DO.Find()
	return result.([]*model.MetadataSource), err
}

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

func (m metadataSourceDo) Attrs(attrs ...field.AssignExpr) IMetadataSourceDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m metadataSourceDo) Assign(attrs ...field.AssignExpr) IMetadataSourceDo {
	return m.withDO(m.DO.Assign(attrs...))
}

func (m metadataSourceDo) Joins(fields ...field.RelationField) IMetadataSourceDo {
	for _, _f := range fields {
		m = *m.withDO(m.DO.Joins(_f))
	}
	return &m
}

func (m metadataSourceDo) Preload(fields ...field.RelationField) IMetadataSourceDo {
	for _, _f := range fields {
		m = *m.withDO(m.DO.Preload(_f))
	}
	return &m
}

func (m metadataSourceDo) FirstOrInit() (*model.MetadataSource, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.MetadataSource), nil
	}
}

func (m metadataSourceDo) FirstOrCreate() (*model.MetadataSource, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.MetadataSource), nil
	}
}

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

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

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

func (m metadataSourceDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m metadataSourceDo) Delete(models ...*model.MetadataSource) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *metadataSourceDo) withDO(do gen.Dao) *metadataSourceDo {
	m.DO = *do.(*gen.DO)
	return m
}
