base.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package base
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gorm.io/gorm"
  7. )
  8. var globalIsRelated bool = true // 全局预加载
  9. // prepare for other
  10. type BaseMgr struct {
  11. *gorm.DB
  12. Ctx context.Context
  13. cancel context.CancelFunc
  14. timeout time.Duration
  15. isRelated bool
  16. }
  17. // SetTimeOut set timeout
  18. func (obj *BaseMgr) SetTimeOut(timeout time.Duration) {
  19. obj.Ctx, obj.cancel = context.WithTimeout(context.Background(), timeout)
  20. obj.timeout = timeout
  21. }
  22. // SetCtx set context
  23. func (obj *BaseMgr) SetCtx(c context.Context) {
  24. if c != nil {
  25. obj.Ctx = c
  26. }
  27. }
  28. // GetCtx get context
  29. func (obj *BaseMgr) GetCtx() context.Context {
  30. return obj.Ctx
  31. }
  32. // Cancel cancel context
  33. func (obj *BaseMgr) Cancel(c context.Context) {
  34. obj.cancel()
  35. }
  36. // GetDB get gorm.DB info
  37. func (obj *BaseMgr) GetDB() *gorm.DB {
  38. return obj.DB
  39. }
  40. // UpdateDB update gorm.DB info
  41. func (obj *BaseMgr) UpdateDB(db *gorm.DB) {
  42. obj.DB = db
  43. }
  44. // GetIsRelated Query foreign key Association.获取是否查询外键关联(gorm.Related)
  45. func (obj *BaseMgr) GetIsRelated() bool {
  46. return obj.isRelated
  47. }
  48. // SetIsRelated Query foreign key Association.设置是否查询外键关联(gorm.Related)
  49. func (obj *BaseMgr) SetIsRelated(b bool) {
  50. obj.isRelated = b
  51. }
  52. // New new gorm.新gorm,重置条件
  53. func (obj *BaseMgr) New() {
  54. obj.DB = obj.NewDB()
  55. }
  56. // NewDB new gorm.新gorm
  57. func (obj *BaseMgr) NewDB() *gorm.DB {
  58. return obj.DB.Session(&gorm.Session{NewDB: true, Context: obj.Ctx})
  59. }
  60. type Options struct {
  61. Query map[string]interface{}
  62. }
  63. // Option overrides behavior of Connect.
  64. type Option interface {
  65. Apply(*Options)
  66. }
  67. type OptionFunc func(*Options)
  68. func (f OptionFunc) apply(o *Options) {
  69. f(o)
  70. }
  71. // OpenRelated 打开全局预加载
  72. func OpenRelated() {
  73. globalIsRelated = true
  74. }
  75. // CloseRelated 关闭全局预加载
  76. func CloseRelated() {
  77. globalIsRelated = true
  78. }
  79. // 自定义sql查询
  80. type Condition struct {
  81. list []*conditionInfo
  82. }
  83. func (c *Condition) AndWithCondition(condition bool, column string, cases string, value interface{}) *Condition {
  84. if condition {
  85. c.list = append(c.list, &conditionInfo{
  86. andor: "and",
  87. column: column, // 列名
  88. case_: cases, // 条件(and,or,in,>=,<=)
  89. value: value,
  90. })
  91. }
  92. return c
  93. }
  94. // And a Condition by and .and 一个条件
  95. func (c *Condition) And(column string, cases string, value interface{}) *Condition {
  96. return c.AndWithCondition(true, column, cases, value)
  97. }
  98. func (c *Condition) OrWithCondition(condition bool, column string, cases string, value interface{}) *Condition {
  99. if condition {
  100. c.list = append(c.list, &conditionInfo{
  101. andor: "or",
  102. column: column, // 列名
  103. case_: cases, // 条件(and,or,in,>=,<=)
  104. value: value,
  105. })
  106. }
  107. return c
  108. }
  109. // Or a Condition by or .or 一个条件
  110. func (c *Condition) Or(column string, cases string, value interface{}) *Condition {
  111. return c.OrWithCondition(true, column, cases, value)
  112. }
  113. func (c *Condition) Get() (where string, out []interface{}) {
  114. firstAnd := -1
  115. for i := 0; i < len(c.list); i++ { // 查找第一个and
  116. if c.list[i].andor == "and" {
  117. where = fmt.Sprintf("`%v` %v ?", c.list[i].column, c.list[i].case_)
  118. out = append(out, c.list[i].value)
  119. firstAnd = i
  120. break
  121. }
  122. }
  123. if firstAnd < 0 && len(c.list) > 0 { // 补刀
  124. where = fmt.Sprintf("`%v` %v ?", c.list[0].column, c.list[0].case_)
  125. out = append(out, c.list[0].value)
  126. firstAnd = 0
  127. }
  128. for i := 0; i < len(c.list); i++ { // 添加剩余的
  129. if firstAnd != i {
  130. where += fmt.Sprintf(" %v `%v` %v ?", c.list[i].andor, c.list[i].column, c.list[i].case_)
  131. out = append(out, c.list[i].value)
  132. }
  133. }
  134. return
  135. }
  136. type conditionInfo struct {
  137. andor string
  138. column string // 列名
  139. case_ string // 条件(in,>=,<=)
  140. value interface{}
  141. }