Fix another bug with #1872 (#1905)

Ensure that structs, arrays and slices are properly converted to strings.

Signed-off-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1905
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-committed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2021-05-08 21:39:03 +08:00 committed by Lunny Xiao
parent 210c30a7dd
commit dab09c73ab
1 changed files with 14 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import (
"xorm.io/xorm/contexts"
"xorm.io/xorm/core"
"xorm.io/xorm/dialects"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/log"
"xorm.io/xorm/names"
@ -457,7 +458,19 @@ func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas.
}
if col.SQLType.IsText() {
var v = fmt.Sprintf("%s", d)
var v string
switch reflect.TypeOf(d).Kind() {
case reflect.Struct, reflect.Array, reflect.Slice:
bytes, err := json.DefaultJSONHandler.Marshal(d)
if err != nil {
v = fmt.Sprintf("%s", d)
} else {
v = string(bytes)
}
default:
v = fmt.Sprintf("%s", d)
}
return "'" + strings.Replace(v, "'", "''", -1) + "'"
} else if col.SQLType.IsTime() {
if dstDialect.URI().DBType == schemas.MSSQL && col.SQLType.Name == schemas.DateTime {