Skip to content

Commit

Permalink
opt time value display the debug sql (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: rain <lvxiaorun@gmail.com>
  • Loading branch information
lvxiaorun and rain committed Oct 17, 2022
1 parent ecb6c6a commit bf5c03d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mysql.go
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/go-sql-driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
"gorm.io/gorm/clause"
Expand All @@ -22,6 +22,7 @@ type Config struct {
DriverName string
ServerVersion string
DSN string
DSNConfig *mysql.Config
Conn gorm.ConnPool
SkipInitializeWithVersion bool
DefaultStringSize uint
Expand Down Expand Up @@ -52,7 +53,8 @@ var (
)

func Open(dsn string) gorm.Dialector {
return &Dialector{Config: &Config{DSN: dsn}}
dsnConf, _ := mysql.ParseDSN(dsn)
return &Dialector{Config: &Config{DSN: dsn, DSNConfig: dsnConf}}
}

func New(config Config) gorm.Dialector {
Expand Down Expand Up @@ -311,6 +313,19 @@ func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
}

func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
if dialector.DSNConfig.Loc == time.Local {
for i, v := range vars {
switch v.(type) {
case time.Time:
vars[i] = v.(time.Time).In(time.Local)
case *time.Time:
if v.(*time.Time) != nil {
newValue := v.(*time.Time).In(time.Local)
vars[i] = &newValue
}
}
}
}
return logger.ExplainSQL(sql, nil, `'`, vars...)
}

Expand Down

0 comments on commit bf5c03d

Please sign in to comment.