Commit b2e372a1 by huangchengyi

Merge branch 'dev' of http://119.23.72.7/mushishixian/search_server into dev

# Conflicts:
#	test/test4.go
parents d113977f 3ca364bd
Showing with 24 additions and 32 deletions
......@@ -7,6 +7,8 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v2.0.1-0.20180401191855-9352ab68be13+incompatible
......@@ -33,8 +35,8 @@ require (
github.com/stretchr/testify v1.5.1 // indirect
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
github.com/urfave/cli v1.22.4 // indirect
github.com/uniplaces/carbon v0.1.6
github.com/urfave/cli v1.22.4 // indirect
go.etcd.io/bbolt v1.3.4
go.uber.org/zap v1.14.1 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
......
package mysql
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/go-xorm/xorm"
_ "github.com/go-sql-driver/mysql"
"search_server/pkg/config"
"search_server/pkg/e"
)
//mysql相关
var mysqlDb, bomDb *gorm.DB
var DatabaseConMap map[string]*xorm.Engine
var DatabaseConMap = map[string]*gorm.DB{
"mysql": mysqlDb,
"bom": bomDb,
}
func Setup() error {
func _setup() error{
DatabaseConMap = make(map[string]*xorm.Engine,0)
DatabaseList := config.BuildDatabaseList()
var err error
//循环生成数据库链接
......@@ -26,34 +23,27 @@ func Setup() error {
host := db.Host
database := db.Database
if db.Prefix != "" {
gorm.DefaultTableNameHandler = func(DB *gorm.DB, defaultTableName string) string {
return db.Prefix + defaultTableName
}
}
dataSourceName := userName + ":" + password + "@tcp(" + host + ")/" + database + "?charset=utf8"
DatabaseConMap[conName], err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local",
userName, password, host, database))
if err != nil {
DatabaseConMap[conName], err = xorm.NewEngine("mysql", dataSourceName)
if err!=nil{
return e.NewFatalError(err.Error()) //这里返回致命异常
}
DatabaseConMap[conName].SingularTable(true)
if db.MaxIdleCons == 0 {
db.MaxIdleCons = 10
}
if db.MaxOpenCons == 0 {
db.MaxOpenCons = 100
}
DatabaseConMap[conName].DB().SetMaxIdleConns(db.MaxIdleCons)
DatabaseConMap[conName].DB().SetMaxOpenConns(db.MaxOpenCons)
if config.Get("gorm.mode").String() == "debug" {
DatabaseConMap[conName].LogMode(true)
}
//日志打印SQL
DatabaseConMap[conName].ShowSQL(true)
//设置连接池的空闲数大小
DatabaseConMap[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
DatabaseConMap[conName].SetMaxOpenConns(db.MaxOpenCons)
}
return nil
}
func Connection(conName string) *gorm.DB {
func Conn(conName string) *xorm.Engine {
return DatabaseConMap[conName]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment