db.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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"
  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 writeUserDb *gorm.DB
  28. var err error
  29. func CreateDB(Host string, Port string, User string, Pass string, Name string) (rsdDb *gorm.DB, err error) {
  30. rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
  31. rsdDb, err = gorm.Open("mysql", rdb)
  32. if err != nil {
  33. //beego.Error(err)
  34. }
  35. rsdDb.DB().SetMaxIdleConns(5)
  36. rsdDb.DB().SetMaxOpenConns(20)
  37. rsdDb.LogMode(true)
  38. return
  39. }
  40. func CreateSqlServiceDB(Host string, Port string, User string, Pass string, Name string) (mssqlDb *gorm.DB, err error) {
  41. //rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
  42. mssqlstring := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;encrypt=disable", Host, Port, Name, User, Pass)
  43. fmt.Println(mssqlstring)
  44. mssqlDb, err = gorm.Open("mssql", mssqlstring)
  45. if err != nil {
  46. panic(err)
  47. }
  48. mssqlDb.DB().SetMaxIdleConns(5)
  49. mssqlDb.DB().SetMaxOpenConns(20)
  50. mssqlDb.LogMode(true)
  51. return
  52. }
  53. func CloseDB(DB *gorm.DB) {
  54. DB.Close()
  55. }
  56. func ConnectDB() {
  57. readHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  58. readPort := beego.AppConfig.String("readmysqlport")
  59. readUser := "syh"
  60. readPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
  61. readName := beego.AppConfig.String("readmysqlname")
  62. writeHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  63. writePort := beego.AppConfig.String("writemysqlport")
  64. writeUser := "syh"
  65. writePass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
  66. writeName := beego.AppConfig.String("writemysqlname")
  67. readUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  68. readUserPort := beego.AppConfig.String("readuserport")
  69. readUserUser := "syh"
  70. readUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
  71. readUserName := beego.AppConfig.String("readusername")
  72. writeUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  73. writeUserPort := beego.AppConfig.String("writeuserport")
  74. writeUserUser := "syh"
  75. writeUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
  76. writeUserName := beego.AppConfig.String("writeusername")
  77. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  78. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  79. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  80. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  81. readDb, err = gorm.Open("mysql", rdsn)
  82. //defer readDb.Close()
  83. if err != nil {
  84. //beego.Error(err)
  85. }
  86. readDb.DB().SetMaxIdleConns(5)
  87. readDb.DB().SetMaxOpenConns(20)
  88. readDb.LogMode(true)
  89. writeDb, err = gorm.Open("mysql", wdsn)
  90. if err != nil {
  91. //beego.Error(err)
  92. }
  93. //defer writeDb.Close()
  94. writeDb.DB().SetMaxIdleConns(5)
  95. writeDb.DB().SetMaxOpenConns(20)
  96. writeDb.LogMode(true)
  97. readUserDb, err = gorm.Open("mysql", rudsn)
  98. if err != nil {
  99. //beego.Error(err)
  100. }
  101. //defer readUserDb.Close()
  102. readUserDb.DB().SetMaxIdleConns(5)
  103. readUserDb.DB().SetMaxOpenConns(20)
  104. readUserDb.LogMode(true)
  105. writeUserDb, err = gorm.Open("mysql", wudsn)
  106. if err != nil {
  107. //beego.Error(err)
  108. }
  109. //defer writeUserDb.Close()
  110. writeUserDb.DB().SetMaxIdleConns(5)
  111. writeUserDb.DB().SetMaxOpenConns(20)
  112. writeUserDb.LogMode(true)
  113. }
  114. func XTReadDB() *gorm.DB {
  115. return readDb
  116. }
  117. func XTWriteDB() *gorm.DB {
  118. return writeDb
  119. }
  120. func UserReadDB() *gorm.DB {
  121. return readUserDb
  122. }
  123. func UserWriteDB() *gorm.DB {
  124. return writeUserDb
  125. }