db.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Pipe - A small and beautiful blogging platform written in golang.
  2. // Copyright (C) 2017-2018, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package service
  17. import (
  18. "fmt"
  19. "github.com/astaxie/beego"
  20. "github.com/jinzhu/gorm"
  21. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  22. )
  23. var readDb *gorm.DB
  24. var readDb2 *gorm.DB
  25. var writeDb *gorm.DB
  26. var readUserDb *gorm.DB
  27. var writeUserDb *gorm.DB
  28. var readMiddleDb *gorm.DB
  29. var writeMiddleDb *gorm.DB
  30. var readPatientDb *gorm.DB
  31. var writePatientDb *gorm.DB
  32. var readSgjPatientDb *gorm.DB
  33. var writeSgjPatientDb *gorm.DB
  34. var readUserDbT *gorm.DB
  35. var writeUserDbT *gorm.DB
  36. var err error
  37. func ConnectDB() {
  38. readHost := beego.AppConfig.String("readmysqlhost")
  39. readPort := beego.AppConfig.String("readmysqlport")
  40. readUser := beego.AppConfig.String("readmysqluser")
  41. readPass := beego.AppConfig.String("readmysqlpass")
  42. readName := beego.AppConfig.String("readmysqlname")
  43. readHost2 := beego.AppConfig.String("readmysqlhost2")
  44. readPort2 := beego.AppConfig.String("readmysqlport2")
  45. readUser2 := beego.AppConfig.String("readmysqluser2")
  46. readPass2 := beego.AppConfig.String("readmysqlpass2")
  47. readName2 := beego.AppConfig.String("readmysqlname2")
  48. writeHost := beego.AppConfig.String("writemysqlhost")
  49. writePort := beego.AppConfig.String("writemysqlport")
  50. writeUser := beego.AppConfig.String("writemysqluser")
  51. writePass := beego.AppConfig.String("writemysqlpass")
  52. writeName := beego.AppConfig.String("writemysqlname")
  53. readUserHost := beego.AppConfig.String("readuserhost")
  54. readUserPort := beego.AppConfig.String("readuserport")
  55. readUserUser := beego.AppConfig.String("readuseruser")
  56. readUserPass := beego.AppConfig.String("readuserpass")
  57. readUserName := beego.AppConfig.String("readusername")
  58. writeUserHost := beego.AppConfig.String("writeuserhost")
  59. writeUserPort := beego.AppConfig.String("writeuserport")
  60. writeUserUser := beego.AppConfig.String("writeuseruser")
  61. writeUserPass := beego.AppConfig.String("writeuserpass")
  62. writeUserName := beego.AppConfig.String("writeusername")
  63. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  64. readMiddlePort := beego.AppConfig.String("readmiddleport")
  65. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  66. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  67. readMiddleName := beego.AppConfig.String("readmiddlename")
  68. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  69. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  70. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  71. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  72. writeMiddleName := beego.AppConfig.String("writemiddlename")
  73. readPatientHost := beego.AppConfig.String("readpatienthost")
  74. readPatientPort := beego.AppConfig.String("readpatientport")
  75. readPatientUser := beego.AppConfig.String("readpatientuser")
  76. readPatientPass := beego.AppConfig.String("readpatientpass")
  77. readPatientName := beego.AppConfig.String("readpatientname")
  78. writePatientHost := beego.AppConfig.String("writepatienthost")
  79. writePatientPort := beego.AppConfig.String("writepatientport")
  80. writePatientUser := beego.AppConfig.String("writepatientuser")
  81. writePatientPass := beego.AppConfig.String("writepatientpass")
  82. writePatientName := beego.AppConfig.String("writepatientname")
  83. readSgjPatientHost := beego.AppConfig.String("readsgjpatientmysqlhost")
  84. readSgjPatientPort := beego.AppConfig.String("readsgjpatientmysqlport")
  85. readSgjPatientUser := beego.AppConfig.String("readsgjpatientmysqluser")
  86. readSgjPatientPass := beego.AppConfig.String("readsgjpatientmysqlpass")
  87. readSgjPatientName := beego.AppConfig.String("readsgjpatientmysqlname")
  88. writeSgjPatientHost := beego.AppConfig.String("writesgjpatientmysqlhost")
  89. writeSgjPatientPort := beego.AppConfig.String("writesgjpatientmysqlport")
  90. writeSgjPatientUser := beego.AppConfig.String("writesgjpatientmysqluser")
  91. writeSgjPatientPass := beego.AppConfig.String("writesgjpatientmysqlpass")
  92. writeSgjPatientName := beego.AppConfig.String("writesgjpatientmysqlname")
  93. readUserHostT := beego.AppConfig.String("readuserhostTest")
  94. readUserPortT := beego.AppConfig.String("readuserportTest")
  95. readUserUserT := beego.AppConfig.String("readuseruserTest")
  96. readUserPassT := beego.AppConfig.String("readuserpassTest")
  97. readUserNameT := beego.AppConfig.String("readusernameTest")
  98. writeUserHostT := beego.AppConfig.String("writeuserhostTest")
  99. writeUserPortT := beego.AppConfig.String("writeuserportTest")
  100. writeUserUserT := beego.AppConfig.String("writeuseruserTest")
  101. writeUserPassT := beego.AppConfig.String("writeuserpassTest")
  102. writeUserNameT := beego.AppConfig.String("writeusernameTest")
  103. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  104. rdsn2 := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser2, readPass2, readHost2, readPort2, readName2)
  105. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  106. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  107. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  108. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  109. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  110. rpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readPatientUser, readPatientPass, readPatientHost, readPatientPort, readPatientName)
  111. wpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writePatientUser, writePatientPass, writePatientHost, writePatientPort, writePatientName)
  112. rspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readSgjPatientUser, readSgjPatientPass, readSgjPatientHost, readSgjPatientPort, readSgjPatientName)
  113. wspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeSgjPatientUser, writeSgjPatientPass, writeSgjPatientHost, writeSgjPatientPort, writeSgjPatientName)
  114. rudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUserT, readUserPassT, readUserHostT, readUserPortT, readUserNameT)
  115. wudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUserT, writeUserPassT, writeUserHostT, writeUserPortT, writeUserNameT)
  116. readDb2, err = gorm.Open("mysql", rdsn2)
  117. readDb, err = gorm.Open("mysql", rdsn)
  118. if err != nil {
  119. //beego.Error(err)
  120. }
  121. readDb.DB().SetMaxIdleConns(10)
  122. readDb.DB().SetMaxOpenConns(100)
  123. readDb.LogMode(true)
  124. readDb2.DB().SetMaxIdleConns(10)
  125. readDb2.DB().SetMaxOpenConns(100)
  126. readDb2.LogMode(true)
  127. writeDb, err = gorm.Open("mysql", wdsn)
  128. if err != nil {
  129. //beego.Error(err)
  130. }
  131. writeDb.DB().SetMaxIdleConns(10)
  132. writeDb.DB().SetMaxOpenConns(100)
  133. writeDb.LogMode(true)
  134. readUserDbT, err = gorm.Open("mysql", rudsnT)
  135. if err != nil {
  136. //beego.Error(err)
  137. }
  138. readUserDbT.DB().SetMaxIdleConns(10)
  139. readUserDbT.DB().SetMaxOpenConns(100)
  140. readUserDbT.LogMode(true)
  141. writeUserDbT, err = gorm.Open("mysql", wudsnT)
  142. if err != nil {
  143. //beego.Error(err)
  144. }
  145. writeUserDbT.DB().SetMaxIdleConns(10)
  146. writeUserDbT.DB().SetMaxOpenConns(100)
  147. writeUserDbT.LogMode(true)
  148. readUserDb, err = gorm.Open("mysql", rudsn)
  149. if err != nil {
  150. //beego.Error(err)
  151. }
  152. readUserDb.DB().SetMaxIdleConns(10)
  153. readUserDb.DB().SetMaxOpenConns(100)
  154. readUserDb.LogMode(true)
  155. writeUserDb, err = gorm.Open("mysql", wudsn)
  156. if err != nil {
  157. //beego.Error(err)
  158. }
  159. writeUserDb.DB().SetMaxIdleConns(10)
  160. writeUserDb.DB().SetMaxOpenConns(100)
  161. writeUserDb.LogMode(true)
  162. readMiddleDb, err = gorm.Open("mysql", rmdsn)
  163. if err != nil {
  164. //beego.Error(err)
  165. }
  166. readMiddleDb.DB().SetMaxIdleConns(10)
  167. readMiddleDb.DB().SetMaxOpenConns(100)
  168. readMiddleDb.LogMode(true)
  169. writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  170. if err != nil {
  171. //beego.Error(err)
  172. }
  173. writeMiddleDb.DB().SetMaxIdleConns(10)
  174. writeMiddleDb.DB().SetMaxOpenConns(100)
  175. writeMiddleDb.LogMode(true)
  176. readPatientDb, err = gorm.Open("mysql", rpdsn)
  177. if err != nil {
  178. beego.Error(err)
  179. }
  180. readPatientDb.DB().SetMaxIdleConns(10)
  181. readPatientDb.DB().SetMaxOpenConns(100)
  182. readPatientDb.LogMode(true)
  183. writePatientDb, err = gorm.Open("mysql", wpdsn)
  184. if err != nil {
  185. beego.Error(err)
  186. }
  187. writePatientDb.DB().SetMaxIdleConns(10)
  188. writePatientDb.DB().SetMaxOpenConns(100)
  189. writePatientDb.LogMode(true)
  190. readSgjPatientDb, err = gorm.Open("mysql", rspdsn)
  191. if err != nil {
  192. beego.Error(err)
  193. }
  194. readSgjPatientDb.DB().SetMaxIdleConns(10)
  195. readSgjPatientDb.DB().SetMaxOpenConns(100)
  196. readSgjPatientDb.LogMode(true)
  197. writeSgjPatientDb, err = gorm.Open("mysql", wspdsn)
  198. if err != nil {
  199. beego.Error(err)
  200. }
  201. writeSgjPatientDb.DB().SetMaxIdleConns(10)
  202. writeSgjPatientDb.DB().SetMaxOpenConns(100)
  203. writeSgjPatientDb.LogMode(true)
  204. }
  205. //func DisconnectDB() {
  206. // if err := readDb.Close(); nil != err {
  207. // beego.Error("Disconnect from database failed: " + err.Error())
  208. // }
  209. //}
  210. func XTReadDB() *gorm.DB {
  211. return readDb
  212. }
  213. func XTWriteDB() *gorm.DB {
  214. return writeDb
  215. }
  216. func UserReadDB() *gorm.DB {
  217. return readUserDb
  218. }
  219. func UserWriteDB() *gorm.DB {
  220. return writeUserDb
  221. }
  222. func MiddleReadDB() *gorm.DB {
  223. return readMiddleDb
  224. }
  225. func MiddleWriteDB() *gorm.DB {
  226. return writeMiddleDb
  227. }
  228. func PatientReadDB() *gorm.DB {
  229. return readPatientDb
  230. }
  231. func PatientWriteDB() *gorm.DB {
  232. return writePatientDb
  233. }
  234. func SgjPatientReadDB() *gorm.DB {
  235. return readSgjPatientDb
  236. }
  237. func SgjPatientWriteDB() *gorm.DB {
  238. return writeSgjPatientDb
  239. }