1 Star 0 Fork 231

jeArts/syncd

forked from dreamans/syncd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
db.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
dreamans 提交于 2019-01-01 00:30 . 调整代码结构
// Copyright 2018 syncd Author. All Rights Reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package syncd
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
)
type DB struct {
DbHandler *gorm.DB
cfg *DbConfig
}
func NewDatabase(cfg *DbConfig) *DB {
return &DB{
cfg: cfg,
}
}
func (db *DB) Open() error {
c, err := gorm.Open("mysql", db.parseConnConfig())
if err != nil {
return err
}
//c.LogMode(true)
c.DB().SetMaxIdleConns(db.cfg.MaxIdleConns)
c.DB().SetMaxOpenConns(db.cfg.MaxOpenConns)
c.DB().SetConnMaxLifetime(time.Second * time.Duration(db.cfg.ConnMaxLifeTime))
db.DbHandler = c
return nil
}
func (db *DB) Close() {
db.DbHandler.Close()
}
func (db *DB) parseConnConfig() string {
var connHost string
if db.cfg.Unix != "" {
connHost = fmt.Sprintf("unix(%s)", db.cfg.Unix)
} else {
connHost = fmt.Sprintf("tcp(%s:%s)", db.cfg.Host, db.cfg.Port)
}
s := fmt.Sprintf("%s:%s@%s/%s?charset=%s&parseTime=True&loc=Local", db.cfg.User, db.cfg.Pass, connHost, db.cfg.DbName, db.cfg.Charset)
return s
}
func (db *DB) GetTablePrefix() string {
return db.cfg.TablePrefix
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/arts1986/syncd.git
git@gitee.com:arts1986/syncd.git
arts1986
syncd
syncd
master

搜索帮助