diff --git a/logger/sql.go b/logger/sql.go index 02d559c5..0efc0971 100644 --- a/logger/sql.go +++ b/logger/sql.go @@ -3,6 +3,7 @@ package logger import ( "database/sql/driver" "fmt" + "gorm.io/gorm/utils" "reflect" "regexp" "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 { var convertParams func(interface{}, int) - var vars = make([]interface{}, len(avars)) - copy(vars, avars) + var vars = make([]string, len(avars)) convertParams = func(v interface{}, idx int) { switch v := v.(type) { case bool: - vars[idx] = fmt.Sprint(v) + vars[idx] = strconv.FormatBool(v) case time.Time: if v.IsZero() { 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 + "" + escaper } 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: vars[idx] = fmt.Sprintf("%.6f", v) 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) } if numericPlaceholder == nil { for _, v := range vars { - sql = strings.Replace(sql, "?", v.(string), 1) + sql = strings.Replace(sql, "?", v, 1) } } else { sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$") 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) } } diff --git a/schema/field.go b/schema/field.go index 524d19fb..2e649d81 100644 --- a/schema/field.go +++ b/schema/field.go @@ -671,7 +671,7 @@ func (field *Field) setupValuerAndSetter() { case []byte: field.ReflectValueOf(value).SetString(string(data)) 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: field.ReflectValueOf(value).SetString(fmt.Sprintf("%."+strconv.Itoa(field.Precision)+"f", data)) default: diff --git a/utils/utils.go b/utils/utils.go index 71336f4b..905001a5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -83,3 +83,31 @@ func AssertEqual(src, dst interface{}) bool { } 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 "" +}