|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
"OpenIM/pkg/errs"
|
|
|
|
"OpenIM/pkg/errs"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
mysqlDriver "github.com/go-sql-driver/mysql"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
@ -65,9 +66,24 @@ func newMysqlGormDB() (*gorm.DB, error) {
|
|
|
|
// gorm mysql
|
|
|
|
// gorm mysql
|
|
|
|
func NewGormDB() (*gorm.DB, error) {
|
|
|
|
func NewGormDB() (*gorm.DB, error) {
|
|
|
|
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
|
|
|
|
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
|
|
|
|
|
|
|
|
specialerror.AddErrHandler(replaceDuplicateKey)
|
|
|
|
return newMysqlGormDB()
|
|
|
|
return newMysqlGormDB()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func replaceDuplicateKey(err error) errs.CodeError {
|
|
|
|
|
|
|
|
if IsMysqlDuplicateKey(err) {
|
|
|
|
|
|
|
|
return errs.ErrDuplicateKey
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func IsMysqlDuplicateKey(err error) bool {
|
|
|
|
|
|
|
|
if mysqlErr, ok := err.(*mysqlDriver.MySQLError); ok {
|
|
|
|
|
|
|
|
return mysqlErr.Number == 1062
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Writer struct{}
|
|
|
|
type Writer struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (w Writer) Printf(format string, args ...interface{}) {
|
|
|
|
func (w Writer) Printf(format string, args ...interface{}) {
|
|
|
|