1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package parser
- import (
- "adm-task/config"
- "git.getensh.com/common/gopkgsv2/database"
- )
- var mysqlConfig config.MysqlConfig
- func MysqlHandler(conf *config.Configure) {
- // mysqlConfig.Addr 不为空表示已经初始化
- if mysqlConfig.Addr != "" {
- sqlDB, err := database.DB().DB()
- if err != nil {
- return
- }
- // 地址,用户名,密码相同比较其他配置是否一致
- if mysqlConfig.User == conf.Mysql.User &&
- mysqlConfig.Password == conf.Mysql.Password &&
- mysqlConfig.Addr == conf.Mysql.Addr {
- if mysqlConfig.MaxIdle != conf.Mysql.MaxIdle {
- maxIdleInt, _ := conf.Mysql.MaxIdle.Int64()
- sqlDB.SetMaxIdleConns(int(maxIdleInt))
- }
- if mysqlConfig.MaxConn != conf.Mysql.MaxConn {
- maxConnInt, _ := conf.Mysql.MaxConn.Int64()
- sqlDB.SetMaxOpenConns(int(maxConnInt))
- }
- if mysqlConfig.LogMode != conf.Mysql.LogMode {
- database.DB().Debug()
- }
- return // 这里直接返回
- }
- // 地址用户名密码不一致,关闭连接重新连接
- _ = sqlDB.Close()
- }
- // 私有db为空使用公共db
- dbname := conf.Rpc.AdmTask.MysqlDb
- if dbname == "" {
- dbname = conf.Mysql.Db
- }
- maxIdleInt, _ := conf.Mysql.MaxIdle.Int64()
- maxConnInt, _ := conf.Mysql.MaxConn.Int64()
- // 连接database
- database.Setup(conf.Mysql.User,
- conf.Mysql.Password,
- conf.Mysql.Addr,
- dbname,
- conf.Mysql.Charset,
- int(maxIdleInt),
- int(maxConnInt),
- conf.Mysql.LogMode != "prod")
- mysqlConfig = conf.Mysql
- }
|