database.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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-task/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. maxIdleInt, _ := conf.Mysql.MaxIdle.Int64()
  22. sqlDB.SetMaxIdleConns(int(maxIdleInt))
  23. }
  24. if mysqlConfig.MaxConn != conf.Mysql.MaxConn {
  25. maxConnInt, _ := conf.Mysql.MaxConn.Int64()
  26. sqlDB.SetMaxOpenConns(int(maxConnInt))
  27. }
  28. if mysqlConfig.LogMode != conf.Mysql.LogMode {
  29. database.DB().Debug()
  30. }
  31. return // 这里直接返回
  32. }
  33. // 地址用户名密码不一致,关闭连接重新连接
  34. _ = sqlDB.Close()
  35. }
  36. // 私有db为空使用公共db
  37. dbname := conf.Rpc.AdmTask.MysqlDb
  38. if dbname == "" {
  39. dbname = conf.Mysql.Db
  40. }
  41. maxIdleInt, _ := conf.Mysql.MaxIdle.Int64()
  42. maxConnInt, _ := conf.Mysql.MaxConn.Int64()
  43. // 连接database
  44. database.Setup(conf.Mysql.User,
  45. conf.Mysql.Password,
  46. conf.Mysql.Addr,
  47. dbname,
  48. conf.Mysql.Charset,
  49. int(maxIdleInt),
  50. int(maxConnInt),
  51. conf.Mysql.LogMode != "prod")
  52. mysqlConfig = conf.Mysql
  53. }