database.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package parser
  4. import (
  5. "adm-data-gateway/config"
  6. "git.getensh.com/common/gopkgsv2/database"
  7. )
  8. var mysqlConfig config.MysqlConfig
  9. func MysqlHandler(conf *config.Configure) {
  10. // mysqlConfig.Addr 不为空表示已经初始化
  11. if mysqlConfig.Addr != "" {
  12. sqlDB, err := database.DB().DB()
  13. if err != nil {
  14. return
  15. }
  16. // 地址,用户名,密码相同比较其他配置是否一致
  17. if mysqlConfig.User == conf.Mysql.User &&
  18. mysqlConfig.Password == conf.Mysql.Password &&
  19. mysqlConfig.Addr == conf.Mysql.Addr {
  20. if mysqlConfig.MaxIdle != conf.Mysql.MaxIdle {
  21. sqlDB.SetMaxIdleConns(conf.Mysql.MaxIdle)
  22. }
  23. if mysqlConfig.MaxConn != conf.Mysql.MaxConn {
  24. sqlDB.SetMaxOpenConns(conf.Mysql.MaxConn)
  25. }
  26. if mysqlConfig.LogMode != conf.Mysql.LogMode {
  27. database.DB().Debug()
  28. }
  29. return // 这里直接返回
  30. }
  31. // 地址用户名密码不一致,关闭连接重新连接
  32. _ = sqlDB.Close()
  33. }
  34. // 私有db为空使用公共db
  35. dbname := conf.ADMDataGateway.MysqlDb
  36. if dbname == "" {
  37. dbname = conf.Mysql.DB
  38. }
  39. // 连接database
  40. database.Setup(conf.Mysql.User,
  41. conf.Mysql.Password,
  42. conf.Mysql.Addr,
  43. dbname,
  44. conf.Mysql.Charset,
  45. conf.Mysql.MaxIdle,
  46. conf.Mysql.MaxConn,
  47. conf.Mysql.LogMode)
  48. mysqlConfig = conf.Mysql
  49. }