ms_db.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 dataBase *gorm.DB
  25. var (
  26. dbName = "local"
  27. user = beego.AppConfig.DefaultString(dbName+"::user", "sa")
  28. password = beego.AppConfig.DefaultString(dbName+"::password", "hy123456")
  29. host = beego.AppConfig.DefaultString(dbName+"::host", "192.168.10.10")
  30. port = beego.AppConfig.DefaultInt(dbName+"::port", 1633)
  31. dbname = beego.AppConfig.DefaultString(dbName+"::dbname", "hyDB")
  32. )
  33. func ConnectSHMSDB() {
  34. connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
  35. host, user, password, port, dbname)
  36. fmt.Println(connectionString)
  37. db, err := gorm.Open("mssql", connectionString)
  38. if err != nil {
  39. fmt.Println("failed to connect database")
  40. fmt.Println(err)
  41. }
  42. db.LogMode(true)
  43. db.CommonDB()
  44. dataBase = db
  45. }