Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions go/sql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
16 changes: 16 additions & 0 deletions go/sql/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}