From d2f52eba64a3b2f89334e228905139fdd785e94a Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 9 May 2021 15:09:59 +0800 Subject: [PATCH] Byte strings in postgres aren't 0x... (#1906) Byte strings in postgres are actually E'\x...' not 0x... This is part of the follow-up to #1872 Signed-off-by: Andrew Thornton Reviewed-on: https://gitea.com/xorm/xorm/pulls/1906 Reviewed-by: Lunny Xiao Co-authored-by: Andrew Thornton Co-committed-by: Andrew Thornton --- dialects/postgres.go | 5 +++++ engine.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dialects/postgres.go b/dialects/postgres.go index 2b234c66..e76e5b7e 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -824,6 +824,11 @@ func (db *postgres) SetQuotePolicy(quotePolicy QuotePolicy) { } } +// FormatBytes formats bytes +func (db *postgres) FormatBytes(bs []byte) string { + return fmt.Sprintf("E'\\x%x'", bs) +} + func (db *postgres) SQLType(c *schemas.Column) string { var res string switch t := c.SQLType.Name; t { diff --git a/engine.go b/engine.go index 22ba6163..d49eea9a 100644 --- a/engine.go +++ b/engine.go @@ -460,7 +460,7 @@ func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas. if col.SQLType.IsText() { var v string switch reflect.TypeOf(d).Kind() { - case reflect.Struct, reflect.Array, reflect.Slice: + case reflect.Struct, reflect.Array, reflect.Slice, reflect.Map: bytes, err := json.DefaultJSONHandler.Marshal(d) if err != nil { v = fmt.Sprintf("%s", d)