db.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 writeDb *gorm.DB
  25. var readUserDb *gorm.DB
  26. var writeUserDb *gorm.DB
  27. var readMiddleDb *gorm.DB
  28. var writeMiddleDb *gorm.DB
  29. var readPatientDb *gorm.DB
  30. var writePatientDb *gorm.DB
  31. var readSgjPatientDb *gorm.DB
  32. var writeSgjPatientDb *gorm.DB
  33. var readUserDbT *gorm.DB
  34. var writeUserDbT *gorm.DB
  35. var err error
  36. func ConnectDB() {
  37. readHost := beego.AppConfig.String("readmysqlhost")
  38. readPort := beego.AppConfig.String("readmysqlport")
  39. readUser := beego.AppConfig.String("readmysqluser")
  40. readPass := beego.AppConfig.String("readmysqlpass")
  41. readName := beego.AppConfig.String("readmysqlname")
  42. writeHost := beego.AppConfig.String("writemysqlhost")
  43. writePort := beego.AppConfig.String("writemysqlport")
  44. writeUser := beego.AppConfig.String("writemysqluser")
  45. writePass := beego.AppConfig.String("writemysqlpass")
  46. writeName := beego.AppConfig.String("writemysqlname")
  47. readUserHost := beego.AppConfig.String("readuserhost")
  48. readUserPort := beego.AppConfig.String("readuserport")
  49. readUserUser := beego.AppConfig.String("readuseruser")
  50. readUserPass := beego.AppConfig.String("readuserpass")
  51. readUserName := beego.AppConfig.String("readusername")
  52. writeUserHost := beego.AppConfig.String("writeuserhost")
  53. writeUserPort := beego.AppConfig.String("writeuserport")
  54. writeUserUser := beego.AppConfig.String("writeuseruser")
  55. writeUserPass := beego.AppConfig.String("writeuserpass")
  56. writeUserName := beego.AppConfig.String("writeusername")
  57. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  58. readMiddlePort := beego.AppConfig.String("readmiddleport")
  59. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  60. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  61. readMiddleName := beego.AppConfig.String("readmiddlename")
  62. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  63. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  64. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  65. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  66. writeMiddleName := beego.AppConfig.String("writemiddlename")
  67. readPatientHost := beego.AppConfig.String("readpatienthost")
  68. readPatientPort := beego.AppConfig.String("readpatientport")
  69. readPatientUser := beego.AppConfig.String("readpatientuser")
  70. readPatientPass := beego.AppConfig.String("readpatientpass")
  71. readPatientName := beego.AppConfig.String("readpatientname")
  72. writePatientHost := beego.AppConfig.String("writepatienthost")
  73. writePatientPort := beego.AppConfig.String("writepatientport")
  74. writePatientUser := beego.AppConfig.String("writepatientuser")
  75. writePatientPass := beego.AppConfig.String("writepatientpass")
  76. writePatientName := beego.AppConfig.String("writepatientname")
  77. readSgjPatientHost := beego.AppConfig.String("readsgjpatientmysqlhost")
  78. readSgjPatientPort := beego.AppConfig.String("readsgjpatientmysqlport")
  79. readSgjPatientUser := beego.AppConfig.String("readsgjpatientmysqluser")
  80. readSgjPatientPass := beego.AppConfig.String("readsgjpatientmysqlpass")
  81. readSgjPatientName := beego.AppConfig.String("readsgjpatientmysqlname")
  82. writeSgjPatientHost := beego.AppConfig.String("writesgjpatientmysqlhost")
  83. writeSgjPatientPort := beego.AppConfig.String("writesgjpatientmysqlport")
  84. writeSgjPatientUser := beego.AppConfig.String("writesgjpatientmysqluser")
  85. writeSgjPatientPass := beego.AppConfig.String("writesgjpatientmysqlpass")
  86. writeSgjPatientName := beego.AppConfig.String("writesgjpatientmysqlname")
  87. readUserHostT := beego.AppConfig.String("readuserhostTest")
  88. readUserPortT := beego.AppConfig.String("readuserportTest")
  89. readUserUserT := beego.AppConfig.String("readuseruserTest")
  90. readUserPassT := beego.AppConfig.String("readuserpassTest")
  91. readUserNameT := beego.AppConfig.String("readusernameTest")
  92. writeUserHostT := beego.AppConfig.String("writeuserhostTest")
  93. writeUserPortT := beego.AppConfig.String("writeuserportTest")
  94. writeUserUserT := beego.AppConfig.String("writeuseruserTest")
  95. writeUserPassT := beego.AppConfig.String("writeuserpassTest")
  96. writeUserNameT := beego.AppConfig.String("writeusernameTest")
  97. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  98. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  99. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  100. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  101. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  102. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  103. rpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readPatientUser, readPatientPass, readPatientHost, readPatientPort, readPatientName)
  104. wpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writePatientUser, writePatientPass, writePatientHost, writePatientPort, writePatientName)
  105. rspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readSgjPatientUser, readSgjPatientPass, readSgjPatientHost, readSgjPatientPort, readSgjPatientName)
  106. wspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeSgjPatientUser, writeSgjPatientPass, writeSgjPatientHost, writeSgjPatientPort, writeSgjPatientName)
  107. rudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUserT, readUserPassT, readUserHostT, readUserPortT, readUserNameT)
  108. wudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUserT, writeUserPassT, writeUserHostT, writeUserPortT, writeUserNameT)
  109. readDb, err = gorm.Open("mysql", rdsn)
  110. if err != nil {
  111. //beego.Error(err)
  112. }
  113. readDb.DB().SetMaxIdleConns(10)
  114. readDb.DB().SetMaxOpenConns(100)
  115. readDb.LogMode(true)
  116. writeDb, err = gorm.Open("mysql", wdsn)
  117. if err != nil {
  118. //beego.Error(err)
  119. }
  120. writeDb.DB().SetMaxIdleConns(10)
  121. writeDb.DB().SetMaxOpenConns(100)
  122. writeDb.LogMode(true)
  123. readUserDbT, err = gorm.Open("mysql", rudsnT)
  124. if err != nil {
  125. //beego.Error(err)
  126. }
  127. readUserDbT.DB().SetMaxIdleConns(10)
  128. readUserDbT.DB().SetMaxOpenConns(100)
  129. readUserDbT.LogMode(true)
  130. writeUserDbT, err = gorm.Open("mysql", wudsnT)
  131. if err != nil {
  132. //beego.Error(err)
  133. }
  134. writeUserDbT.DB().SetMaxIdleConns(10)
  135. writeUserDbT.DB().SetMaxOpenConns(100)
  136. writeUserDbT.LogMode(true)
  137. readUserDb, err = gorm.Open("mysql", rudsn)
  138. if err != nil {
  139. //beego.Error(err)
  140. }
  141. readUserDb.DB().SetMaxIdleConns(10)
  142. readUserDb.DB().SetMaxOpenConns(100)
  143. readUserDb.LogMode(true)
  144. writeUserDb, err = gorm.Open("mysql", wudsn)
  145. if err != nil {
  146. //beego.Error(err)
  147. }
  148. writeUserDb.DB().SetMaxIdleConns(10)
  149. writeUserDb.DB().SetMaxOpenConns(100)
  150. writeUserDb.LogMode(true)
  151. readMiddleDb, err = gorm.Open("mysql", rmdsn)
  152. if err != nil {
  153. //beego.Error(err)
  154. }
  155. readMiddleDb.DB().SetMaxIdleConns(10)
  156. readMiddleDb.DB().SetMaxOpenConns(100)
  157. readMiddleDb.LogMode(true)
  158. writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  159. if err != nil {
  160. //beego.Error(err)
  161. }
  162. writeMiddleDb.DB().SetMaxIdleConns(10)
  163. writeMiddleDb.DB().SetMaxOpenConns(100)
  164. writeMiddleDb.LogMode(true)
  165. readPatientDb, err = gorm.Open("mysql", rpdsn)
  166. if err != nil {
  167. beego.Error(err)
  168. }
  169. readPatientDb.DB().SetMaxIdleConns(10)
  170. readPatientDb.DB().SetMaxOpenConns(100)
  171. readPatientDb.LogMode(true)
  172. writePatientDb, err = gorm.Open("mysql", wpdsn)
  173. if err != nil {
  174. beego.Error(err)
  175. }
  176. writePatientDb.DB().SetMaxIdleConns(10)
  177. writePatientDb.DB().SetMaxOpenConns(100)
  178. writePatientDb.LogMode(true)
  179. readSgjPatientDb, err = gorm.Open("mysql", rspdsn)
  180. if err != nil {
  181. beego.Error(err)
  182. }
  183. readSgjPatientDb.DB().SetMaxIdleConns(10)
  184. readSgjPatientDb.DB().SetMaxOpenConns(100)
  185. readSgjPatientDb.LogMode(true)
  186. writeSgjPatientDb, err = gorm.Open("mysql", wspdsn)
  187. if err != nil {
  188. beego.Error(err)
  189. }
  190. writeSgjPatientDb.DB().SetMaxIdleConns(10)
  191. writeSgjPatientDb.DB().SetMaxOpenConns(100)
  192. writeSgjPatientDb.LogMode(true)
  193. }
  194. //func DisconnectDB() {
  195. // if err := readDb.Close(); nil != err {
  196. // beego.Error("Disconnect from database failed: " + err.Error())
  197. // }
  198. //}
  199. func XTReadDB() *gorm.DB {
  200. return readDb
  201. }
  202. func XTWriteDB() *gorm.DB {
  203. return writeDb
  204. }
  205. func UserReadDB() *gorm.DB {
  206. return readUserDb
  207. }
  208. func UserWriteDB() *gorm.DB {
  209. return writeUserDb
  210. }
  211. func MiddleReadDB() *gorm.DB {
  212. return readMiddleDb
  213. }
  214. func MiddleWriteDB() *gorm.DB {
  215. return writeMiddleDb
  216. }
  217. func PatientReadDB() *gorm.DB {
  218. return readPatientDb
  219. }
  220. func PatientWriteDB() *gorm.DB {
  221. return writePatientDb
  222. }
  223. func SgjPatientReadDB() *gorm.DB {
  224. return readSgjPatientDb
  225. }
  226. func SgjPatientWriteDB() *gorm.DB {
  227. return writeSgjPatientDb
  228. }