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

import (
	"context"

	"git.mstar.dev/mstar/linstrom/storage-new/models"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

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

	"gorm.io/plugin/dbresolver"
)

func newMediaMetadata(db *gorm.DB, opts ...gen.DOOption) mediaMetadata {
	_mediaMetadata := mediaMetadata{}

	_mediaMetadata.mediaMetadataDo.UseDB(db, opts...)
	_mediaMetadata.mediaMetadataDo.UseModel(&models.MediaMetadata{})

	tableName := _mediaMetadata.mediaMetadataDo.TableName()
	_mediaMetadata.ALL = field.NewAsterisk(tableName)
	_mediaMetadata.ID = field.NewString(tableName, "id")
	_mediaMetadata.CreatedAt = field.NewTime(tableName, "created_at")
	_mediaMetadata.UpdatedAt = field.NewTime(tableName, "updated_at")
	_mediaMetadata.DeletedAt = field.NewField(tableName, "deleted_at")
	_mediaMetadata.OwnedById = field.NewField(tableName, "owned_by_id")
	_mediaMetadata.Remote = field.NewBool(tableName, "remote")
	_mediaMetadata.Location = field.NewString(tableName, "location")
	_mediaMetadata.Type = field.NewString(tableName, "type")
	_mediaMetadata.Name = field.NewString(tableName, "name")
	_mediaMetadata.AltText = field.NewString(tableName, "alt_text")
	_mediaMetadata.Blurred = field.NewBool(tableName, "blurred")

	_mediaMetadata.fillFieldMap()

	return _mediaMetadata
}

type mediaMetadata struct {
	mediaMetadataDo

	ALL       field.Asterisk
	ID        field.String
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	OwnedById field.Field
	Remote    field.Bool
	Location  field.String
	Type      field.String
	Name      field.String
	AltText   field.String
	Blurred   field.Bool

	fieldMap map[string]field.Expr
}

func (m mediaMetadata) Table(newTableName string) *mediaMetadata {
	m.mediaMetadataDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

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

func (m *mediaMetadata) updateTableName(table string) *mediaMetadata {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewString(table, "id")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")
	m.DeletedAt = field.NewField(table, "deleted_at")
	m.OwnedById = field.NewField(table, "owned_by_id")
	m.Remote = field.NewBool(table, "remote")
	m.Location = field.NewString(table, "location")
	m.Type = field.NewString(table, "type")
	m.Name = field.NewString(table, "name")
	m.AltText = field.NewString(table, "alt_text")
	m.Blurred = field.NewBool(table, "blurred")

	m.fillFieldMap()

	return m
}

func (m *mediaMetadata) 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 *mediaMetadata) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 11)
	m.fieldMap["id"] = m.ID
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["deleted_at"] = m.DeletedAt
	m.fieldMap["owned_by_id"] = m.OwnedById
	m.fieldMap["remote"] = m.Remote
	m.fieldMap["location"] = m.Location
	m.fieldMap["type"] = m.Type
	m.fieldMap["name"] = m.Name
	m.fieldMap["alt_text"] = m.AltText
	m.fieldMap["blurred"] = m.Blurred
}

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

func (m mediaMetadata) replaceDB(db *gorm.DB) mediaMetadata {
	m.mediaMetadataDo.ReplaceDB(db)
	return m
}

type mediaMetadataDo struct{ gen.DO }

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

func (m mediaMetadataDo) Debug() IMediaMetadataDo {
	return m.withDO(m.DO.Debug())
}

func (m mediaMetadataDo) WithContext(ctx context.Context) IMediaMetadataDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m mediaMetadataDo) ReadDB() IMediaMetadataDo {
	return m.Clauses(dbresolver.Read)
}

func (m mediaMetadataDo) WriteDB() IMediaMetadataDo {
	return m.Clauses(dbresolver.Write)
}

func (m mediaMetadataDo) Session(config *gorm.Session) IMediaMetadataDo {
	return m.withDO(m.DO.Session(config))
}

func (m mediaMetadataDo) Clauses(conds ...clause.Expression) IMediaMetadataDo {
	return m.withDO(m.DO.Clauses(conds...))
}

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

func (m mediaMetadataDo) Not(conds ...gen.Condition) IMediaMetadataDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m mediaMetadataDo) Or(conds ...gen.Condition) IMediaMetadataDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m mediaMetadataDo) Select(conds ...field.Expr) IMediaMetadataDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m mediaMetadataDo) Where(conds ...gen.Condition) IMediaMetadataDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m mediaMetadataDo) Order(conds ...field.Expr) IMediaMetadataDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m mediaMetadataDo) Distinct(cols ...field.Expr) IMediaMetadataDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m mediaMetadataDo) Omit(cols ...field.Expr) IMediaMetadataDo {
	return m.withDO(m.DO.Omit(cols...))
}

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

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

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

func (m mediaMetadataDo) Group(cols ...field.Expr) IMediaMetadataDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m mediaMetadataDo) Having(conds ...gen.Condition) IMediaMetadataDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m mediaMetadataDo) Limit(limit int) IMediaMetadataDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m mediaMetadataDo) Offset(offset int) IMediaMetadataDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m mediaMetadataDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMediaMetadataDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m mediaMetadataDo) Unscoped() IMediaMetadataDo {
	return m.withDO(m.DO.Unscoped())
}

func (m mediaMetadataDo) Create(values ...*models.MediaMetadata) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

func (m mediaMetadataDo) CreateInBatches(values []*models.MediaMetadata, 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 mediaMetadataDo) Save(values ...*models.MediaMetadata) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Save(values)
}

func (m mediaMetadataDo) First() (*models.MediaMetadata, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaMetadata), nil
	}
}

func (m mediaMetadataDo) Take() (*models.MediaMetadata, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaMetadata), nil
	}
}

func (m mediaMetadataDo) Last() (*models.MediaMetadata, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaMetadata), nil
	}
}

func (m mediaMetadataDo) Find() ([]*models.MediaMetadata, error) {
	result, err := m.DO.Find()
	return result.([]*models.MediaMetadata), err
}

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

func (m mediaMetadataDo) Attrs(attrs ...field.AssignExpr) IMediaMetadataDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m mediaMetadataDo) Assign(attrs ...field.AssignExpr) IMediaMetadataDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m mediaMetadataDo) FirstOrInit() (*models.MediaMetadata, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaMetadata), nil
	}
}

func (m mediaMetadataDo) FirstOrCreate() (*models.MediaMetadata, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaMetadata), nil
	}
}

func (m mediaMetadataDo) FindByPage(offset int, limit int) (result []*models.MediaMetadata, 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 mediaMetadataDo) 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 mediaMetadataDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m mediaMetadataDo) Delete(models ...*models.MediaMetadata) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

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