123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package dao
- import (
- "fmt"
- "gorm.io/driver/sqlserver"
- "os"
- "time"
- //_ "github.com/denisenkom/go-mssqldb"
- "gorm.io/gorm/logger"
- "gorm.io/gorm/schema"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- // _ "github.com/jinzhu/gorm/dialects/mysql"
- // "github.com/jinzhu/gorm"
- "kpt.xdmy/apiserver/config"
- "kpt.xdmy/apiserver/routers/restful"
- "kpt.xdmy/pkg/log"
- "kpt.xdmy/pkg/setting"
- )
- var D *Dao
- type Dao struct {
- DB *gorm.DB
- }
- var SqlServerDB *SqlServer
- type SqlServer struct {
- DB *gorm.DB
- }
- func New(conf *config.Config) *Dao {
- newLogger := logger.New(
- log.New(os.Stdout, "\r\n"), // io writer
- logger.Config{
- SlowThreshold: time.Second, // Slow SQL threshold
- LogLevel: logger.Error, // Log level
- IgnoreRecordNotFoundError: false, // Ignore ErrRecordNotFound error for logger
- Colorful: true, // Disable color
- // LogMode:
- })
- gc := &gorm.Config{
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true,
- NoLowerCase: true,
- },
- //Logger: newLogger,
- Logger: logger.Default.LogMode(logger.Silent),
- }
- db, err := gorm.Open(mysql.Open(conf.DB.DSN), gc)
- tx := db.Session(&gorm.Session{Logger: newLogger})
- if err != nil {
- log.Error("model.Setup err: %v", err, conf.DB.DSN)
- }
- sqlDB, _ := db.DB()
- // SetMaxOpenConns 设置打开数据库连接的最大数量。
- sqlDB.SetMaxOpenConns(100)
- // SetMaxIdleConns 设置空闲连接池中连接的最大数量
- sqlDB.SetMaxIdleConns(10)
- // SetConnMaxLifetime 设置了连接可复用的最大时间。
- sqlDB.SetConnMaxLifetime(300 * time.Second)
- D = &Dao{
- DB: tx,
- }
- return D
- }
- func Setup() {
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
- setting.DatabaseSetting.User,
- setting.DatabaseSetting.Password,
- setting.DatabaseSetting.Host,
- setting.DatabaseSetting.Name)
- // restful 接口的 数据库初始化
- err := restful.SQLInit("mysql", dsn, 500, 50, setting.DatabaseSetting.ShowXormlog)
- if err != nil {
- log.Fatal("dao Setup ", err)
- fmt.Println("dbs 数据库初始化失败 ")
- }
- if err != nil {
- log.Fatal("dao Setup ", err)
- }
- }
- func NewSqlServer(conf *config.Config) *SqlServer {
- sqlserverSourceName := fmt.Sprintf("server=%s;user id=%s;password=%s;database=%s;encrypt=disable", setting.SQlServerSetting.Host,
- setting.SQlServerSetting.User, setting.SQlServerSetting.Password, setting.SQlServerSetting.Name)
- newLogger := logger.New(
- log.New(os.Stdout, "\r\n"), // io writer
- logger.Config{
- SlowThreshold: time.Second, // Slow SQL threshold
- LogLevel: logger.Error, // Log level
- IgnoreRecordNotFoundError: false, // Ignore ErrRecordNotFound error for logger
- Colorful: true, // Disable color
- // LogMode:
- })
- gc := &gorm.Config{
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true,
- NoLowerCase: true,
- },
- //Logger: newLogger,
- Logger: logger.Default.LogMode(logger.Silent),
- }
- db, err := gorm.Open(sqlserver.Open(sqlserverSourceName), gc)
- tx := db.Session(&gorm.Session{Logger: newLogger})
- if err != nil {
- log.Error("model.Setup err: %v", err, conf.DB.DSN)
- }
- sqlDB, _ := db.DB()
- // SetMaxOpenConns 设置打开数据库连接的最大数量。
- sqlDB.SetMaxOpenConns(100)
- // SetMaxIdleConns 设置空闲连接池中连接的最大数量
- sqlDB.SetMaxIdleConns(10)
- // SetConnMaxLifetime 设置了连接可复用的最大时间。
- sqlDB.SetConnMaxLifetime(300 * time.Second)
- SqlServerDB = &SqlServer{
- DB: tx,
- }
- return SqlServerDB
- }
|