123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package init_serve
- import (
- "database/sql/driver"
- _ "dm"
- dm "dmgorm2"
- "eta_gn/eta_bridge/global"
- "fmt"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- "gorm.io/gorm/schema"
- "io"
- "log"
- "os"
- "time"
- _ "github.com/go-sql-driver/mysql"
- )
- type LocalTime time.Time
- // InitDm
- // @Description: 达梦数据库初始化
- func InitDm() {
- dmSqlMap := make(map[string]*gorm.DB)
- //开启日志
- logWriter := io.MultiWriter(global.MYSQL_LOG) //binlog日志,记录到文件中去
- if global.CONFIG.Mysql.Stdout {
- logWriter = io.MultiWriter(global.MYSQL_LOG, os.Stdout)
- }
- newLogger := logger.New(log.New(logWriter, "\r\n", log.LstdFlags), logger.Config{
- SlowThreshold: 200 * time.Millisecond, //慢sql :200ms
- LogLevel: logger.Info, //记录的日志类型,info代表所有信息都记录
- IgnoreRecordNotFoundError: true, //是否忽略找不到数据错误信息(只是日志记录记录成err还是普通的输出的区别,并不影响业务代码中的:找不到数据行error)
- Colorful: false, //是否颜色输出
- })
- mysqlConf := global.CONFIG.Mysql
- if len(mysqlConf.List) <= 0 {
- global.LOG.Error("mysql链接未配置")
- panic(fmt.Errorf("mysql链接未配置"))
- }
- for _, conf := range mysqlConf.List {
- isDefault := false
- //默认数据库连接
- if mysqlConf.DefaultDsnAliasName == conf.AliasName {
- isDefault = true
- }
- // 库连接
- connectDm(conf.Dsn, conf.AliasName, newLogger, dmSqlMap, isDefault)
- }
- //全局赋值数据库链接
- global.MYSQL = dmSqlMap
- }
- // connectDm
- // @Description: 达梦数据库连接
- // @param dsn
- // @param aliasName
- // @param newLogger
- // @param dmSqlMap
- // @param isDefault
- func connectDm(dsn, aliasName string, newLogger logger.Interface, dmSqlMap map[string]*gorm.DB, isDefault bool) {
- //fmt.Println("dsn:", dsn, " == ;aliasName:", aliasName)
- if dsn == `` {
- return
- }
- //dsn := "dm://SYSDBA:SYSDBA001@8.136.199.33:30236?schema=\"eta_master\""
- //dsn := "dm://ETA:Han6258199118%60@localhost:5236?schema=ECOLOGY_TARGET"
- db, err := gorm.Open(dm.Open(dsn), &gorm.Config{
- Logger: newLogger,
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true, // 表示使用单数表名,启用该选项后,GORM 将不会对表名进行复数化处理
- },
- })
- if err != nil {
- //global.LOG.Errorf("mysql 启动异常,数据库:default;Err:", err)
- panic(fmt.Errorf("mysql 启动异常,数据库:%s;Err:%s", aliasName, err))
- }
- //创建连接池
- sqlDB, err := db.DB()
- if err != nil {
- //global.LOG.Errorf("mysql 创建连接池失败,数据库:default;Err:", err)
- panic(fmt.Errorf("mysql 创建连接池失败,数据库:%s;Err:%s", aliasName, err))
- }
- dmSqlMap[aliasName] = db
- //默认数据库连接
- if isDefault {
- global.DEFAULT_MYSQL = db
- }
- // SetMaxIdleConns 设置空闲连接池中连接的最大数量
- sqlDB.SetMaxIdleConns(50)
- // SetMaxOpenConns 设置打开数据库连接的最大数量。
- sqlDB.SetMaxOpenConns(100)
- // SetConnMaxLifetime 设置了连接可复用的最大时间。
- sqlDB.SetConnMaxLifetime(10 * time.Minute)
- }
- func (t *LocalTime) MarshalJSON() ([]byte, error) {
- tTime := time.Time(*t)
- if tTime.IsZero() {
- return []byte("\"\""), nil
- }
- return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02 15:04:05"))), nil
- }
- func (t LocalTime) Value() (driver.Value, error) {
- var zeroTime time.Time
- tlt := time.Time(t)
- //判断给定时间是否和默认零时间的时间戳相同
- if tlt.UnixNano() == zeroTime.UnixNano() {
- return nil, nil
- }
- return tlt, nil
- }
- func (t *LocalTime) Scan(v interface{}) error {
- if value, ok := v.(time.Time); ok {
- *t = LocalTime(value)
- return nil
- }
- return fmt.Errorf("can not convert %v to timestamp", v)
- }
|