123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // Pipe - A small and beautiful blogging platform written in golang.
- // Copyright (C) 2017-2018, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- package service
-
- import (
- "fmt"
- "github.com/astaxie/beego"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mssql"
- _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
- )
-
- var readDb *gorm.DB
- var writeDb *gorm.DB
- var readUserDb *gorm.DB
- var writeUserDb *gorm.DB
-
- var err error
-
- func CreateDB(Host string, Port string, User string, Pass string, Name string) (rsdDb *gorm.DB, err error) {
- rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
- rsdDb, err = gorm.Open("mysql", rdb)
- if err != nil {
- //beego.Error(err)
- }
- rsdDb.DB().SetMaxIdleConns(5)
- rsdDb.DB().SetMaxOpenConns(20)
- rsdDb.LogMode(true)
- return
- }
-
- func CreateSqlServiceDB(Host string, Port string, User string, Pass string, Name string) (mssqlDb *gorm.DB, err error) {
- //rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
- mssqlstring := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;encrypt=disable", Host, Port, Name, User, Pass)
- fmt.Println(mssqlstring)
- mssqlDb, err = gorm.Open("mssql", mssqlstring)
- if err != nil {
- panic(err)
- }
- mssqlDb.DB().SetMaxIdleConns(5)
- mssqlDb.DB().SetMaxOpenConns(20)
- mssqlDb.LogMode(true)
- return
- }
-
- func CloseDB(DB *gorm.DB) {
- DB.Close()
- }
-
- func ConnectDB() {
- readHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- readPort := beego.AppConfig.String("readmysqlport")
- readUser := "syh"
- readPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- readName := beego.AppConfig.String("readmysqlname")
-
- writeHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- writePort := beego.AppConfig.String("writemysqlport")
- writeUser := "syh"
- writePass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- writeName := beego.AppConfig.String("writemysqlname")
-
- readUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- readUserPort := beego.AppConfig.String("readuserport")
- readUserUser := "syh"
- readUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- readUserName := beego.AppConfig.String("readusername")
-
- writeUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- writeUserPort := beego.AppConfig.String("writeuserport")
- writeUserUser := "syh"
- writeUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- writeUserName := beego.AppConfig.String("writeusername")
-
- rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
- wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
-
- rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
- wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
-
- readDb, err = gorm.Open("mysql", rdsn)
-
- //defer readDb.Close()
- if err != nil {
- //beego.Error(err)
- }
- readDb.DB().SetMaxIdleConns(5)
- readDb.DB().SetMaxOpenConns(20)
- readDb.LogMode(true)
-
- writeDb, err = gorm.Open("mysql", wdsn)
- if err != nil {
- //beego.Error(err)
- }
-
- //defer writeDb.Close()
-
- writeDb.DB().SetMaxIdleConns(5)
- writeDb.DB().SetMaxOpenConns(20)
- writeDb.LogMode(true)
-
- readUserDb, err = gorm.Open("mysql", rudsn)
- if err != nil {
- //beego.Error(err)
- }
-
- //defer readUserDb.Close()
-
- readUserDb.DB().SetMaxIdleConns(5)
- readUserDb.DB().SetMaxOpenConns(20)
- readUserDb.LogMode(true)
-
- writeUserDb, err = gorm.Open("mysql", wudsn)
- if err != nil {
- //beego.Error(err)
- }
- //defer writeUserDb.Close()
-
- writeUserDb.DB().SetMaxIdleConns(5)
- writeUserDb.DB().SetMaxOpenConns(20)
- writeUserDb.LogMode(true)
-
- }
-
- func XTReadDB() *gorm.DB {
- return readDb
- }
- func XTWriteDB() *gorm.DB {
- return writeDb
- }
- func UserReadDB() *gorm.DB {
- return readUserDb
- }
- func UserWriteDB() *gorm.DB {
- return writeUserDb
- }
|