db.go 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 CreateSqlServiceDB(Host string, Port string, User string, Pass string, Name string) (mssqlDb *gorm.DB, err error) {
  37. //rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
  38. mssqlstring := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;encrypt=disable", Host, Port, Name, User, Pass)
  39. fmt.Println(mssqlstring)
  40. mssqlDb, err = gorm.Open("mssql", mssqlstring)
  41. if err != nil {
  42. panic(err)
  43. }
  44. mssqlDb.DB().SetMaxIdleConns(5)
  45. mssqlDb.DB().SetMaxOpenConns(20)
  46. mssqlDb.LogMode(true)
  47. return
  48. }
  49. func CloseDB(DB *gorm.DB) {
  50. DB.Close()
  51. }
  52. func ConnectDB() {
  53. readHost := beego.AppConfig.String("readmysqlhost")
  54. readPort := beego.AppConfig.String("readmysqlport")
  55. readUser := beego.AppConfig.String("readmysqluser")
  56. readPass := beego.AppConfig.String("readmysqlpass")
  57. readName := beego.AppConfig.String("readmysqlname")
  58. writeHost := beego.AppConfig.String("writemysqlhost")
  59. writePort := beego.AppConfig.String("writemysqlport")
  60. writeUser := beego.AppConfig.String("writemysqluser")
  61. writePass := beego.AppConfig.String("writemysqlpass")
  62. writeName := beego.AppConfig.String("writemysqlname")
  63. // tempwriteHost := beego.AppConfig.String("tempwritemysqlhost")
  64. // tempwritePort := beego.AppConfig.String("tempwritemysqlport")
  65. // tempwriteUser := beego.AppConfig.String("tempwritemysqluser")
  66. // tempwritePass := beego.AppConfig.String("tempwritemysqlpass")
  67. // tempwriteName := beego.AppConfig.String("tempwritemysqlname")
  68. readUserHost := beego.AppConfig.String("readuserhost")
  69. readUserPort := beego.AppConfig.String("readuserport")
  70. readUserUser := beego.AppConfig.String("readuseruser")
  71. readUserPass := beego.AppConfig.String("readuserpass")
  72. readUserName := beego.AppConfig.String("readusername")
  73. // readRemoteHost := beego.AppConfig.String("readremotehost")
  74. // readRemotePort := beego.AppConfig.String("readremoteport")
  75. // readRemoteUser := beego.AppConfig.String("readremoteuser")
  76. // readRemotePass := beego.AppConfig.String("readremotepass")
  77. // readRemoteName := beego.AppConfig.String("readremotename")
  78. writeUserHost := beego.AppConfig.String("writeuserhost")
  79. writeUserPort := beego.AppConfig.String("writeuserport")
  80. writeUserUser := beego.AppConfig.String("writeuseruser")
  81. writeUserPass := beego.AppConfig.String("writeuserpass")
  82. writeUserName := beego.AppConfig.String("writeusername")
  83. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  84. readMiddlePort := beego.AppConfig.String("readmiddleport")
  85. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  86. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  87. readMiddleName := beego.AppConfig.String("readmiddlename")
  88. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  89. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  90. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  91. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  92. writeMiddleName := beego.AppConfig.String("writemiddlename")
  93. // backupHost := beego.AppConfig.String("backuphost")
  94. // backupPort := beego.AppConfig.String("backupport")
  95. // backupUser := beego.AppConfig.String("backupuser")
  96. // backupPass := beego.AppConfig.String("backuppass")
  97. // backupName := beego.AppConfig.String("backupname")
  98. // blHost := beego.AppConfig.String("blmysqlhost")
  99. // blPort := beego.AppConfig.String("blmysqlport")
  100. // blUser := beego.AppConfig.String("blmysqluser")
  101. // blPass := beego.AppConfig.String("blmysqlpass")
  102. // blName := beego.AppConfig.String("blmysqlname")
  103. // fmt.Println(blHost)
  104. // fmt.Println(blPort)
  105. // fmt.Println(blUser)
  106. // fmt.Println(blPass)
  107. // fmt.Println(blName)
  108. // shHost := beego.AppConfig.String("shmysqlhost")
  109. // shPort := beego.AppConfig.String("shmysqlport")
  110. // shUser := beego.AppConfig.String("shmysqluser")
  111. // shPass := beego.AppConfig.String("shmysqlpass")
  112. // shName := beego.AppConfig.String("shmysqlname")
  113. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  114. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  115. // tempwdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", tempwriteUser, tempwritePass, tempwriteHost, tempwritePort, tempwriteName)
  116. // rudsn := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s", readRemoteHost, readRemotePort, readRemoteName, readRemoteUser, readRemotePass)
  117. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  118. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  119. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  120. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  121. // bldsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", blUser, blPass, blHost, blPort, blName)
  122. // shdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", shUser, shPass, shHost, shPort, shName)
  123. // bdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", backupUser, backupPass, backupHost, backupPort, backupName)
  124. readDb, err = gorm.Open("mysql", rdsn)
  125. if err != nil {
  126. //beego.Error(err)
  127. }
  128. readDb.DB().SetMaxIdleConns(10)
  129. readDb.DB().SetMaxOpenConns(100)
  130. readDb.LogMode(true)
  131. // backupDb, err = gorm.Open("mysql", bdsn)
  132. // if err != nil {
  133. // //beego.Error(err)
  134. // }
  135. // backupDb.DB().SetMaxIdleConns(10)
  136. // backupDb.DB().SetMaxOpenConns(100)
  137. // backupDb.LogMode(true)
  138. writeDb, err = gorm.Open("mysql", wdsn)
  139. if err != nil {
  140. //beego.Error(err)
  141. }
  142. writeDb.DB().SetMaxIdleConns(10)
  143. writeDb.DB().SetMaxOpenConns(100)
  144. writeDb.LogMode(true)
  145. // tempwriteDb, err = gorm.Open("mysql", tempwdsn)
  146. // if err != nil {
  147. // //beego.Error(err)
  148. // }
  149. // tempwriteDb.DB().SetMaxIdleConns(10)
  150. // tempwriteDb.DB().SetMaxOpenConns(100)
  151. // tempwriteDb.LogMode(true)
  152. // fmt.Println(rudsn)
  153. // readRemoteDb, err = gorm.Open("mssql", rudsn)
  154. // if err != nil {
  155. // panic(err)
  156. // }
  157. // readRemoteDb.DB().SetMaxIdleConns(10)
  158. // readRemoteDb.DB().SetMaxOpenConns(100)
  159. // readRemoteDb.LogMode(true)
  160. writeUserDb, err = gorm.Open("mysql", wudsn)
  161. if err != nil {
  162. //beego.Error(err)
  163. }
  164. writeUserDb.DB().SetMaxIdleConns(10)
  165. writeUserDb.DB().SetMaxOpenConns(100)
  166. writeUserDb.LogMode(true)
  167. readMiddleDb, err = gorm.Open("mysql", rmdsn)
  168. if err != nil {
  169. //beego.Error(err)
  170. }
  171. readMiddleDb.DB().SetMaxIdleConns(10)
  172. readMiddleDb.DB().SetMaxOpenConns(100)
  173. readMiddleDb.LogMode(true)
  174. writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  175. if err != nil {
  176. //beego.Error(err)
  177. }
  178. writeMiddleDb.DB().SetMaxIdleConns(10)
  179. writeMiddleDb.DB().SetMaxOpenConns(100)
  180. writeMiddleDb.LogMode(true)
  181. readUserDb, err = gorm.Open("mysql", rudsn)
  182. if err != nil {
  183. //beego.Error(err)
  184. }
  185. readUserDb.DB().SetMaxIdleConns(10)
  186. readUserDb.DB().SetMaxOpenConns(100)
  187. readUserDb.LogMode(true)
  188. // blDb, err = gorm.Open("mysql", bldsn)
  189. // if err != nil {
  190. // fmt.Println("12344")
  191. // fmt.Println(bldsn)
  192. // fmt.Println(err)
  193. // }
  194. // blDb.DB().SetMaxIdleConns(10)
  195. // blDb.DB().SetMaxOpenConns(100)
  196. // blDb.LogMode(true)
  197. // shDb, err = gorm.Open("mysql", shdsn)
  198. // if err != nil {
  199. // fmt.Println("12344")
  200. // fmt.Println(err)
  201. // }
  202. // shDb.DB().SetMaxIdleConns(10)
  203. // shDb.DB().SetMaxOpenConns(100)
  204. // shDb.LogMode(true)
  205. }
  206. //func DisconnectDB() {
  207. // if err := readDb.Close(); nil != err {
  208. // beego.Error("Disconnect from database failed: " + err.Error())
  209. // }
  210. //}
  211. func XTReadDB() *gorm.DB {
  212. return readDb
  213. }
  214. func XTWriteDB() *gorm.DB {
  215. return writeDb
  216. }
  217. // func RemoteReadDB() *gorm.DB {
  218. // return readRemoteDb
  219. // }
  220. func UserWriteDB() *gorm.DB {
  221. return writeUserDb
  222. }
  223. func MiddleReadDB() *gorm.DB {
  224. return readMiddleDb
  225. }
  226. func MiddleWriteDB() *gorm.DB {
  227. return writeMiddleDb
  228. }