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.

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