background/db/sqlManager.go

38 lines
821 B
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package db
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
2019-04-07 04:25:07 +00:00
"background/config"
2019-01-25 09:11:15 +00:00
)
var gDb Database
2019-05-16 10:05:20 +00:00
var blogDb Database
2019-01-25 09:11:15 +00:00
2019-03-07 02:36:09 +00:00
func Init() {
2019-01-25 09:11:15 +00:00
mysqlconf := config.GetMysqlConfig()
2019-05-16 10:05:20 +00:00
blogConf := config.GetMysqlBlogConfig()
gDb = Database{Type: string(""), DB: initMysql(mysqlconf)}
blogDb = Database{Type: string(""), DB: initMysql(blogConf)}
}
func initMysql(mysqlconf *config.MysqlConfig) *sql.DB {
2019-03-07 02:36:09 +00:00
cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
2019-01-25 09:11:15 +00:00
}
e := _db.Ping()
2019-03-07 02:36:09 +00:00
if nil != e {
2019-01-25 09:11:15 +00:00
fmt.Println(e.Error())
}
2019-05-16 10:05:20 +00:00
return _db
2019-01-25 09:11:15 +00:00
}
func GetMysqlClient() *Database {
return &gDb
}
2019-05-16 10:05:20 +00:00
func GetBlogMysql() *Database{
return &blogDb
}