fmt.Sprint() to strconv.Format (#3354)

This commit is contained in:
aimuz 2020-09-01 21:03:37 +08:00 committed by GitHub
parent d1e17d549f
commit 9a101c8a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package logger
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"gorm.io/gorm/utils"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -24,13 +25,12 @@ var convertableTypes = []reflect.Type{reflect.TypeOf(time.Time{}), reflect.TypeO
func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, avars ...interface{}) string { func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, avars ...interface{}) string {
var convertParams func(interface{}, int) var convertParams func(interface{}, int)
var vars = make([]interface{}, len(avars)) var vars = make([]string, len(avars))
copy(vars, avars)
convertParams = func(v interface{}, idx int) { convertParams = func(v interface{}, idx int) {
switch v := v.(type) { switch v := v.(type) {
case bool: case bool:
vars[idx] = fmt.Sprint(v) vars[idx] = strconv.FormatBool(v)
case time.Time: case time.Time:
if v.IsZero() { if v.IsZero() {
vars[idx] = escaper + "0000-00-00 00:00:00" + escaper vars[idx] = escaper + "0000-00-00 00:00:00" + escaper
@ -44,7 +44,7 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
vars[idx] = escaper + "<binary>" + escaper vars[idx] = escaper + "<binary>" + escaper
} }
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
vars[idx] = fmt.Sprintf("%d", v) vars[idx] = utils.ToString(v)
case float64, float32: case float64, float32:
vars[idx] = fmt.Sprintf("%.6f", v) vars[idx] = fmt.Sprintf("%.6f", v)
case string: case string:
@ -70,18 +70,18 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
} }
} }
for idx, v := range vars { for idx, v := range avars {
convertParams(v, idx) convertParams(v, idx)
} }
if numericPlaceholder == nil { if numericPlaceholder == nil {
for _, v := range vars { for _, v := range vars {
sql = strings.Replace(sql, "?", v.(string), 1) sql = strings.Replace(sql, "?", v, 1)
} }
} else { } else {
sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$") sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$")
for idx, v := range vars { for idx, v := range vars {
sql = strings.Replace(sql, "$"+strconv.Itoa(idx+1)+"$", v.(string), 1) sql = strings.Replace(sql, "$"+strconv.Itoa(idx+1)+"$", v, 1)
} }
} }

View File

@ -671,7 +671,7 @@ func (field *Field) setupValuerAndSetter() {
case []byte: case []byte:
field.ReflectValueOf(value).SetString(string(data)) field.ReflectValueOf(value).SetString(string(data))
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
field.ReflectValueOf(value).SetString(fmt.Sprint(data)) field.ReflectValueOf(value).SetString(utils.ToString(data))
case float64, float32: case float64, float32:
field.ReflectValueOf(value).SetString(fmt.Sprintf("%."+strconv.Itoa(field.Precision)+"f", data)) field.ReflectValueOf(value).SetString(fmt.Sprintf("%."+strconv.Itoa(field.Precision)+"f", data))
default: default:

View File

@ -83,3 +83,31 @@ func AssertEqual(src, dst interface{}) bool {
} }
return true return true
} }
func ToString(value interface{}) string {
switch v := value.(type) {
case string:
return v
case int:
return strconv.FormatInt(int64(v), 10)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
}
return ""
}