123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- // 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"
- "sws_xcx/utils"
-
- "github.com/astaxie/beego"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
- )
-
- var readDb *gorm.DB
- var writeDb *gorm.DB
-
- var readUserDb *gorm.DB
- var readXtDb *gorm.DB
-
- // var writeUserDb *gorm.DB
-
- // var readMiddleDb *gorm.DB
- // var writeMiddleDb *gorm.DB
-
- // var readPatientDb *gorm.DB
- // var writePatientDb *gorm.DB
-
- // var readSgjPatientDb *gorm.DB
- // var writeSgjPatientDb *gorm.DB
-
- // var readUserDbT *gorm.DB
- // var writeUserDbT *gorm.DB
-
- var err error
-
- func ConnectDB() {
- readHost := beego.AppConfig.String("readmysqlhost")
- readPort := beego.AppConfig.String("readmysqlport")
- readUser := beego.AppConfig.String("readmysqluser")
- readPass := beego.AppConfig.String("readmysqlpass")
- readName := beego.AppConfig.String("readmysqlname")
-
- writeHost := beego.AppConfig.String("writemysqlhost")
- writePort := beego.AppConfig.String("writemysqlport")
- writeUser := beego.AppConfig.String("writemysqluser")
- writePass := beego.AppConfig.String("writemysqlpass")
- writeName := beego.AppConfig.String("writemysqlname")
-
- // readUserHost := beego.AppConfig.String("readuserhost")
- // readUserPort := beego.AppConfig.String("readuserport")
- // readUserUser := beego.AppConfig.String("readuseruser")
- // readUserPass := beego.AppConfig.String("readuserpass")
- // readUserName := beego.AppConfig.String("readusername")
-
- readXtHost := beego.AppConfig.String("readxthost")
- readXtPort := beego.AppConfig.String("readxtport")
- readXtUser := beego.AppConfig.String("readxtuser")
- readXtPass := beego.AppConfig.String("readxtpass")
- readXtName := beego.AppConfig.String("readxtname")
-
- // writeUserHost := beego.AppConfig.String("writeuserhost")
- // writeUserPort := beego.AppConfig.String("writeuserport")
- // writeUserUser := beego.AppConfig.String("writeuseruser")
- // writeUserPass := beego.AppConfig.String("writeuserpass")
- // writeUserName := beego.AppConfig.String("writeusername")
-
- // readMiddleHost := beego.AppConfig.String("readmiddlehost")
- // readMiddlePort := beego.AppConfig.String("readmiddleport")
- // readMiddleUser := beego.AppConfig.String("readmiddleuser")
- // readMiddlePass := beego.AppConfig.String("readmiddlepass")
- // readMiddleName := beego.AppConfig.String("readmiddlename")
-
- // writeMiddleHost := beego.AppConfig.String("writemiddlehost")
- // writeMiddlePort := beego.AppConfig.String("writemiddleport")
- // writeMiddleUser := beego.AppConfig.String("writemiddleuser")
- // writeMiddlePass := beego.AppConfig.String("writemiddlepass")
- // writeMiddleName := beego.AppConfig.String("writemiddlename")
-
- // readPatientHost := beego.AppConfig.String("readpatienthost")
- // readPatientPort := beego.AppConfig.String("readpatientport")
- // readPatientUser := beego.AppConfig.String("readpatientuser")
- // readPatientPass := beego.AppConfig.String("readpatientpass")
- // readPatientName := beego.AppConfig.String("readpatientname")
-
- // writePatientHost := beego.AppConfig.String("writepatienthost")
- // writePatientPort := beego.AppConfig.String("writepatientport")
- // writePatientUser := beego.AppConfig.String("writepatientuser")
- // writePatientPass := beego.AppConfig.String("writepatientpass")
- // writePatientName := beego.AppConfig.String("writepatientname")
-
- // readSgjPatientHost := beego.AppConfig.String("readsgjpatientmysqlhost")
- // readSgjPatientPort := beego.AppConfig.String("readsgjpatientmysqlport")
- // readSgjPatientUser := beego.AppConfig.String("readsgjpatientmysqluser")
- // readSgjPatientPass := beego.AppConfig.String("readsgjpatientmysqlpass")
- // readSgjPatientName := beego.AppConfig.String("readsgjpatientmysqlname")
-
- // writeSgjPatientHost := beego.AppConfig.String("writesgjpatientmysqlhost")
- // writeSgjPatientPort := beego.AppConfig.String("writesgjpatientmysqlport")
- // writeSgjPatientUser := beego.AppConfig.String("writesgjpatientmysqluser")
- // writeSgjPatientPass := beego.AppConfig.String("writesgjpatientmysqlpass")
- // writeSgjPatientName := beego.AppConfig.String("writesgjpatientmysqlname")
-
- // readUserHostT := beego.AppConfig.String("readuserhostTest")
- // readUserPortT := beego.AppConfig.String("readuserportTest")
- // readUserUserT := beego.AppConfig.String("readuseruserTest")
- // readUserPassT := beego.AppConfig.String("readuserpassTest")
- // readUserNameT := beego.AppConfig.String("readusernameTest")
-
- // writeUserHostT := beego.AppConfig.String("writeuserhostTest")
- // writeUserPortT := beego.AppConfig.String("writeuserportTest")
- // writeUserUserT := beego.AppConfig.String("writeuseruserTest")
- // writeUserPassT := beego.AppConfig.String("writeuserpassTest")
- // writeUserNameT := beego.AppConfig.String("writeusernameTest")
-
- rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", readUser, readPass, readHost, readPort, readName)
- wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", writeUser, writePass, writeHost, writePort, writeName)
-
- //rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
- rxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readXtUser, readXtPass, readXtHost, readXtPort, readXtName)
- // wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
-
- // rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
- // wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
-
- // rpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readPatientUser, readPatientPass, readPatientHost, readPatientPort, readPatientName)
- // wpdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writePatientUser, writePatientPass, writePatientHost, writePatientPort, writePatientName)
-
- // rspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readSgjPatientUser, readSgjPatientPass, readSgjPatientHost, readSgjPatientPort, readSgjPatientName)
- // wspdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeSgjPatientUser, writeSgjPatientPass, writeSgjPatientHost, writeSgjPatientPort, writeSgjPatientName)
-
- // rudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUserT, readUserPassT, readUserHostT, readUserPortT, readUserNameT)
- // wudsnT := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUserT, writeUserPassT, writeUserHostT, writeUserPortT, writeUserNameT)
-
- readDb, err = gorm.Open("mysql", rdsn)
- if err != nil {
- utils.ErrorLog("init DB err %v", err)
- }
- readDb.DB().SetMaxIdleConns(10)
- readDb.DB().SetMaxOpenConns(100)
- readDb.LogMode(true)
-
- writeDb, err = gorm.Open("mysql", wdsn)
- if err != nil {
- utils.ErrorLog("init DB err %v", err)
- }
- writeDb.DB().SetMaxIdleConns(10)
- writeDb.DB().SetMaxOpenConns(100)
- writeDb.LogMode(true)
-
- // readUserDbT, err = gorm.Open("mysql", rudsnT)
- // if err != nil {
- // //beego.Error(err)
- // }
- // readUserDbT.DB().SetMaxIdleConns(10)
- // readUserDbT.DB().SetMaxOpenConns(100)
- // readUserDbT.LogMode(true)
-
- // writeUserDbT, err = gorm.Open("mysql", wudsnT)
- // if err != nil {
- // //beego.Error(err)
- // }
- // writeUserDbT.DB().SetMaxIdleConns(10)
- // writeUserDbT.DB().SetMaxOpenConns(100)
- // writeUserDbT.LogMode(true)
-
- // readUserDb, err = gorm.Open("mysql", rudsn)
- // if err != nil {
- // utils.ErrorLog("init DB err %v", err)
- // }
- // readUserDb.DB().SetMaxIdleConns(10)
- // readUserDb.DB().SetMaxOpenConns(100)
- // readUserDb.LogMode(true)
-
- readXtDb, err = gorm.Open("mysql", rxdsn)
- if err != nil {
- utils.ErrorLog("init DB err %v", err)
- }
- readXtDb.DB().SetMaxIdleConns(10)
- readXtDb.DB().SetMaxOpenConns(100)
- readXtDb.LogMode(true)
-
- // writeUserDb, err = gorm.Open("mysql", wudsn)
- // if err != nil {
- // //beego.Error(err)
- // }
- // writeUserDb.DB().SetMaxIdleConns(10)
- // writeUserDb.DB().SetMaxOpenConns(100)
- // writeUserDb.LogMode(true)
-
- // readMiddleDb, err = gorm.Open("mysql", rmdsn)
- // if err != nil {
- // //beego.Error(err)
- // }
- // readMiddleDb.DB().SetMaxIdleConns(10)
- // readMiddleDb.DB().SetMaxOpenConns(100)
- // readMiddleDb.LogMode(true)
-
- // writeMiddleDb, err = gorm.Open("mysql", wmdsn)
- // if err != nil {
- // //beego.Error(err)
- // }
- // writeMiddleDb.DB().SetMaxIdleConns(10)
- // writeMiddleDb.DB().SetMaxOpenConns(100)
- // writeMiddleDb.LogMode(true)
-
- // readPatientDb, err = gorm.Open("mysql", rpdsn)
- // if err != nil {
- // beego.Error(err)
- // }
- // readPatientDb.DB().SetMaxIdleConns(10)
- // readPatientDb.DB().SetMaxOpenConns(100)
- // readPatientDb.LogMode(true)
-
- // writePatientDb, err = gorm.Open("mysql", wpdsn)
- // if err != nil {
- // beego.Error(err)
- // }
- // writePatientDb.DB().SetMaxIdleConns(10)
- // writePatientDb.DB().SetMaxOpenConns(100)
- // writePatientDb.LogMode(true)
-
- // readSgjPatientDb, err = gorm.Open("mysql", rspdsn)
- // if err != nil {
- // beego.Error(err)
- // }
- // readSgjPatientDb.DB().SetMaxIdleConns(10)
- // readSgjPatientDb.DB().SetMaxOpenConns(100)
- // readSgjPatientDb.LogMode(true)
-
- // writeSgjPatientDb, err = gorm.Open("mysql", wspdsn)
- // if err != nil {
- // beego.Error(err)
- // }
- // writeSgjPatientDb.DB().SetMaxIdleConns(10)
- // writeSgjPatientDb.DB().SetMaxOpenConns(100)
- // writeSgjPatientDb.LogMode(true)
- }
-
- //func DisconnectDB() {
- // if err := readDb.Close(); nil != err {
- // beego.Error("Disconnect from database failed: " + err.Error())
- // }
- //}
-
- func ReadDB() *gorm.DB {
- return readDb
- }
- func WriteDB() *gorm.DB {
- return writeDb
- }
-
- func ReadUserDB() *gorm.DB {
- return readUserDb
- }
-
- func ReadXtDB() *gorm.DB {
- return readXtDb
- }
-
- // func UserWriteDB() *gorm.DB {
- // return writeUserDb
- // }
-
- // func MiddleReadDB() *gorm.DB {
- // return readMiddleDb
- // }
- // func MiddleWriteDB() *gorm.DB {
- // return writeMiddleDb
- // }
-
- // func PatientReadDB() *gorm.DB {
- // return readPatientDb
- // }
-
- // func PatientWriteDB() *gorm.DB {
- // return writePatientDb
- // }
-
- // func SgjPatientReadDB() *gorm.DB {
- // return readSgjPatientDb
- // }
-
- // func SgjPatientWriteDB() *gorm.DB {
- // return writeSgjPatientDb
- // }
|