dm.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package init_serve
  2. import (
  3. "database/sql/driver"
  4. _ "dm"
  5. dm "dmgorm2"
  6. "eta_gn/eta_bridge/global"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. "gorm.io/gorm/schema"
  11. "io"
  12. "log"
  13. "os"
  14. "time"
  15. _ "github.com/go-sql-driver/mysql"
  16. )
  17. type LocalTime time.Time
  18. func InitDm() {
  19. dmSqlMap := make(map[string]*gorm.DB)
  20. logWriter := io.MultiWriter(global.MYSQL_LOG) //binlog日志,记录到文件中去
  21. if global.CONFIG.Mysql.Stdout {
  22. logWriter = io.MultiWriter(global.MYSQL_LOG, os.Stdout)
  23. }
  24. newLogger := logger.New(log.New(logWriter, "\r\n", log.LstdFlags), logger.Config{
  25. SlowThreshold: 200 * time.Millisecond, //慢sql :200ms
  26. LogLevel: logger.Info, //记录的日志类型,info代表所有信息都记录
  27. IgnoreRecordNotFoundError: true, //是否忽略找不到数据错误信息(只是日志记录记录成err还是普通的输出的区别,并不影响业务代码中的:找不到数据行error)
  28. Colorful: false, //是否颜色输出
  29. })
  30. mysqlConf := global.CONFIG.Mysql
  31. if len(mysqlConf.List) <= 0 {
  32. global.LOG.Error("mysql链接未配置")
  33. panic(fmt.Errorf("mysql链接未配置"))
  34. }
  35. for _, conf := range mysqlConf.List {
  36. isDefault := false
  37. if mysqlConf.DefaultDsnAliasName == conf.AliasName {
  38. isDefault = true
  39. }
  40. connectDm(conf.Dsn, conf.AliasName, newLogger, dmSqlMap, isDefault)
  41. }
  42. global.MYSQL = dmSqlMap
  43. }
  44. func connectDm(dsn, aliasName string, newLogger logger.Interface, dmSqlMap map[string]*gorm.DB, isDefault bool) {
  45. if dsn == `` {
  46. return
  47. }
  48. db, err := gorm.Open(dm.Open(dsn), &gorm.Config{
  49. Logger: newLogger,
  50. NamingStrategy: schema.NamingStrategy{
  51. SingularTable: true, // 表示使用单数表名,启用该选项后,GORM 将不会对表名进行复数化处理
  52. },
  53. })
  54. if err != nil {
  55. panic(fmt.Errorf("mysql 启动异常,数据库:%s;Err:%s", aliasName, err))
  56. }
  57. sqlDB, err := db.DB()
  58. if err != nil {
  59. panic(fmt.Errorf("mysql 创建连接池失败,数据库:%s;Err:%s", aliasName, err))
  60. }
  61. dmSqlMap[aliasName] = db
  62. if isDefault {
  63. global.DEFAULT_MYSQL = db
  64. }
  65. sqlDB.SetMaxIdleConns(50)
  66. sqlDB.SetMaxOpenConns(100)
  67. sqlDB.SetConnMaxLifetime(10 * time.Minute)
  68. }
  69. func (t *LocalTime) MarshalJSON() ([]byte, error) {
  70. tTime := time.Time(*t)
  71. if tTime.IsZero() {
  72. return []byte("\"\""), nil
  73. }
  74. return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02 15:04:05"))), nil
  75. }
  76. func (t LocalTime) Value() (driver.Value, error) {
  77. var zeroTime time.Time
  78. tlt := time.Time(t)
  79. if tlt.UnixNano() == zeroTime.UnixNano() {
  80. return nil, nil
  81. }
  82. return tlt, nil
  83. }
  84. func (t *LocalTime) Scan(v interface{}) error {
  85. if value, ok := v.(time.Time); ok {
  86. *t = LocalTime(value)
  87. return nil
  88. }
  89. return fmt.Errorf("can not convert %v to timestamp", v)
  90. }