Refactor format SQL for driver.Valuer

This commit is contained in:
Jinzhu 2020-09-08 19:11:20 +08:00
parent c9d5c0b07a
commit c70c097e88
2 changed files with 20 additions and 4 deletions

View File

@ -38,6 +38,26 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
} else { } else {
vars[idx] = escaper + v.Format("2006-01-02 15:04:05.999") + escaper 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: case []byte:
if isPrintable(v) { if isPrintable(v) {
vars[idx] = escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper vars[idx] = escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper

View File

@ -6,10 +6,6 @@ require (
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/jinzhu/now v1.1.1 github.com/jinzhu/now v1.1.1
github.com/lib/pq v1.6.0 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 gorm.io/gorm v1.9.19
) )