This commit is contained in:
thsun 2021-01-13 17:21:21 +08:00
parent 3665d666b9
commit d0f0b95cf8
3 changed files with 23 additions and 0 deletions

Binary file not shown.

View File

@ -553,6 +553,7 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
columnName := m.GetString("COLUMN_NAME")
columnType := m.GetString("COLUMN_TYPE")
columnOctetLength := m.GetUint("CHARACTER_OCTET_LENGTH")
for _, columnsList := range columnsLists {
column := columnsList.GetColumn(columnName)
if column == nil {
@ -580,6 +581,10 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
if strings.HasPrefix(columnType, "enum") {
column.Type = sql.EnumColumnType
}
if strings.HasPrefix(columnType, "binary") {
column.Type = sql.BinaryColumnType
column.BinaryOctetLength = columnOctetLength
}
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
column.Charset = charset
}

View File

@ -10,6 +10,7 @@ import (
"reflect"
"strconv"
"strings"
"bytes"
)
type ColumnType int
@ -22,6 +23,7 @@ const (
MediumIntColumnType
JSONColumnType
FloatColumnType
BinaryColumnType
)
const maxMediumintUnsigned int32 = 16777215
@ -35,6 +37,10 @@ type Column struct {
IsUnsigned bool
Charset string
Type ColumnType
// add Octet length for binary type, fix bytes with suffix "00" get clipped in mysql binlog.
// https://github.com/github/gh-ost/issues/909
BinaryOctetLength uint
timezoneConversion *TimezoneConversion
}
@ -44,6 +50,18 @@ func (this *Column) convertArg(arg interface{}) interface{} {
if encoding, ok := charsetEncodingMap[this.Charset]; ok {
arg, _ = encoding.NewDecoder().String(s)
}
if this.Type == BinaryColumnType {
size := len([]byte(arg.(string)))
if uint(size) < this.BinaryOctetLength {
buf := bytes.NewBuffer([]byte(arg.(string)))
for i := uint(0); i < (this.BinaryOctetLength - uint(size)); i++ {
buf.Write([]byte{0})
}
arg = buf.String()
}
}
return arg
}