From c70c097e88bd5372783da6af55c4742fa4fe83ab Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 8 Sep 2020 19:11:20 +0800 Subject: [PATCH] Refactor format SQL for driver.Valuer --- logger/sql.go | 20 ++++++++++++++++++++ tests/go.mod | 4 ---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/logger/sql.go b/logger/sql.go index 80645b0c..096b9407 100644 --- a/logger/sql.go +++ b/logger/sql.go @@ -38,6 +38,26 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a } else { vars[idx] = escaper + v.Format("2006-01-02 15:04:05.999") + escaper } + case *time.Time: + if v != nil { + if v.IsZero() { + vars[idx] = escaper + "0000-00-00 00:00:00" + escaper + } else { + vars[idx] = escaper + v.Format("2006-01-02 15:04:05.999") + escaper + } + } else { + vars[idx] = "NULL" + } + case fmt.Stringer: + vars[idx] = escaper + strings.Replace(fmt.Sprintf("%v", v), escaper, "\\"+escaper, -1) + escaper + case driver.Valuer: + reflectValue := reflect.ValueOf(v) + if v != nil && reflectValue.IsValid() && (reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) { + r, _ := v.Value() + vars[idx] = fmt.Sprintf("%v", r) + } else { + vars[idx] = "NULL" + } case []byte: if isPrintable(v) { vars[idx] = escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper diff --git a/tests/go.mod b/tests/go.mod index 4ddb0b69..76db6764 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -6,10 +6,6 @@ require ( github.com/google/uuid v1.1.1 github.com/jinzhu/now v1.1.1 github.com/lib/pq v1.6.0 - gorm.io/driver/mysql v1.0.1 - gorm.io/driver/postgres v1.0.0 - gorm.io/driver/sqlite v1.1.1 - gorm.io/driver/sqlserver v1.0.3 gorm.io/gorm v1.9.19 )