12345678910111213141516171819202122232425262728293031323334353637 |
- package lib
- import (
- . "jjl-tools/tinodeService/config"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql"
- "log"
- "sync"
- )
- var dblock *sync.Mutex = &sync.Mutex{}
- var tiDbClient *gorm.DB
- var esOnce sync.Once
- //GetTiNodeDbInstance 数据库连接实例
- func GetTiNodeDbInstance() *gorm.DB {
- var err error
- esOnce.Do(func() {
- config := GlobalConfig
- tiDbClient, err = gorm.Open("mysql", config.DbConnect)
- if err != nil {
- log.Panic(err.Error())
- }
- //设置闲置的连接数
- tiDbClient.DB().SetMaxIdleConns(10)
- //设置最大打开的连接数,默认值为0表示不限制
- tiDbClient.DB().SetMaxOpenConns(100)
- //如果设置为true,`User`的默认表名为`user`,使用`TableName`设置的表名不受影响
- tiDbClient.SingularTable(true)
- })
- return tiDbClient
- }
- func ColumnIncr(column string) interface{} {
- return gorm.Expr(column+" + ?", 1)
- }
|