Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

223 lines
5.1 KiB

  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "context"
  7. "time"
  8. "xorm.io/xorm/caches"
  9. "xorm.io/xorm/dialects"
  10. "xorm.io/xorm/log"
  11. "xorm.io/xorm/names"
  12. )
  13. // EngineGroup defines an engine group
  14. type EngineGroup struct {
  15. *Engine
  16. slaves []*Engine
  17. policy GroupPolicy
  18. }
  19. // NewEngineGroup creates a new engine group
  20. func NewEngineGroup(args1 interface{}, args2 interface{}, policies ...GroupPolicy) (*EngineGroup, error) {
  21. var eg EngineGroup
  22. if len(policies) > 0 {
  23. eg.policy = policies[0]
  24. } else {
  25. eg.policy = RoundRobinPolicy()
  26. }
  27. driverName, ok1 := args1.(string)
  28. conns, ok2 := args2.([]string)
  29. if ok1 && ok2 {
  30. engines := make([]*Engine, len(conns))
  31. for i, conn := range conns {
  32. engine, err := NewEngine(driverName, conn)
  33. if err != nil {
  34. return nil, err
  35. }
  36. engine.engineGroup = &eg
  37. engines[i] = engine
  38. }
  39. eg.Engine = engines[0]
  40. eg.slaves = engines[1:]
  41. return &eg, nil
  42. }
  43. master, ok3 := args1.(*Engine)
  44. slaves, ok4 := args2.([]*Engine)
  45. if ok3 && ok4 {
  46. master.engineGroup = &eg
  47. for i := 0; i < len(slaves); i++ {
  48. slaves[i].engineGroup = &eg
  49. }
  50. eg.Engine = master
  51. eg.slaves = slaves
  52. return &eg, nil
  53. }
  54. return nil, ErrParamsType
  55. }
  56. // Close the engine
  57. func (eg *EngineGroup) Close() error {
  58. err := eg.Engine.Close()
  59. if err != nil {
  60. return err
  61. }
  62. for i := 0; i < len(eg.slaves); i++ {
  63. err := eg.slaves[i].Close()
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. return nil
  69. }
  70. // Context returned a group session
  71. func (eg *EngineGroup) Context(ctx context.Context) *Session {
  72. sess := eg.NewSession()
  73. sess.isAutoClose = true
  74. return sess.Context(ctx)
  75. }
  76. // NewSession returned a group session
  77. func (eg *EngineGroup) NewSession() *Session {
  78. sess := eg.Engine.NewSession()
  79. sess.sessionType = groupSession
  80. return sess
  81. }
  82. // Master returns the master engine
  83. func (eg *EngineGroup) Master() *Engine {
  84. return eg.Engine
  85. }
  86. // Ping tests if database is alive
  87. func (eg *EngineGroup) Ping() error {
  88. if err := eg.Engine.Ping(); err != nil {
  89. return err
  90. }
  91. for _, slave := range eg.slaves {
  92. if err := slave.Ping(); err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }
  98. // SetColumnMapper set the column name mapping rule
  99. func (eg *EngineGroup) SetColumnMapper(mapper names.Mapper) {
  100. eg.Engine.SetColumnMapper(mapper)
  101. for i := 0; i < len(eg.slaves); i++ {
  102. eg.slaves[i].SetColumnMapper(mapper)
  103. }
  104. }
  105. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
  106. func (eg *EngineGroup) SetConnMaxLifetime(d time.Duration) {
  107. eg.Engine.SetConnMaxLifetime(d)
  108. for i := 0; i < len(eg.slaves); i++ {
  109. eg.slaves[i].SetConnMaxLifetime(d)
  110. }
  111. }
  112. // SetDefaultCacher set the default cacher
  113. func (eg *EngineGroup) SetDefaultCacher(cacher caches.Cacher) {
  114. eg.Engine.SetDefaultCacher(cacher)
  115. for i := 0; i < len(eg.slaves); i++ {
  116. eg.slaves[i].SetDefaultCacher(cacher)
  117. }
  118. }
  119. // SetLogger set the new logger
  120. func (eg *EngineGroup) SetLogger(logger interface{}) {
  121. eg.Engine.SetLogger(logger)
  122. for i := 0; i < len(eg.slaves); i++ {
  123. eg.slaves[i].SetLogger(logger)
  124. }
  125. }
  126. // SetLogLevel sets the logger level
  127. func (eg *EngineGroup) SetLogLevel(level log.LogLevel) {
  128. eg.Engine.SetLogLevel(level)
  129. for i := 0; i < len(eg.slaves); i++ {
  130. eg.slaves[i].SetLogLevel(level)
  131. }
  132. }
  133. // SetMapper set the name mapping rules
  134. func (eg *EngineGroup) SetMapper(mapper names.Mapper) {
  135. eg.Engine.SetMapper(mapper)
  136. for i := 0; i < len(eg.slaves); i++ {
  137. eg.slaves[i].SetMapper(mapper)
  138. }
  139. }
  140. // SetMaxIdleConns set the max idle connections on pool, default is 2
  141. func (eg *EngineGroup) SetMaxIdleConns(conns int) {
  142. eg.Engine.DB().SetMaxIdleConns(conns)
  143. for i := 0; i < len(eg.slaves); i++ {
  144. eg.slaves[i].DB().SetMaxIdleConns(conns)
  145. }
  146. }
  147. // SetMaxOpenConns is only available for go 1.2+
  148. func (eg *EngineGroup) SetMaxOpenConns(conns int) {
  149. eg.Engine.DB().SetMaxOpenConns(conns)
  150. for i := 0; i < len(eg.slaves); i++ {
  151. eg.slaves[i].DB().SetMaxOpenConns(conns)
  152. }
  153. }
  154. // SetPolicy set the group policy
  155. func (eg *EngineGroup) SetPolicy(policy GroupPolicy) *EngineGroup {
  156. eg.policy = policy
  157. return eg
  158. }
  159. // SetQuotePolicy sets the special quote policy
  160. func (eg *EngineGroup) SetQuotePolicy(quotePolicy dialects.QuotePolicy) {
  161. eg.Engine.SetQuotePolicy(quotePolicy)
  162. for i := 0; i < len(eg.slaves); i++ {
  163. eg.slaves[i].SetQuotePolicy(quotePolicy)
  164. }
  165. }
  166. // SetTableMapper set the table name mapping rule
  167. func (eg *EngineGroup) SetTableMapper(mapper names.Mapper) {
  168. eg.Engine.SetTableMapper(mapper)
  169. for i := 0; i < len(eg.slaves); i++ {
  170. eg.slaves[i].SetTableMapper(mapper)
  171. }
  172. }
  173. // ShowSQL show SQL statement or not on logger if log level is great than INFO
  174. func (eg *EngineGroup) ShowSQL(show ...bool) {
  175. eg.Engine.ShowSQL(show...)
  176. for i := 0; i < len(eg.slaves); i++ {
  177. eg.slaves[i].ShowSQL(show...)
  178. }
  179. }
  180. // Slave returns one of the physical databases which is a slave according the policy
  181. func (eg *EngineGroup) Slave() *Engine {
  182. switch len(eg.slaves) {
  183. case 0:
  184. return eg.Engine
  185. case 1:
  186. return eg.slaves[0]
  187. }
  188. return eg.policy.Slave(eg)
  189. }
  190. // Slaves returns all the slaves
  191. func (eg *EngineGroup) Slaves() []*Engine {
  192. return eg.slaves
  193. }