Skip to content

Commit

Permalink
customize time format in sql log
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyuehong committed Aug 5, 2023
1 parent f473761 commit ba01300
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 9 additions & 5 deletions logger/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
"gorm.io/gorm/utils"
)

var (

Check failure on line 16 in logger/sql.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[gofumpt] reported by reviewdog 🐶 Raw Output: logger/sql.go:16:-var ( logger/sql.go:17:- // TimeParamFormat defines the format of the time parameter in ExpalianSQL. logger/sql.go:18:- TimeParamFormat = "2006-01-02 15:04:05.999" logger/sql.go:19:-) logger/sql.go:16:+// TimeParamFormat defines the format of the time parameter in ExpalianSQL. logger/sql.go:17:+var TimeParamFormat = "2006-01-02 15:04:05.999"
// TimeParamFormat defines the format of the time parameter in ExpalianSQL.
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"
nullStr = "NULL"
)

func isPrintable(s string) bool {
Expand Down Expand Up @@ -47,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
18 changes: 17 additions & 1 deletion 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 @@ -101,12 +102,27 @@ func TestExplainSQL(t *testing.T) {
Vars: []interface{}{"jinzhu", 1, 0.1753607109, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es},
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 0.1753607109, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)),
},

}

for idx, r := range results {
if result := logger.ExplainSQL(r.SQL, r.NumericRegexp, `"`, r.Vars...); result != r.Result {
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)
}
})
}

0 comments on commit ba01300

Please sign in to comment.