diff --git a/go/sql/types.go b/go/sql/types.go index 6ecf75881..aac52bc32 100644 --- a/go/sql/types.go +++ b/go/sql/types.go @@ -53,14 +53,22 @@ type Column struct { } func (this *Column) convertArg(arg interface{}, isUniqueKeyColumn bool) interface{} { + var arg2Bytes []byte if s, ok := arg.(string); ok { - arg2Bytes := []byte(s) - // convert to bytes if character string without charsetConversion. + arg2Bytes = []byte(s) + } else if b, ok := arg.([]uint8); ok { + arg2Bytes = b + } else { + arg2Bytes = nil + } + + if arg2Bytes != nil { if this.Charset != "" && this.charsetConversion == nil { arg = arg2Bytes } else { if encoding, ok := charsetEncodingMap[this.Charset]; ok { - arg, _ = encoding.NewDecoder().String(s) + decodedBytes, _ := encoding.NewDecoder().Bytes(arg2Bytes) + arg = string(decodedBytes) } } diff --git a/go/sql/types_test.go b/go/sql/types_test.go index 5d71070c2..7b808e64f 100644 --- a/go/sql/types_test.go +++ b/go/sql/types_test.go @@ -49,3 +49,19 @@ func TestBinaryToString(t *testing.T) { require.Equal(t, "1b99", cv.StringColumn(0)) } + +func TestConvertArgCharsetDecoding(t *testing.T) { + latin1Bytes := []uint8{0x47, 0x61, 0x72, 0xe7, 0x6f, 0x6e, 0x20, 0x21} + + col := Column{ + Charset: "latin1", + charsetConversion: &CharacterSetConversion{ + FromCharset: "latin1", + ToCharset: "utf8mb4", + }, + } + + // Should decode []uint8 + str := col.convertArg(latin1Bytes, false) + require.Equal(t, "Garçon !", str) +}