123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package model
- import (
- "context"
- "database/sql"
- "gorm.io/gorm"
- "gorm.io/gen"
- "gorm.io/plugin/dbresolver"
- )
- var (
- Q = new(Query)
- BookCategory *MBookCategory
- Font *MFont
- Group *MGroup
- Material *MMaterial
- Module *MModule
- Role *MRole
- User *MUser
- UserGroup *MUserGroup
- UserPoster *MUserPoster
- UserPosterBatch *MUserPosterBatch
- )
- func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
- *Q = *Use(db, opts...)
- BookCategory = &Q.BookCategory
- Font = &Q.Font
- Group = &Q.Group
- Material = &Q.Material
- Module = &Q.Module
- Role = &Q.Role
- User = &Q.User
- UserGroup = &Q.UserGroup
- UserPoster = &Q.UserPoster
- UserPosterBatch = &Q.UserPosterBatch
- }
- func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
- return &Query{
- db: db,
- BookCategory: newBookCategory(db, opts...),
- Font: newFont(db, opts...),
- Group: newGroup(db, opts...),
- Material: newMaterial(db, opts...),
- Module: newModule(db, opts...),
- Role: newRole(db, opts...),
- User: newUser(db, opts...),
- UserGroup: newUserGroup(db, opts...),
- UserPoster: newUserPoster(db, opts...),
- UserPosterBatch: newUserPosterBatch(db, opts...),
- }
- }
- type Query struct {
- db *gorm.DB
- BookCategory MBookCategory
- Font MFont
- Group MGroup
- Material MMaterial
- Module MModule
- Role MRole
- User MUser
- UserGroup MUserGroup
- UserPoster MUserPoster
- UserPosterBatch MUserPosterBatch
- }
- func (q *Query) Available() bool { return q.db != nil }
- func (q *Query) clone(db *gorm.DB) *Query {
- return &Query{
- db: db,
- BookCategory: q.BookCategory.clone(db),
- Font: q.Font.clone(db),
- Group: q.Group.clone(db),
- Material: q.Material.clone(db),
- Module: q.Module.clone(db),
- Role: q.Role.clone(db),
- User: q.User.clone(db),
- UserGroup: q.UserGroup.clone(db),
- UserPoster: q.UserPoster.clone(db),
- UserPosterBatch: q.UserPosterBatch.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,
- BookCategory: q.BookCategory.replaceDB(db),
- Font: q.Font.replaceDB(db),
- Group: q.Group.replaceDB(db),
- Material: q.Material.replaceDB(db),
- Module: q.Module.replaceDB(db),
- Role: q.Role.replaceDB(db),
- User: q.User.replaceDB(db),
- UserGroup: q.UserGroup.replaceDB(db),
- UserPoster: q.UserPoster.replaceDB(db),
- UserPosterBatch: q.UserPosterBatch.replaceDB(db),
- }
- }
- type queryCtx struct {
- BookCategory IBookCategoryDo
- Font IFontDo
- Group IGroupDo
- Material IMaterialDo
- Module IModuleDo
- Role IRoleDo
- User IUserDo
- UserGroup IUserGroupDo
- UserPoster IUserPosterDo
- UserPosterBatch IUserPosterBatchDo
- }
- func (q *Query) WithContext(ctx context.Context) *queryCtx {
- return &queryCtx{
- BookCategory: q.BookCategory.WithContext(ctx),
- Font: q.Font.WithContext(ctx),
- Group: q.Group.WithContext(ctx),
- Material: q.Material.WithContext(ctx),
- Module: q.Module.WithContext(ctx),
- Role: q.Role.WithContext(ctx),
- User: q.User.WithContext(ctx),
- UserGroup: q.UserGroup.WithContext(ctx),
- UserPoster: q.UserPoster.WithContext(ctx),
- UserPosterBatch: q.UserPosterBatch.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 {
- return &QueryTx{q.clone(q.db.Begin(opts...))}
- }
- type QueryTx struct{ *Query }
- 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
- }
|