1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mysql.go 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-09-17 16:31 . feat: rebuild log
// 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", mysqlHandler)
}
func mysqlHandler(d *DatasourceProcessor, config data.Datasource) *sql.DB {
switch config.GetTunnel() {
case "ssh":
mysql.RegisterDialContext("mysql+ssh", d.sshDialer.Dial)
case "tcp":
mysql.RegisterDialContext("mysql+tcp", d.tcpDialer.Dial)
}
dsn := fmt.Sprintf("%s:%s@mysql+%s(%s)%s",
config.GetUsername(), config.GetPassword(), config.GetTunnel(), config.GetHost(), config.GetDatabase())
conn, err := sql.Open("mysql", dsn)
if err != nil {
d.Log.Panic("Failed to open mysql connection:\ncaused by:\ndsn: %s\n%s", dsn, err.Error())
}
if !config.IsLazyFetch() {
if err = conn.Ping(); err != nil {
d.Log.Panic("ping mysql failed:\ncaused by:\n%s", err.Error())
}
}
initConn(config, conn)
return conn
}
func initConn(config data.Datasource, conn *sql.DB) {
if config.GetConnMaxLifeTime() != 0 {
conn.SetConnMaxLifetime(time.Second * time.Duration(config.GetConnMaxLifeTime()))
}
if config.GetConnMaxIdleTime() != 0 {
conn.SetConnMaxIdleTime(time.Second * time.Duration(config.GetConnMaxIdleTime()))
}
if config.GetMaxIdleConns() != 0 {
conn.SetMaxIdleConns(config.GetMaxIdleConns())
}
if config.GetMaxOpenConns() != 0 {
conn.SetMaxOpenConns(config.GetMaxOpenConns())
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.7

搜索帮助