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

	"gorm.io/plugin/dbresolver"
)

var (
	Q               = new(Query)
	Files           *files
	LibrarySection  *librarySection
	MediaItem       *mediaItem
	MediaPart       *mediaPart
	MetadataItem    *metadataItem
	PathDownload    *pathDownload
	SectionLocation *sectionLocation
	Tag             *tag
	Tagging         *tagging
	User            *user
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	Files = &Q.Files
	LibrarySection = &Q.LibrarySection
	MediaItem = &Q.MediaItem
	MediaPart = &Q.MediaPart
	MetadataItem = &Q.MetadataItem
	PathDownload = &Q.PathDownload
	SectionLocation = &Q.SectionLocation
	Tag = &Q.Tag
	Tagging = &Q.Tagging
	User = &Q.User
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:              db,
		Files:           newFiles(db, opts...),
		LibrarySection:  newLibrarySection(db, opts...),
		MediaItem:       newMediaItem(db, opts...),
		MediaPart:       newMediaPart(db, opts...),
		MetadataItem:    newMetadataItem(db, opts...),
		PathDownload:    newPathDownload(db, opts...),
		SectionLocation: newSectionLocation(db, opts...),
		Tag:             newTag(db, opts...),
		Tagging:         newTagging(db, opts...),
		User:            newUser(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	Files           files
	LibrarySection  librarySection
	MediaItem       mediaItem
	MediaPart       mediaPart
	MetadataItem    metadataItem
	PathDownload    pathDownload
	SectionLocation sectionLocation
	Tag             tag
	Tagging         tagging
	User            user
}

func (q *Query) Available() bool { return q.db != nil }

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:              db,
		Files:           q.Files.clone(db),
		LibrarySection:  q.LibrarySection.clone(db),
		MediaItem:       q.MediaItem.clone(db),
		MediaPart:       q.MediaPart.clone(db),
		MetadataItem:    q.MetadataItem.clone(db),
		PathDownload:    q.PathDownload.clone(db),
		SectionLocation: q.SectionLocation.clone(db),
		Tag:             q.Tag.clone(db),
		Tagging:         q.Tagging.clone(db),
		User:            q.User.clone(db),
	}
}

func (q *Query) ReadDB() *Query {
	return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}

func (q *Query) WriteDB() *Query {
	return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}

func (q *Query) ReplaceDB(db *gorm.DB) *Query {
	return &Query{
		db:              db,
		Files:           q.Files.replaceDB(db),
		LibrarySection:  q.LibrarySection.replaceDB(db),
		MediaItem:       q.MediaItem.replaceDB(db),
		MediaPart:       q.MediaPart.replaceDB(db),
		MetadataItem:    q.MetadataItem.replaceDB(db),
		PathDownload:    q.PathDownload.replaceDB(db),
		SectionLocation: q.SectionLocation.replaceDB(db),
		Tag:             q.Tag.replaceDB(db),
		Tagging:         q.Tagging.replaceDB(db),
		User:            q.User.replaceDB(db),
	}
}

type queryCtx struct {
	Files           IFilesDo
	LibrarySection  ILibrarySectionDo
	MediaItem       IMediaItemDo
	MediaPart       IMediaPartDo
	MetadataItem    IMetadataItemDo
	PathDownload    IPathDownloadDo
	SectionLocation ISectionLocationDo
	Tag             ITagDo
	Tagging         ITaggingDo
	User            IUserDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		Files:           q.Files.WithContext(ctx),
		LibrarySection:  q.LibrarySection.WithContext(ctx),
		MediaItem:       q.MediaItem.WithContext(ctx),
		MediaPart:       q.MediaPart.WithContext(ctx),
		MetadataItem:    q.MetadataItem.WithContext(ctx),
		PathDownload:    q.PathDownload.WithContext(ctx),
		SectionLocation: q.SectionLocation.WithContext(ctx),
		Tag:             q.Tag.WithContext(ctx),
		Tagging:         q.Tagging.WithContext(ctx),
		User:            q.User.WithContext(ctx),
	}
}

func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
	return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}

func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
	tx := q.db.Begin(opts...)
	return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}

type QueryTx struct {
	*Query
	Error error
}

func (q *QueryTx) Commit() error {
	return q.db.Commit().Error
}

func (q *QueryTx) Rollback() error {
	return q.db.Rollback().Error
}

func (q *QueryTx) SavePoint(name string) error {
	return q.db.SavePoint(name).Error
}

func (q *QueryTx) RollbackTo(name string) error {
	return q.db.RollbackTo(name).Error
}
