1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mysql.go 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-04-16 18:55 . feat: add casbin
// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/**
*@describe:
*@author wfl19/Kristas
*@date 2021/04/10
*/
package datasource_processor
import (
"database/sql"
"fmt"
"gitee.com/kristas/booting-go/framework/data"
"github.com/go-sql-driver/mysql"
"time"
)
func init() {
UseDataBaseHandler("mysql", buildMysqlConn)
}
func buildMysqlConn(d *DatasourceProcessor, configure data.Configure) data.DatasourceWrapper {
switch configure.Config().Tunnel {
case "ssh":
mysql.RegisterDialContext("mysql+ssh", d.sshDialer.Dial)
case "tcp":
mysql.RegisterDialContext("mysql+tcp", d.tcpDialer.Dial)
}
config := configure.Config()
dsn := fmt.Sprintf("%s:%s@mysql+%s(%s)%s",
config.Username, config.Password, config.Tunnel, config.Host, config.Database)
conn, err := sql.Open("mysql", dsn)
if err != nil {
d.Log.Panicf("Failed to open mysql connection:\ncaused by:\ndsn: %s\n%s", dsn, err.Error())
}
if err = conn.Ping(); err != nil {
d.Log.Panicf("ping mysql failed:\ncaused by:\n%s", err.Error())
}
initConn(config, conn)
d.Log.Info("build mysql connection")
datasourceWrapper := data.NewDatasourceWrapper(conn, configure)
return datasourceWrapper
}
func initConn(config data.Config, conn *sql.DB) {
if config.ConnMaxLifeTime != 0 {
conn.SetConnMaxLifetime(time.Second * time.Duration(config.ConnMaxLifeTime))
}
if config.ConnMaxIdleTime != 0 {
conn.SetConnMaxIdleTime(time.Second * time.Duration(config.ConnMaxIdleTime))
}
if config.MaxIdleConns != 0 {
conn.SetMaxIdleConns(config.MaxIdleConns)
}
if config.MaxOpenConns != 0 {
conn.SetMaxOpenConns(config.MaxOpenConns)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.2.6

搜索帮助