database.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "property-task/config"
  6. "git.getensh.com/common/gopkgs/database"
  7. )
  8. var mysqlConfig config.MysqlConfig
  9. func MysqlHandler(conf *config.Configure) {
  10. // mysqlConfig.Addr 不为空表示已经初始化
  11. if mysqlConfig.Addr != "" {
  12. // 地址,用户名,密码相同比较其他配置是否一致
  13. sqlDB, _ := database.DB().DB()
  14. if mysqlConfig.User == conf.Mysql.User &&
  15. mysqlConfig.Password == conf.Mysql.Password &&
  16. mysqlConfig.Addr == conf.Mysql.Addr {
  17. if mysqlConfig.MaxIdle != conf.Mysql.MaxIdle {
  18. sqlDB.SetMaxIdleConns(conf.Mysql.MaxIdle)
  19. }
  20. if mysqlConfig.MaxConn != conf.Mysql.MaxConn {
  21. sqlDB.SetMaxOpenConns(conf.Mysql.MaxConn)
  22. }
  23. if mysqlConfig.LogMode != conf.Mysql.LogMode {
  24. //database.DB().LogMode(conf.Mysql.LogMode)
  25. }
  26. return // 这里直接返回
  27. }
  28. // 地址用户名密码不一致,关闭连接重新连接
  29. sqlDB.Close()
  30. }
  31. // 私有db为空使用公共db
  32. dbname := conf.Rpc.PropertyTask.MysqlDb
  33. if dbname == "" {
  34. dbname = conf.Mysql.DB
  35. }
  36. // 连接database
  37. database.Setup(conf.Mysql.User,
  38. conf.Mysql.Password,
  39. conf.Mysql.Addr,
  40. dbname,
  41. conf.Mysql.Charset,
  42. conf.Mysql.MaxIdle,
  43. conf.Mysql.MaxConn,
  44. conf.Mysql.LogMode)
  45. mysqlConfig = conf.Mysql
  46. }