query_auto.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Code generated by gorm.io/gen. DO NOT EDIT.
  2. // Code generated by gorm.io/gen. DO NOT EDIT.
  3. // Code generated by gorm.io/gen. DO NOT EDIT.
  4. package model
  5. import (
  6. "context"
  7. "database/sql"
  8. "gorm.io/gorm"
  9. "gorm.io/gen"
  10. "gorm.io/plugin/dbresolver"
  11. )
  12. var (
  13. Q = new(Query)
  14. BookCategory *MBookCategory
  15. Font *MFont
  16. Group *MGroup
  17. Material *MMaterial
  18. Module *MModule
  19. Role *MRole
  20. User *MUser
  21. UserGroup *MUserGroup
  22. UserPoster *MUserPoster
  23. UserPosterBatch *MUserPosterBatch
  24. )
  25. func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
  26. *Q = *Use(db, opts...)
  27. BookCategory = &Q.BookCategory
  28. Font = &Q.Font
  29. Group = &Q.Group
  30. Material = &Q.Material
  31. Module = &Q.Module
  32. Role = &Q.Role
  33. User = &Q.User
  34. UserGroup = &Q.UserGroup
  35. UserPoster = &Q.UserPoster
  36. UserPosterBatch = &Q.UserPosterBatch
  37. }
  38. func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
  39. return &Query{
  40. db: db,
  41. BookCategory: newBookCategory(db, opts...),
  42. Font: newFont(db, opts...),
  43. Group: newGroup(db, opts...),
  44. Material: newMaterial(db, opts...),
  45. Module: newModule(db, opts...),
  46. Role: newRole(db, opts...),
  47. User: newUser(db, opts...),
  48. UserGroup: newUserGroup(db, opts...),
  49. UserPoster: newUserPoster(db, opts...),
  50. UserPosterBatch: newUserPosterBatch(db, opts...),
  51. }
  52. }
  53. type Query struct {
  54. db *gorm.DB
  55. BookCategory MBookCategory
  56. Font MFont
  57. Group MGroup
  58. Material MMaterial
  59. Module MModule
  60. Role MRole
  61. User MUser
  62. UserGroup MUserGroup
  63. UserPoster MUserPoster
  64. UserPosterBatch MUserPosterBatch
  65. }
  66. func (q *Query) Available() bool { return q.db != nil }
  67. func (q *Query) clone(db *gorm.DB) *Query {
  68. return &Query{
  69. db: db,
  70. BookCategory: q.BookCategory.clone(db),
  71. Font: q.Font.clone(db),
  72. Group: q.Group.clone(db),
  73. Material: q.Material.clone(db),
  74. Module: q.Module.clone(db),
  75. Role: q.Role.clone(db),
  76. User: q.User.clone(db),
  77. UserGroup: q.UserGroup.clone(db),
  78. UserPoster: q.UserPoster.clone(db),
  79. UserPosterBatch: q.UserPosterBatch.clone(db),
  80. }
  81. }
  82. func (q *Query) ReadDB() *Query {
  83. return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
  84. }
  85. func (q *Query) WriteDB() *Query {
  86. return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
  87. }
  88. func (q *Query) ReplaceDB(db *gorm.DB) *Query {
  89. return &Query{
  90. db: db,
  91. BookCategory: q.BookCategory.replaceDB(db),
  92. Font: q.Font.replaceDB(db),
  93. Group: q.Group.replaceDB(db),
  94. Material: q.Material.replaceDB(db),
  95. Module: q.Module.replaceDB(db),
  96. Role: q.Role.replaceDB(db),
  97. User: q.User.replaceDB(db),
  98. UserGroup: q.UserGroup.replaceDB(db),
  99. UserPoster: q.UserPoster.replaceDB(db),
  100. UserPosterBatch: q.UserPosterBatch.replaceDB(db),
  101. }
  102. }
  103. type queryCtx struct {
  104. BookCategory IBookCategoryDo
  105. Font IFontDo
  106. Group IGroupDo
  107. Material IMaterialDo
  108. Module IModuleDo
  109. Role IRoleDo
  110. User IUserDo
  111. UserGroup IUserGroupDo
  112. UserPoster IUserPosterDo
  113. UserPosterBatch IUserPosterBatchDo
  114. }
  115. func (q *Query) WithContext(ctx context.Context) *queryCtx {
  116. return &queryCtx{
  117. BookCategory: q.BookCategory.WithContext(ctx),
  118. Font: q.Font.WithContext(ctx),
  119. Group: q.Group.WithContext(ctx),
  120. Material: q.Material.WithContext(ctx),
  121. Module: q.Module.WithContext(ctx),
  122. Role: q.Role.WithContext(ctx),
  123. User: q.User.WithContext(ctx),
  124. UserGroup: q.UserGroup.WithContext(ctx),
  125. UserPoster: q.UserPoster.WithContext(ctx),
  126. UserPosterBatch: q.UserPosterBatch.WithContext(ctx),
  127. }
  128. }
  129. func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
  130. return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
  131. }
  132. func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
  133. return &QueryTx{q.clone(q.db.Begin(opts...))}
  134. }
  135. type QueryTx struct{ *Query }
  136. func (q *QueryTx) Commit() error {
  137. return q.db.Commit().Error
  138. }
  139. func (q *QueryTx) Rollback() error {
  140. return q.db.Rollback().Error
  141. }
  142. func (q *QueryTx) SavePoint(name string) error {
  143. return q.db.SavePoint(name).Error
  144. }
  145. func (q *QueryTx) RollbackTo(name string) error {
  146. return q.db.RollbackTo(name).Error
  147. }