41 lines
756 B
Go
41 lines
756 B
Go
package model
|
|
|
|
import (
|
|
"git.hpds.cc/Component/logging"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"go.uber.org/zap"
|
|
"os"
|
|
|
|
"xorm.io/xorm"
|
|
"xorm.io/xorm/dialects"
|
|
)
|
|
|
|
var (
|
|
DB *xorm.Engine
|
|
Logger *logging.Logger
|
|
)
|
|
|
|
func New(driveName, dsn string) {
|
|
DB, _ = NewDbConnection(driveName, dsn)
|
|
DB.ShowSQL(true)
|
|
DB.Dialect().SetQuotePolicy(dialects.QuotePolicyReserved)
|
|
err := DB.Sync2(
|
|
&Node{},
|
|
&NodeState{},
|
|
)
|
|
if err != nil {
|
|
zap.L().Error("同步数据库表结构", zap.Error(err))
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func NewDbConnection(driveName, dsn string) (db *xorm.Engine, err error) {
|
|
db, err = xorm.NewEngine(driveName, dsn)
|
|
if err != nil {
|
|
zap.L().Error("创建数据库连接", zap.Error(err))
|
|
os.Exit(-1)
|
|
}
|
|
db.SetMaxOpenConns(300)
|
|
return
|
|
}
|