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

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:                db,
		AccessToken:       newAccessToken(db, opts...),
		Cast:              newCast(db, opts...),
		Character:         newCharacter(db, opts...),
		CharacterSubjects: newCharacterSubjects(db, opts...),
		EpCollection:      newEpCollection(db, opts...),
		Episode:           newEpisode(db, opts...),
		Friend:            newFriend(db, opts...),
		Index:             newIndex(db, opts...),
		IndexCollect:      newIndexCollect(db, opts...),
		IndexSubject:      newIndexSubject(db, opts...),
		Member:            newMember(db, opts...),
		Notification:      newNotification(db, opts...),
		NotificationField: newNotificationField(db, opts...),
		Person:            newPerson(db, opts...),
		PersonCollect:     newPersonCollect(db, opts...),
		PersonField:       newPersonField(db, opts...),
		PersonSubjects:    newPersonSubjects(db, opts...),
		PrivateMessage:    newPrivateMessage(db, opts...),
		RevisionHistory:   newRevisionHistory(db, opts...),
		RevisionText:      newRevisionText(db, opts...),
		Subject:           newSubject(db, opts...),
		SubjectCollection: newSubjectCollection(db, opts...),
		SubjectField:      newSubjectField(db, opts...),
		SubjectRelation:   newSubjectRelation(db, opts...),
		SubjectRevision:   newSubjectRevision(db, opts...),
		TagIndex:          newTagIndex(db, opts...),
		TagList:           newTagList(db, opts...),
		UserGroup:         newUserGroup(db, opts...),
		WebSession:        newWebSession(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	AccessToken       accessToken
	Cast              cast
	Character         character
	CharacterSubjects characterSubjects
	EpCollection      epCollection
	Episode           episode
	Friend            friend
	Index             index
	IndexCollect      indexCollect
	IndexSubject      indexSubject
	Member            member
	Notification      notification
	NotificationField notificationField
	Person            person
	PersonCollect     personCollect
	PersonField       personField
	PersonSubjects    personSubjects
	PrivateMessage    privateMessage
	RevisionHistory   revisionHistory
	RevisionText      revisionText
	Subject           subject
	SubjectCollection subjectCollection
	SubjectField      subjectField
	SubjectRelation   subjectRelation
	SubjectRevision   subjectRevision
	TagIndex          tagIndex
	TagList           tagList
	UserGroup         userGroup
	WebSession        webSession
}

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

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:                db,
		AccessToken:       q.AccessToken.clone(db),
		Cast:              q.Cast.clone(db),
		Character:         q.Character.clone(db),
		CharacterSubjects: q.CharacterSubjects.clone(db),
		EpCollection:      q.EpCollection.clone(db),
		Episode:           q.Episode.clone(db),
		Friend:            q.Friend.clone(db),
		Index:             q.Index.clone(db),
		IndexCollect:      q.IndexCollect.clone(db),
		IndexSubject:      q.IndexSubject.clone(db),
		Member:            q.Member.clone(db),
		Notification:      q.Notification.clone(db),
		NotificationField: q.NotificationField.clone(db),
		Person:            q.Person.clone(db),
		PersonCollect:     q.PersonCollect.clone(db),
		PersonField:       q.PersonField.clone(db),
		PersonSubjects:    q.PersonSubjects.clone(db),
		PrivateMessage:    q.PrivateMessage.clone(db),
		RevisionHistory:   q.RevisionHistory.clone(db),
		RevisionText:      q.RevisionText.clone(db),
		Subject:           q.Subject.clone(db),
		SubjectCollection: q.SubjectCollection.clone(db),
		SubjectField:      q.SubjectField.clone(db),
		SubjectRelation:   q.SubjectRelation.clone(db),
		SubjectRevision:   q.SubjectRevision.clone(db),
		TagIndex:          q.TagIndex.clone(db),
		TagList:           q.TagList.clone(db),
		UserGroup:         q.UserGroup.clone(db),
		WebSession:        q.WebSession.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,
		AccessToken:       q.AccessToken.replaceDB(db),
		Cast:              q.Cast.replaceDB(db),
		Character:         q.Character.replaceDB(db),
		CharacterSubjects: q.CharacterSubjects.replaceDB(db),
		EpCollection:      q.EpCollection.replaceDB(db),
		Episode:           q.Episode.replaceDB(db),
		Friend:            q.Friend.replaceDB(db),
		Index:             q.Index.replaceDB(db),
		IndexCollect:      q.IndexCollect.replaceDB(db),
		IndexSubject:      q.IndexSubject.replaceDB(db),
		Member:            q.Member.replaceDB(db),
		Notification:      q.Notification.replaceDB(db),
		NotificationField: q.NotificationField.replaceDB(db),
		Person:            q.Person.replaceDB(db),
		PersonCollect:     q.PersonCollect.replaceDB(db),
		PersonField:       q.PersonField.replaceDB(db),
		PersonSubjects:    q.PersonSubjects.replaceDB(db),
		PrivateMessage:    q.PrivateMessage.replaceDB(db),
		RevisionHistory:   q.RevisionHistory.replaceDB(db),
		RevisionText:      q.RevisionText.replaceDB(db),
		Subject:           q.Subject.replaceDB(db),
		SubjectCollection: q.SubjectCollection.replaceDB(db),
		SubjectField:      q.SubjectField.replaceDB(db),
		SubjectRelation:   q.SubjectRelation.replaceDB(db),
		SubjectRevision:   q.SubjectRevision.replaceDB(db),
		TagIndex:          q.TagIndex.replaceDB(db),
		TagList:           q.TagList.replaceDB(db),
		UserGroup:         q.UserGroup.replaceDB(db),
		WebSession:        q.WebSession.replaceDB(db),
	}
}

type queryCtx struct {
	AccessToken       *accessTokenDo
	Cast              *castDo
	Character         *characterDo
	CharacterSubjects *characterSubjectsDo
	EpCollection      *epCollectionDo
	Episode           *episodeDo
	Friend            *friendDo
	Index             *indexDo
	IndexCollect      *indexCollectDo
	IndexSubject      *indexSubjectDo
	Member            *memberDo
	Notification      *notificationDo
	NotificationField *notificationFieldDo
	Person            *personDo
	PersonCollect     *personCollectDo
	PersonField       *personFieldDo
	PersonSubjects    *personSubjectsDo
	PrivateMessage    *privateMessageDo
	RevisionHistory   *revisionHistoryDo
	RevisionText      *revisionTextDo
	Subject           *subjectDo
	SubjectCollection *subjectCollectionDo
	SubjectField      *subjectFieldDo
	SubjectRelation   *subjectRelationDo
	SubjectRevision   *subjectRevisionDo
	TagIndex          *tagIndexDo
	TagList           *tagListDo
	UserGroup         *userGroupDo
	WebSession        *webSessionDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		AccessToken:       q.AccessToken.WithContext(ctx),
		Cast:              q.Cast.WithContext(ctx),
		Character:         q.Character.WithContext(ctx),
		CharacterSubjects: q.CharacterSubjects.WithContext(ctx),
		EpCollection:      q.EpCollection.WithContext(ctx),
		Episode:           q.Episode.WithContext(ctx),
		Friend:            q.Friend.WithContext(ctx),
		Index:             q.Index.WithContext(ctx),
		IndexCollect:      q.IndexCollect.WithContext(ctx),
		IndexSubject:      q.IndexSubject.WithContext(ctx),
		Member:            q.Member.WithContext(ctx),
		Notification:      q.Notification.WithContext(ctx),
		NotificationField: q.NotificationField.WithContext(ctx),
		Person:            q.Person.WithContext(ctx),
		PersonCollect:     q.PersonCollect.WithContext(ctx),
		PersonField:       q.PersonField.WithContext(ctx),
		PersonSubjects:    q.PersonSubjects.WithContext(ctx),
		PrivateMessage:    q.PrivateMessage.WithContext(ctx),
		RevisionHistory:   q.RevisionHistory.WithContext(ctx),
		RevisionText:      q.RevisionText.WithContext(ctx),
		Subject:           q.Subject.WithContext(ctx),
		SubjectCollection: q.SubjectCollection.WithContext(ctx),
		SubjectField:      q.SubjectField.WithContext(ctx),
		SubjectRelation:   q.SubjectRelation.WithContext(ctx),
		SubjectRevision:   q.SubjectRevision.WithContext(ctx),
		TagIndex:          q.TagIndex.WithContext(ctx),
		TagList:           q.TagList.WithContext(ctx),
		UserGroup:         q.UserGroup.WithContext(ctx),
		WebSession:        q.WebSession.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
}
