Skip to content

Commit

Permalink
Merge branch 'master' into disksing/kv-refine-interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Dec 24, 2015
2 parents 15a5e4f + 7212e11 commit d8fe27d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions util/types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ func DefaultTypeForValue(value interface{}) *FieldType {
tp := NewFieldType(mysql.TypeBlob)
tp.Charset = charset.CharsetBin
tp.Collate = charset.CharsetBin
return tp
case mysql.Bit:
return NewFieldType(mysql.TypeBit)
case mysql.Hex:
tp := NewFieldType(mysql.TypeVarchar)
tp.Charset = charset.CharsetBin
tp.Collate = charset.CharsetBin
return tp
case mysql.Time:
return NewFieldType(x.Type)
case mysql.Duration:
Expand Down
28 changes: 28 additions & 0 deletions util/types/field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,31 @@ func (s *testFieldTypeSuite) TestDataItem(c *check.C) {
c.Assert(RawData(d), check.Equals, "string")
c.Assert(IsNil(d), check.IsFalse)
}

func (s *testFieldTypeSuite) TestDefaultTypeForValue(c *check.C) {
nullType := DefaultTypeForValue(nil)
di := &DataItem{Type: nullType}
cases := []struct {
value interface{}
tp byte
}{
{nil, mysql.TypeNull},
{1, mysql.TypeLonglong},
{uint64(1), mysql.TypeLonglong},
{"abc", mysql.TypeVarchar},
{1.1, mysql.TypeNewDecimal},
{[]byte("abc"), mysql.TypeBlob},
{mysql.Bit{}, mysql.TypeBit},
{mysql.Hex{}, mysql.TypeVarchar},
{mysql.Time{Type: mysql.TypeDatetime}, mysql.TypeDatetime},
{mysql.Duration{}, mysql.TypeDuration},
{mysql.Decimal{}, mysql.TypeNewDecimal},
{mysql.Enum{}, mysql.TypeEnum},
{mysql.Set{}, mysql.TypeSet},
{di, mysql.TypeNull},
}
for _, ca := range cases {
ft := DefaultTypeForValue(ca.value)
c.Assert(ft.Tp, check.Equals, ca.tp, check.Commentf("%v %v", ft, ca))
}
}

0 comments on commit d8fe27d

Please sign in to comment.