*: Fix hibernate bug about col.Flen/Decimal

This commit is contained in:
shenli
2015-10-13 18:11:11 +08:00
parent e35d415d4a
commit d50b6c5fcc
10 changed files with 58 additions and 29 deletions

View File

@ -24,7 +24,6 @@ import (
"github.com/juju/errors"
"github.com/pingcap/tidb/column"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/util/types"
)
const (
@ -89,14 +88,6 @@ func ColToResultField(col *column.Col, tableName string) *ResultField {
TableName: tableName,
OrgTableName: tableName,
}
if rf.Col.Flen == types.UnspecifiedLength {
rf.Col.Flen = 0
}
if rf.Col.Decimal == types.UnspecifiedLength {
rf.Col.Decimal = 0
}
// Keep things compatible for old clients.
// Refer to mysql-server/sql/protocol.cc send_result_set_metadata()
if rf.Tp == mysql.TypeVarchar {

View File

@ -17,6 +17,7 @@ package mysqldef
// Call this when no Flen assigned in ddl.
// or column value is calculated from an expression.
// For example: "select count(*) from t;", the column type is int64 and Flen in ResultField will be 21.
// See: https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
func GetDefaultFieldLength(tp byte) int {
switch tp {
case TypeTiny:
@ -29,8 +30,25 @@ func GetDefaultFieldLength(tp byte) int {
return 11
case TypeLonglong:
return 21
case TypeDecimal:
// See: https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
return 10
case TypeBit, TypeBlob:
return -1
default:
//TODO: add more types
return 0
return -1
}
}
// GetDefaultDecimal returns the default decimal length for column.
func GetDefaultDecimal(tp byte) int {
switch tp {
case TypeDecimal:
// See: https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
return 0
default:
//TODO: add more types
return -1
}
}

View File

@ -25,7 +25,9 @@ func TestGetFieldLength(t *testing.T) {
{TypeInt24, 9},
{TypeLong, 11},
{TypeLonglong, 21},
{TypeNull, 0},
{TypeBit, -1},
{TypeBlob, -1},
{TypeNull, -1},
}
for _, test := range tbl {

View File

@ -181,9 +181,12 @@ func ColumnDefToCol(offset int, colDef *ColumnDef) (*column.Col, []*TableConstra
}
// If flen is not assigned, assigned it by type.
if col.Flen == 0 {
if col.Flen == types.UnspecifiedLength {
col.Flen = mysql.GetDefaultFieldLength(col.Tp)
}
if col.Decimal == types.UnspecifiedLength {
col.Decimal = mysql.GetDefaultDecimal(col.Tp)
}
setOnUpdateNow := false
hasDefaultValue := false

View File

@ -361,8 +361,14 @@ func (isp *InfoSchemaPlan) fetchColumns(schemas []*model.DBInfo) {
if decimal == types.UnspecifiedLength {
decimal = 0
}
dataType := types.TypeToStr(col.Tp, col.Charset == charset.CharsetBin)
columnType := fmt.Sprintf("%s(%d)", dataType, colLen)
columnType := types.TypeToStr(col.Tp, col.Charset == charset.CharsetBin)
if col.Decimal == types.UnspecifiedLength {
if colLen != types.UnspecifiedLength {
columnType = fmt.Sprintf("%s(%d)", columnType, colLen)
}
} else {
columnType = fmt.Sprintf("%s(%d, %d)", columnType, colLen, col.Decimal)
}
columnDesc := column.NewColDesc(&column.Col{ColumnInfo: *col})
var columnDefault interface{}
if columnDesc.DefaultValue != nil {

View File

@ -433,6 +433,7 @@ func (cc *clientConn) writeResultset(rs ResultSet, binary bool) error {
var rowData []byte
rowData, err = dumpRowValuesBinary(cc.alloc, columns, row)
if err != nil {
fmt.Println("[conn]", err)
return errors.Trace(err)
}
data = append(data, rowData...)

View File

@ -272,8 +272,16 @@ func convertColumnInfo(fld *field.ResultField) (ci *ColumnInfo) {
ci.Schema = fld.DBName
ci.Flag = uint16(fld.Flag)
ci.Charset = uint16(mysql.CharsetIDs[fld.Charset])
ci.ColumnLength = uint32(fld.Flen)
ci.Decimal = uint8(fld.Decimal)
if fld.Flen == -1 {
ci.ColumnLength = 0
} else {
ci.ColumnLength = uint32(fld.Flen)
}
if fld.Decimal == -1 {
ci.Decimal = 0
} else {
ci.Decimal = uint8(fld.Decimal)
}
ci.Type = uint8(fld.Tp)
return
}

View File

@ -888,7 +888,7 @@ func (s *testSessionSuite) TestShow(c *C) {
rows, err := r.Rows(-1, 0)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 1)
match(c, rows[0], "c", "int", "YES", "", nil, "")
match(c, rows[0], "c", "int(11)", "YES", "", nil, "")
r = mustExecSQL(c, se, "show collation where Charset = 'utf8' and Collation = 'utf8_bin'")
row, err = r.FirstRow()

View File

@ -89,22 +89,22 @@ func TypeStr(tp byte) (r string) {
func TypeToStr(tp byte, binary bool) string {
switch tp {
case mysql.TypeBlob:
if binary {
if !binary {
return "text"
}
return "blob"
case mysql.TypeLongBlob:
if binary {
if !binary {
return "longtext"
}
return "longblob"
case mysql.TypeTinyBlob:
if binary {
if !binary {
return "tinytext"
}
return "tinyblob"
case mysql.TypeMediumBlob:
if binary {
if !binary {
return "mediumtext"
}
return "mediumblob"

View File

@ -68,17 +68,17 @@ func (s *testTypeEtcSuite) TestTypeToStr(c *C) {
testTypeStr(c, mysql.TypeYear, "year")
testTypeStr(c, 0xdd, "")
testTypeToStr(c, mysql.TypeBlob, true, "text")
testTypeToStr(c, mysql.TypeLongBlob, true, "longtext")
testTypeToStr(c, mysql.TypeTinyBlob, true, "tinytext")
testTypeToStr(c, mysql.TypeMediumBlob, true, "mediumtext")
testTypeToStr(c, mysql.TypeBlob, false, "text")
testTypeToStr(c, mysql.TypeLongBlob, false, "longtext")
testTypeToStr(c, mysql.TypeTinyBlob, false, "tinytext")
testTypeToStr(c, mysql.TypeMediumBlob, false, "mediumtext")
testTypeToStr(c, mysql.TypeVarchar, true, "varbinary")
testTypeToStr(c, mysql.TypeString, true, "binary")
testTypeToStr(c, mysql.TypeTiny, true, "tinyint")
testTypeToStr(c, mysql.TypeBlob, false, "blob")
testTypeToStr(c, mysql.TypeLongBlob, false, "longblob")
testTypeToStr(c, mysql.TypeTinyBlob, false, "tinyblob")
testTypeToStr(c, mysql.TypeMediumBlob, false, "mediumblob")
testTypeToStr(c, mysql.TypeBlob, true, "blob")
testTypeToStr(c, mysql.TypeLongBlob, true, "longblob")
testTypeToStr(c, mysql.TypeTinyBlob, true, "tinyblob")
testTypeToStr(c, mysql.TypeMediumBlob, true, "mediumblob")
testTypeToStr(c, mysql.TypeVarchar, false, "varchar")
testTypeToStr(c, mysql.TypeString, false, "char")
testTypeToStr(c, mysql.TypeShort, true, "smallint")