database.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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-garden/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.Garden.MysqlDb
  33. if dbname == "" {
  34. dbname = conf.Mysql.DB
  35. }
  36. conf.Mysql.LogMode = true
  37. // 连接database
  38. database.Setup(conf.Mysql.User,
  39. conf.Mysql.Password,
  40. conf.Mysql.Addr,
  41. dbname,
  42. conf.Mysql.Charset,
  43. conf.Mysql.MaxIdle,
  44. conf.Mysql.MaxConn,
  45. conf.Mysql.LogMode)
  46. mysqlConfig = conf.Mysql
  47. }