// 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)
	AdTemplate        *adTemplate
	Domain            *domain
	Job               *job
	Notification      *notification
	Payment           *payment
	Plan              *plan
	PlanSubscription  *planSubscription
	PlayerConfig      *playerConfig
	User              *user
	UserPreference    *userPreference
	Video             *video
	WalletTransaction *walletTransaction
)

func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
	*Q = *Use(db, opts...)
	AdTemplate = &Q.AdTemplate
	Domain = &Q.Domain
	Job = &Q.Job
	Notification = &Q.Notification
	Payment = &Q.Payment
	Plan = &Q.Plan
	PlanSubscription = &Q.PlanSubscription
	PlayerConfig = &Q.PlayerConfig
	User = &Q.User
	UserPreference = &Q.UserPreference
	Video = &Q.Video
	WalletTransaction = &Q.WalletTransaction
}

func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
	return &Query{
		db:                db,
		AdTemplate:        newAdTemplate(db, opts...),
		Domain:            newDomain(db, opts...),
		Job:               newJob(db, opts...),
		Notification:      newNotification(db, opts...),
		Payment:           newPayment(db, opts...),
		Plan:              newPlan(db, opts...),
		PlanSubscription:  newPlanSubscription(db, opts...),
		PlayerConfig:      newPlayerConfig(db, opts...),
		User:              newUser(db, opts...),
		UserPreference:    newUserPreference(db, opts...),
		Video:             newVideo(db, opts...),
		WalletTransaction: newWalletTransaction(db, opts...),
	}
}

type Query struct {
	db *gorm.DB

	AdTemplate        adTemplate
	Domain            domain
	Job               job
	Notification      notification
	Payment           payment
	Plan              plan
	PlanSubscription  planSubscription
	PlayerConfig      playerConfig
	User              user
	UserPreference    userPreference
	Video             video
	WalletTransaction walletTransaction
}

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

func (q *Query) clone(db *gorm.DB) *Query {
	return &Query{
		db:                db,
		AdTemplate:        q.AdTemplate.clone(db),
		Domain:            q.Domain.clone(db),
		Job:               q.Job.clone(db),
		Notification:      q.Notification.clone(db),
		Payment:           q.Payment.clone(db),
		Plan:              q.Plan.clone(db),
		PlanSubscription:  q.PlanSubscription.clone(db),
		PlayerConfig:      q.PlayerConfig.clone(db),
		User:              q.User.clone(db),
		UserPreference:    q.UserPreference.clone(db),
		Video:             q.Video.clone(db),
		WalletTransaction: q.WalletTransaction.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,
		AdTemplate:        q.AdTemplate.replaceDB(db),
		Domain:            q.Domain.replaceDB(db),
		Job:               q.Job.replaceDB(db),
		Notification:      q.Notification.replaceDB(db),
		Payment:           q.Payment.replaceDB(db),
		Plan:              q.Plan.replaceDB(db),
		PlanSubscription:  q.PlanSubscription.replaceDB(db),
		PlayerConfig:      q.PlayerConfig.replaceDB(db),
		User:              q.User.replaceDB(db),
		UserPreference:    q.UserPreference.replaceDB(db),
		Video:             q.Video.replaceDB(db),
		WalletTransaction: q.WalletTransaction.replaceDB(db),
	}
}

type queryCtx struct {
	AdTemplate        IAdTemplateDo
	Domain            IDomainDo
	Job               IJobDo
	Notification      INotificationDo
	Payment           IPaymentDo
	Plan              IPlanDo
	PlanSubscription  IPlanSubscriptionDo
	PlayerConfig      IPlayerConfigDo
	User              IUserDo
	UserPreference    IUserPreferenceDo
	Video             IVideoDo
	WalletTransaction IWalletTransactionDo
}

func (q *Query) WithContext(ctx context.Context) *queryCtx {
	return &queryCtx{
		AdTemplate:        q.AdTemplate.WithContext(ctx),
		Domain:            q.Domain.WithContext(ctx),
		Job:               q.Job.WithContext(ctx),
		Notification:      q.Notification.WithContext(ctx),
		Payment:           q.Payment.WithContext(ctx),
		Plan:              q.Plan.WithContext(ctx),
		PlanSubscription:  q.PlanSubscription.WithContext(ctx),
		PlayerConfig:      q.PlayerConfig.WithContext(ctx),
		User:              q.User.WithContext(ctx),
		UserPreference:    q.UserPreference.WithContext(ctx),
		Video:             q.Video.WithContext(ctx),
		WalletTransaction: q.WalletTransaction.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
}
