db.go 11KB

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