db.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "sws_xcx/utils"
  20. "github.com/astaxie/beego"
  21. "github.com/jinzhu/gorm"
  22. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  23. )
  24. var readDb *gorm.DB
  25. var writeDb *gorm.DB
  26. var readUserDb *gorm.DB
  27. var readXtDb *gorm.DB
  28. // var writeUserDb *gorm.DB
  29. // var readMiddleDb *gorm.DB
  30. // var writeMiddleDb *gorm.DB
  31. // var readPatientDb *gorm.DB
  32. // var writePatientDb *gorm.DB
  33. // var readSgjPatientDb *gorm.DB
  34. // var writeSgjPatientDb *gorm.DB
  35. // var readUserDbT *gorm.DB
  36. // var writeUserDbT *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. readXtHost := beego.AppConfig.String("readxthost")
  55. readXtPort := beego.AppConfig.String("readxtport")
  56. readXtUser := beego.AppConfig.String("readxtuser")
  57. readXtPass := beego.AppConfig.String("readxtpass")
  58. readXtName := beego.AppConfig.String("readxtname")
  59. // writeUserHost := beego.AppConfig.String("writeuserhost")
  60. // writeUserPort := beego.AppConfig.String("writeuserport")
  61. // writeUserUser := beego.AppConfig.String("writeuseruser")
  62. // writeUserPass := beego.AppConfig.String("writeuserpass")
  63. // writeUserName := beego.AppConfig.String("writeusername")
  64. // readMiddleHost := beego.AppConfig.String("readmiddlehost")
  65. // readMiddlePort := beego.AppConfig.String("readmiddleport")
  66. // readMiddleUser := beego.AppConfig.String("readmiddleuser")
  67. // readMiddlePass := beego.AppConfig.String("readmiddlepass")
  68. // readMiddleName := beego.AppConfig.String("readmiddlename")
  69. // writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  70. // writeMiddlePort := beego.AppConfig.String("writemiddleport")
  71. // writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  72. // writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  73. // writeMiddleName := beego.AppConfig.String("writemiddlename")
  74. // readPatientHost := beego.AppConfig.String("readpatienthost")
  75. // readPatientPort := beego.AppConfig.String("readpatientport")
  76. // readPatientUser := beego.AppConfig.String("readpatientuser")
  77. // readPatientPass := beego.AppConfig.String("readpatientpass")
  78. // readPatientName := beego.AppConfig.String("readpatientname")
  79. // writePatientHost := beego.AppConfig.String("writepatienthost")
  80. // writePatientPort := beego.AppConfig.String("writepatientport")
  81. // writePatientUser := beego.AppConfig.String("writepatientuser")
  82. // writePatientPass := beego.AppConfig.String("writepatientpass")
  83. // writePatientName := beego.AppConfig.String("writepatientname")
  84. // readSgjPatientHost := beego.AppConfig.String("readsgjpatientmysqlhost")
  85. // readSgjPatientPort := beego.AppConfig.String("readsgjpatientmysqlport")
  86. // readSgjPatientUser := beego.AppConfig.String("readsgjpatientmysqluser")
  87. // readSgjPatientPass := beego.AppConfig.String("readsgjpatientmysqlpass")
  88. // readSgjPatientName := beego.AppConfig.String("readsgjpatientmysqlname")
  89. // writeSgjPatientHost := beego.AppConfig.String("writesgjpatientmysqlhost")
  90. // writeSgjPatientPort := beego.AppConfig.String("writesgjpatientmysqlport")
  91. // writeSgjPatientUser := beego.AppConfig.String("writesgjpatientmysqluser")
  92. // writeSgjPatientPass := beego.AppConfig.String("writesgjpatientmysqlpass")
  93. // writeSgjPatientName := beego.AppConfig.String("writesgjpatientmysqlname")
  94. // readUserHostT := beego.AppConfig.String("readuserhostTest")
  95. // readUserPortT := beego.AppConfig.String("readuserportTest")
  96. // readUserUserT := beego.AppConfig.String("readuseruserTest")
  97. // readUserPassT := beego.AppConfig.String("readuserpassTest")
  98. // readUserNameT := beego.AppConfig.String("readusernameTest")
  99. // writeUserHostT := beego.AppConfig.String("writeuserhostTest")
  100. // writeUserPortT := beego.AppConfig.String("writeuserportTest")
  101. // writeUserUserT := beego.AppConfig.String("writeuseruserTest")
  102. // writeUserPassT := beego.AppConfig.String("writeuserpassTest")
  103. // writeUserNameT := beego.AppConfig.String("writeusernameTest")
  104. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", readUser, readPass, readHost, readPort, readName)
  105. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", 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. rxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readXtUser, readXtPass, readXtHost, readXtPort, readXtName)
  108. // wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  109. // rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  110. // wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  111. // rpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readPatientUser, readPatientPass, readPatientHost, readPatientPort, readPatientName)
  112. // wpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writePatientUser, writePatientPass, writePatientHost, writePatientPort, writePatientName)
  113. // rspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readSgjPatientUser, readSgjPatientPass, readSgjPatientHost, readSgjPatientPort, readSgjPatientName)
  114. // wspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeSgjPatientUser, writeSgjPatientPass, writeSgjPatientHost, writeSgjPatientPort, writeSgjPatientName)
  115. // rudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUserT, readUserPassT, readUserHostT, readUserPortT, readUserNameT)
  116. // wudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUserT, writeUserPassT, writeUserHostT, writeUserPortT, writeUserNameT)
  117. readDb, err = gorm.Open("mysql", rdsn)
  118. if err != nil {
  119. utils.ErrorLog("init DB err %v", err)
  120. }
  121. readDb.DB().SetMaxIdleConns(10)
  122. readDb.DB().SetMaxOpenConns(100)
  123. readDb.LogMode(true)
  124. writeDb, err = gorm.Open("mysql", wdsn)
  125. if err != nil {
  126. utils.ErrorLog("init DB err %v", err)
  127. }
  128. writeDb.DB().SetMaxIdleConns(10)
  129. writeDb.DB().SetMaxOpenConns(100)
  130. writeDb.LogMode(true)
  131. // readUserDbT, err = gorm.Open("mysql", rudsnT)
  132. // if err != nil {
  133. // //beego.Error(err)
  134. // }
  135. // readUserDbT.DB().SetMaxIdleConns(10)
  136. // readUserDbT.DB().SetMaxOpenConns(100)
  137. // readUserDbT.LogMode(true)
  138. // writeUserDbT, err = gorm.Open("mysql", wudsnT)
  139. // if err != nil {
  140. // //beego.Error(err)
  141. // }
  142. // writeUserDbT.DB().SetMaxIdleConns(10)
  143. // writeUserDbT.DB().SetMaxOpenConns(100)
  144. // writeUserDbT.LogMode(true)
  145. // readUserDb, err = gorm.Open("mysql", rudsn)
  146. // if err != nil {
  147. // utils.ErrorLog("init DB err %v", err)
  148. // }
  149. // readUserDb.DB().SetMaxIdleConns(10)
  150. // readUserDb.DB().SetMaxOpenConns(100)
  151. // readUserDb.LogMode(true)
  152. readXtDb, err = gorm.Open("mysql", rxdsn)
  153. if err != nil {
  154. utils.ErrorLog("init DB err %v", err)
  155. }
  156. readXtDb.DB().SetMaxIdleConns(10)
  157. readXtDb.DB().SetMaxOpenConns(100)
  158. readXtDb.LogMode(true)
  159. // writeUserDb, err = gorm.Open("mysql", wudsn)
  160. // if err != nil {
  161. // //beego.Error(err)
  162. // }
  163. // writeUserDb.DB().SetMaxIdleConns(10)
  164. // writeUserDb.DB().SetMaxOpenConns(100)
  165. // writeUserDb.LogMode(true)
  166. // readMiddleDb, err = gorm.Open("mysql", rmdsn)
  167. // if err != nil {
  168. // //beego.Error(err)
  169. // }
  170. // readMiddleDb.DB().SetMaxIdleConns(10)
  171. // readMiddleDb.DB().SetMaxOpenConns(100)
  172. // readMiddleDb.LogMode(true)
  173. // writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  174. // if err != nil {
  175. // //beego.Error(err)
  176. // }
  177. // writeMiddleDb.DB().SetMaxIdleConns(10)
  178. // writeMiddleDb.DB().SetMaxOpenConns(100)
  179. // writeMiddleDb.LogMode(true)
  180. // readPatientDb, err = gorm.Open("mysql", rpdsn)
  181. // if err != nil {
  182. // beego.Error(err)
  183. // }
  184. // readPatientDb.DB().SetMaxIdleConns(10)
  185. // readPatientDb.DB().SetMaxOpenConns(100)
  186. // readPatientDb.LogMode(true)
  187. // writePatientDb, err = gorm.Open("mysql", wpdsn)
  188. // if err != nil {
  189. // beego.Error(err)
  190. // }
  191. // writePatientDb.DB().SetMaxIdleConns(10)
  192. // writePatientDb.DB().SetMaxOpenConns(100)
  193. // writePatientDb.LogMode(true)
  194. // readSgjPatientDb, err = gorm.Open("mysql", rspdsn)
  195. // if err != nil {
  196. // beego.Error(err)
  197. // }
  198. // readSgjPatientDb.DB().SetMaxIdleConns(10)
  199. // readSgjPatientDb.DB().SetMaxOpenConns(100)
  200. // readSgjPatientDb.LogMode(true)
  201. // writeSgjPatientDb, err = gorm.Open("mysql", wspdsn)
  202. // if err != nil {
  203. // beego.Error(err)
  204. // }
  205. // writeSgjPatientDb.DB().SetMaxIdleConns(10)
  206. // writeSgjPatientDb.DB().SetMaxOpenConns(100)
  207. // writeSgjPatientDb.LogMode(true)
  208. }
  209. //func DisconnectDB() {
  210. // if err := readDb.Close(); nil != err {
  211. // beego.Error("Disconnect from database failed: " + err.Error())
  212. // }
  213. //}
  214. func ReadDB() *gorm.DB {
  215. return readDb
  216. }
  217. func WriteDB() *gorm.DB {
  218. return writeDb
  219. }
  220. func ReadUserDB() *gorm.DB {
  221. return readUserDb
  222. }
  223. func ReadXtDB() *gorm.DB {
  224. return readXtDb
  225. }
  226. // func UserWriteDB() *gorm.DB {
  227. // return writeUserDb
  228. // }
  229. // func MiddleReadDB() *gorm.DB {
  230. // return readMiddleDb
  231. // }
  232. // func MiddleWriteDB() *gorm.DB {
  233. // return writeMiddleDb
  234. // }
  235. // func PatientReadDB() *gorm.DB {
  236. // return readPatientDb
  237. // }
  238. // func PatientWriteDB() *gorm.DB {
  239. // return writePatientDb
  240. // }
  241. // func SgjPatientReadDB() *gorm.DB {
  242. // return readSgjPatientDb
  243. // }
  244. // func SgjPatientWriteDB() *gorm.DB {
  245. // return writeSgjPatientDb
  246. // }