本站源代码
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.

220 lines
5.0KB

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