Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customize the time param format in SQL log #6508

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions logger/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"gorm.io/gorm/utils"
)

// TimeParamFormat defines the format of the time parameter in ExplainSQL.
var TimeParamFormat = "2006-01-02 15:04:05.999"

const (
tmFmtWithMS = "2006-01-02 15:04:05.999"
tmFmtZero = "0000-00-00 00:00:00"
nullStr = "NULL"
tmFmtZero = "0000-00-00 00:00:00"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using the same tmFmtZero and TimeParamFormat format? IMO This would make confusion later if the TimeParamFormat is not used for zero values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using the same tmFmtZero and TimeParamFormat format? IMO This would make confusion later if the TimeParamFormat is not used for zero values.

@saeidee
Thank you for your comment. I have considered your suggestion, but using the same TimeParamFormat for zero time values would lead to a breaking change, so I have maintained the current behavior for now.

before: 0000-00-00 00:00:00
after:  0001-01-01 00:00:00

nullStr = "NULL"
)

func isPrintable(s string) bool {
Expand Down Expand Up @@ -49,14 +51,14 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
if v.IsZero() {
vars[idx] = escaper + tmFmtZero + escaper
} else {
vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper
vars[idx] = escaper + v.Format(TimeParamFormat) + escaper
}
case *time.Time:
if v != nil {
if v.IsZero() {
vars[idx] = escaper + tmFmtZero + escaper
} else {
vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper
vars[idx] = escaper + v.Format(TimeParamFormat) + escaper
}
} else {
vars[idx] = nullStr
Expand Down
19 changes: 19 additions & 0 deletions logger/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"testing"
"time"

"github.com/jinzhu/now"
"gorm.io/gorm/logger"
Expand Down Expand Up @@ -114,4 +115,22 @@ func TestExplainSQL(t *testing.T) {
t.Errorf("Explain SQL #%v expects %v, but got %v", idx, r.Result, result)
}
}

t.Run("customize time format", func(t *testing.T) {
orignalFormat := logger.TimeParamFormat
t.Cleanup(func() { logger.TimeParamFormat = orignalFormat })

logger.TimeParamFormat = time.RFC3339Nano

tnano := now.MustParse("2020-02-23T11:10:10.123456789+08:00")
var zt time.Time

sql := "create table users (name, create_at, update_at, delete_at, init_at) values (?, ?, ?, ?, ?)"
vars := []interface{}{"jinzhu", tnano, &tnano, zt, &zt}
expected := `create table users (name, create_at, update_at, delete_at, init_at) values ("jinzhu", "2020-02-23T11:10:10.123456789+08:00", "2020-02-23T11:10:10.123456789+08:00", "0000-00-00 00:00:00", "0000-00-00 00:00:00")`

if result := logger.ExplainSQL(sql, nil, `"`, vars...); result != expected {
t.Errorf("Explain SQL expects %v, but got %v", expected, result)
}
})
}
Loading