db.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/mssql" // mssql
  22. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  23. )
  24. var readDb *gorm.DB
  25. var writeDb *gorm.DB
  26. var tempwriteDb *gorm.DB
  27. var readRemoteDb *gorm.DB
  28. var writeUserDb *gorm.DB
  29. var readUserDb *gorm.DB
  30. var readMiddleDb *gorm.DB
  31. var writeMiddleDb *gorm.DB
  32. var backupDb *gorm.DB
  33. var err error
  34. var blDb *gorm.DB
  35. var shDb *gorm.DB
  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. tempwriteHost := beego.AppConfig.String("tempwritemysqlhost")
  48. tempwritePort := beego.AppConfig.String("tempwritemysqlport")
  49. tempwriteUser := beego.AppConfig.String("tempwritemysqluser")
  50. tempwritePass := beego.AppConfig.String("tempwritemysqlpass")
  51. tempwriteName := beego.AppConfig.String("tempwritemysqlname")
  52. readUserHost := beego.AppConfig.String("readuserhost")
  53. readUserPort := beego.AppConfig.String("readuserport")
  54. readUserUser := beego.AppConfig.String("readuseruser")
  55. readUserPass := beego.AppConfig.String("readuserpass")
  56. readUserName := beego.AppConfig.String("readusername")
  57. // readRemoteHost := beego.AppConfig.String("readremotehost")
  58. // readRemotePort := beego.AppConfig.String("readremoteport")
  59. // readRemoteUser := beego.AppConfig.String("readremoteuser")
  60. // readRemotePass := beego.AppConfig.String("readremotepass")
  61. // readRemoteName := beego.AppConfig.String("readremotename")
  62. writeUserHost := beego.AppConfig.String("writeuserhost")
  63. writeUserPort := beego.AppConfig.String("writeuserport")
  64. writeUserUser := beego.AppConfig.String("writeuseruser")
  65. writeUserPass := beego.AppConfig.String("writeuserpass")
  66. writeUserName := beego.AppConfig.String("writeusername")
  67. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  68. readMiddlePort := beego.AppConfig.String("readmiddleport")
  69. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  70. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  71. readMiddleName := beego.AppConfig.String("readmiddlename")
  72. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  73. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  74. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  75. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  76. writeMiddleName := beego.AppConfig.String("writemiddlename")
  77. // backupHost := beego.AppConfig.String("backuphost")
  78. // backupPort := beego.AppConfig.String("backupport")
  79. // backupUser := beego.AppConfig.String("backupuser")
  80. // backupPass := beego.AppConfig.String("backuppass")
  81. // backupName := beego.AppConfig.String("backupname")
  82. // blHost := beego.AppConfig.String("blmysqlhost")
  83. // blPort := beego.AppConfig.String("blmysqlport")
  84. // blUser := beego.AppConfig.String("blmysqluser")
  85. // blPass := beego.AppConfig.String("blmysqlpass")
  86. // blName := beego.AppConfig.String("blmysqlname")
  87. // fmt.Println(blHost)
  88. // fmt.Println(blPort)
  89. // fmt.Println(blUser)
  90. // fmt.Println(blPass)
  91. // fmt.Println(blName)
  92. shHost := beego.AppConfig.String("shmysqlhost")
  93. shPort := beego.AppConfig.String("shmysqlport")
  94. shUser := beego.AppConfig.String("shmysqluser")
  95. shPass := beego.AppConfig.String("shmysqlpass")
  96. shName := beego.AppConfig.String("shmysqlname")
  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. tempwdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", tempwriteUser, tempwritePass, tempwriteHost, tempwritePort, tempwriteName)
  100. // rudsn := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s", readRemoteHost, readRemotePort, readRemoteName, readRemoteUser, readRemotePass)
  101. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  102. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  103. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  104. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  105. // bldsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", blUser, blPass, blHost, blPort, blName)
  106. shdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", shUser, shPass, shHost, shPort, shName)
  107. // bdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", backupUser, backupPass, backupHost, backupPort, backupName)
  108. readDb, err = gorm.Open("mysql", rdsn)
  109. if err != nil {
  110. //beego.Error(err)
  111. }
  112. readDb.DB().SetMaxIdleConns(10)
  113. readDb.DB().SetMaxOpenConns(100)
  114. readDb.LogMode(true)
  115. // backupDb, err = gorm.Open("mysql", bdsn)
  116. // if err != nil {
  117. // //beego.Error(err)
  118. // }
  119. // backupDb.DB().SetMaxIdleConns(10)
  120. // backupDb.DB().SetMaxOpenConns(100)
  121. // backupDb.LogMode(true)
  122. writeDb, err = gorm.Open("mysql", wdsn)
  123. if err != nil {
  124. //beego.Error(err)
  125. }
  126. writeDb.DB().SetMaxIdleConns(10)
  127. writeDb.DB().SetMaxOpenConns(100)
  128. writeDb.LogMode(true)
  129. tempwriteDb, err = gorm.Open("mysql", tempwdsn)
  130. if err != nil {
  131. //beego.Error(err)
  132. }
  133. tempwriteDb.DB().SetMaxIdleConns(10)
  134. tempwriteDb.DB().SetMaxOpenConns(100)
  135. tempwriteDb.LogMode(true)
  136. // fmt.Println(rudsn)
  137. // readRemoteDb, err = gorm.Open("mssql", rudsn)
  138. // if err != nil {
  139. // panic(err)
  140. // }
  141. // readRemoteDb.DB().SetMaxIdleConns(10)
  142. // readRemoteDb.DB().SetMaxOpenConns(100)
  143. // readRemoteDb.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. readUserDb, err = gorm.Open("mysql", rudsn)
  166. if err != nil {
  167. //beego.Error(err)
  168. }
  169. readUserDb.DB().SetMaxIdleConns(10)
  170. readUserDb.DB().SetMaxOpenConns(100)
  171. readUserDb.LogMode(true)
  172. // blDb, err = gorm.Open("mysql", bldsn)
  173. // if err != nil {
  174. // fmt.Println("12344")
  175. // fmt.Println(bldsn)
  176. // fmt.Println(err)
  177. // }
  178. // blDb.DB().SetMaxIdleConns(10)
  179. // blDb.DB().SetMaxOpenConns(100)
  180. // blDb.LogMode(true)
  181. shDb, err = gorm.Open("mysql", shdsn)
  182. if err != nil {
  183. fmt.Println("12344")
  184. fmt.Println(err)
  185. }
  186. shDb.DB().SetMaxIdleConns(10)
  187. shDb.DB().SetMaxOpenConns(100)
  188. shDb.LogMode(true)
  189. }
  190. //func DisconnectDB() {
  191. // if err := readDb.Close(); nil != err {
  192. // beego.Error("Disconnect from database failed: " + err.Error())
  193. // }
  194. //}
  195. func XTReadDB() *gorm.DB {
  196. return readDb
  197. }
  198. func XTWriteDB() *gorm.DB {
  199. return writeDb
  200. }
  201. // func RemoteReadDB() *gorm.DB {
  202. // return readRemoteDb
  203. // }
  204. func UserWriteDB() *gorm.DB {
  205. return writeUserDb
  206. }
  207. func MiddleReadDB() *gorm.DB {
  208. return readMiddleDb
  209. }
  210. func MiddleWriteDB() *gorm.DB {
  211. return writeMiddleDb
  212. }