dao.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package dao
  2. import (
  3. "fmt"
  4. "gorm.io/driver/sqlserver"
  5. "os"
  6. "time"
  7. //_ "github.com/denisenkom/go-mssqldb"
  8. "gorm.io/gorm/logger"
  9. "gorm.io/gorm/schema"
  10. "gorm.io/driver/mysql"
  11. "gorm.io/gorm"
  12. // _ "github.com/jinzhu/gorm/dialects/mysql"
  13. // "github.com/jinzhu/gorm"
  14. "kpt.xdmy/apiserver/config"
  15. "kpt.xdmy/apiserver/routers/restful"
  16. "kpt.xdmy/pkg/log"
  17. "kpt.xdmy/pkg/setting"
  18. )
  19. var D *Dao
  20. type Dao struct {
  21. DB *gorm.DB
  22. }
  23. var SqlServerDB *SqlServer
  24. type SqlServer struct {
  25. DB *gorm.DB
  26. }
  27. func New(conf *config.Config) *Dao {
  28. newLogger := logger.New(
  29. log.New(os.Stdout, "\r\n"), // io writer
  30. logger.Config{
  31. SlowThreshold: time.Second, // Slow SQL threshold
  32. LogLevel: logger.Error, // Log level
  33. IgnoreRecordNotFoundError: false, // Ignore ErrRecordNotFound error for logger
  34. Colorful: true, // Disable color
  35. // LogMode:
  36. })
  37. gc := &gorm.Config{
  38. NamingStrategy: schema.NamingStrategy{
  39. SingularTable: true,
  40. NoLowerCase: true,
  41. },
  42. //Logger: newLogger,
  43. Logger: logger.Default.LogMode(logger.Silent),
  44. }
  45. db, err := gorm.Open(mysql.Open(conf.DB.DSN), gc)
  46. tx := db.Session(&gorm.Session{Logger: newLogger})
  47. if err != nil {
  48. log.Error("model.Setup err: %v", err, conf.DB.DSN)
  49. }
  50. sqlDB, _ := db.DB()
  51. // SetMaxOpenConns 设置打开数据库连接的最大数量。
  52. sqlDB.SetMaxOpenConns(100)
  53. // SetMaxIdleConns 设置空闲连接池中连接的最大数量
  54. sqlDB.SetMaxIdleConns(10)
  55. // SetConnMaxLifetime 设置了连接可复用的最大时间。
  56. sqlDB.SetConnMaxLifetime(300 * time.Second)
  57. D = &Dao{
  58. DB: tx,
  59. }
  60. return D
  61. }
  62. func Setup() {
  63. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
  64. setting.DatabaseSetting.User,
  65. setting.DatabaseSetting.Password,
  66. setting.DatabaseSetting.Host,
  67. setting.DatabaseSetting.Name)
  68. // restful 接口的 数据库初始化
  69. err := restful.SQLInit("mysql", dsn, 500, 50, setting.DatabaseSetting.ShowXormlog)
  70. if err != nil {
  71. log.Fatal("dao Setup ", err)
  72. fmt.Println("dbs 数据库初始化失败 ")
  73. }
  74. if err != nil {
  75. log.Fatal("dao Setup ", err)
  76. }
  77. }
  78. func NewSqlServer(conf *config.Config) *SqlServer {
  79. sqlserverSourceName := fmt.Sprintf("server=%s;user id=%s;password=%s;database=%s;encrypt=disable", setting.SQlServerSetting.Host,
  80. setting.SQlServerSetting.User, setting.SQlServerSetting.Password, setting.SQlServerSetting.Name)
  81. newLogger := logger.New(
  82. log.New(os.Stdout, "\r\n"), // io writer
  83. logger.Config{
  84. SlowThreshold: time.Second, // Slow SQL threshold
  85. LogLevel: logger.Error, // Log level
  86. IgnoreRecordNotFoundError: false, // Ignore ErrRecordNotFound error for logger
  87. Colorful: true, // Disable color
  88. // LogMode:
  89. })
  90. gc := &gorm.Config{
  91. NamingStrategy: schema.NamingStrategy{
  92. SingularTable: true,
  93. NoLowerCase: true,
  94. },
  95. //Logger: newLogger,
  96. Logger: logger.Default.LogMode(logger.Silent),
  97. }
  98. db, err := gorm.Open(sqlserver.Open(sqlserverSourceName), gc)
  99. tx := db.Session(&gorm.Session{Logger: newLogger})
  100. if err != nil {
  101. log.Error("model.Setup err: %v", err, conf.DB.DSN)
  102. }
  103. sqlDB, _ := db.DB()
  104. // SetMaxOpenConns 设置打开数据库连接的最大数量。
  105. sqlDB.SetMaxOpenConns(100)
  106. // SetMaxIdleConns 设置空闲连接池中连接的最大数量
  107. sqlDB.SetMaxIdleConns(10)
  108. // SetConnMaxLifetime 设置了连接可复用的最大时间。
  109. sqlDB.SetConnMaxLifetime(300 * time.Second)
  110. SqlServerDB = &SqlServer{
  111. DB: tx,
  112. }
  113. return SqlServerDB
  114. }