コード例 #1
0
ファイル: util_test.go プロジェクト: pingcap/mysqlrelay
func (s *testUtilSuite) TestDumpBinaryTime(c *C) {
	t, err := mysql.ParseTimestamp("0000-00-00 00:00:00.0000000")
	c.Assert(err, IsNil)
	d := dumpBinaryDateTime(t, nil)
	c.Assert(d, DeepEquals, []byte{11, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0})
	t, err = mysql.ParseDatetime("0000-00-00 00:00:00.0000000")
	c.Assert(err, IsNil)
	d = dumpBinaryDateTime(t, nil)
	c.Assert(d, DeepEquals, []byte{11, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0})

	t, err = mysql.ParseDate("0000-00-00")
	c.Assert(err, IsNil)
	d = dumpBinaryDateTime(t, nil)
	c.Assert(d, DeepEquals, []byte{4, 1, 0, 1, 1})

	myDuration, err := mysql.ParseDuration("0000-00-00 00:00:00.0000000", 6)
	c.Assert(err, IsNil)
	d = dumpBinaryTime(myDuration.Duration)
	c.Assert(d, DeepEquals, []byte{0})
}
コード例 #2
0
ファイル: convert_test.go プロジェクト: fuxiaohei/tidb
func (s *testTypeConvertSuite) TestConvertType(c *check.C) {
	ft := NewFieldType(mysql.TypeBlob)
	ft.Flen = 4
	ft.Charset = "utf8"
	v, err := Convert("123456", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, "1234")
	ft = NewFieldType(mysql.TypeString)
	ft.Flen = 4
	ft.Charset = charset.CharsetBin
	v, err = Convert("12345", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, []byte("1234"))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(111.114, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(111.11))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(999.999, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(999.99))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(-999.999, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(-999.99))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(1111.11, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(999.99))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(999.916, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(999.92))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(999.915, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(999.91))

	ft = NewFieldType(mysql.TypeFloat)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(999.9155, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float32(999.92))

	// For TypeBlob
	ft = NewFieldType(mysql.TypeBlob)
	v, err = Convert(&invalidMockType{}, ft)
	c.Assert(err, check.NotNil)

	// Nil
	ft = NewFieldType(mysql.TypeBlob)
	v, err = Convert(nil, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.IsNil)

	// TypeDouble
	ft = NewFieldType(mysql.TypeDouble)
	ft.Flen = 5
	ft.Decimal = 2
	v, err = Convert(999.9155, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, float64(999.92))

	// For TypeString
	ft = NewFieldType(mysql.TypeString)
	ft.Flen = 3
	v, err = Convert("12345", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, "123")
	ft = NewFieldType(mysql.TypeString)
	ft.Flen = 3
	ft.Charset = charset.CharsetBin
	v, err = Convert("12345", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, []byte("123"))

	// For TypeDuration
	ft = NewFieldType(mysql.TypeDuration)
	ft.Decimal = 3
	v, err = Convert("10:11:12.123456", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v.(mysql.Duration).String(), check.Equals, "10:11:12.123")
	ft.Decimal = 1
	vv, err := Convert(v, ft)
	c.Assert(err, check.IsNil)
	c.Assert(vv.(mysql.Duration).String(), check.Equals, "10:11:12.1")

	vt, err := mysql.ParseTime("2010-10-10 10:11:11.12345", mysql.TypeTimestamp, 2)
	c.Assert(vt.String(), check.Equals, "2010-10-10 10:11:11.12")
	c.Assert(err, check.IsNil)
	v, err = Convert(vt, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v.(mysql.Duration).String(), check.Equals, "10:11:11.1")

	// For mysql.TypeTimestamp, mysql.TypeDatetime, mysql.TypeDate
	ft = NewFieldType(mysql.TypeTimestamp)
	ft.Decimal = 3
	v, err = Convert("2010-10-10 10:11:11.12345", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v.(mysql.Time).String(), check.Equals, "2010-10-10 10:11:11.123")
	ft.Decimal = 1
	vv, err = Convert(v, ft)
	c.Assert(err, check.IsNil)
	c.Assert(vv.(mysql.Time).String(), check.Equals, "2010-10-10 10:11:11.1")

	// For TypeLonglong
	ft = NewFieldType(mysql.TypeLonglong)
	v, err = Convert("100", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, int64(100))
	ft = NewFieldType(mysql.TypeLonglong)
	ft.Flag |= mysql.UnsignedFlag
	v, err = Convert("100", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, uint64(100))

	// For TypeBit
	ft = NewFieldType(mysql.TypeBit)
	ft.Flen = 8
	v, err = Convert("100", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, mysql.Bit{Value: 100, Width: 8})

	v, err = Convert(mysql.Hex{Value: 100}, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, mysql.Bit{Value: 100, Width: 8})

	v, err = Convert(mysql.Bit{Value: 100, Width: 8}, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, mysql.Bit{Value: 100, Width: 8})

	ft.Flen = 1
	v, err = Convert(1, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, mysql.Bit{Value: 1, Width: 1})

	_, err = Convert(2, ft)
	c.Assert(err, check.NotNil)

	ft.Flen = 0
	_, err = Convert(2, ft)
	c.Assert(err, check.NotNil)

	// For TypeNewDecimal
	ft = NewFieldType(mysql.TypeNewDecimal)
	ft.Decimal = 5
	v, err = Convert(3.1415926, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v.(mysql.Decimal).String(), check.Equals, "3.14159")

	// For TypeYear
	ft = NewFieldType(mysql.TypeYear)
	v, err = Convert("2015-11-11", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, int64(2015))
	v, err = Convert(2015, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.Equals, int64(2015))
	v, err = Convert(1800, ft)
	c.Assert(err, check.NotNil)
	dt, err := mysql.ParseDate("2015-11-11")
	c.Assert(err, check.IsNil)
	v, err = Convert(dt, ft)
	c.Assert(v, check.Equals, int64(2015))
	v, err = Convert(mysql.ZeroDuration, ft)
	c.Assert(v, check.Equals, int64(time.Now().Year()))

	// For enum
	ft = NewFieldType(mysql.TypeEnum)
	ft.Elems = []string{"a", "b", "c"}
	v, err = Convert("a", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, mysql.Enum{Name: "a", Value: 1})
	v, err = Convert(2, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, mysql.Enum{Name: "b", Value: 2})
	_, err = Convert("d", ft)
	c.Assert(err, check.NotNil)
	_, err = Convert(4, ft)
	c.Assert(err, check.NotNil)

	ft = NewFieldType(mysql.TypeSet)
	ft.Elems = []string{"a", "b", "c"}
	v, err = Convert("a", ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, mysql.Set{Name: "a", Value: 1})
	v, err = Convert(2, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, mysql.Set{Name: "b", Value: 2})
	v, err = Convert(3, ft)
	c.Assert(err, check.IsNil)
	c.Assert(v, check.DeepEquals, mysql.Set{Name: "a,b", Value: 3})
	_, err = Convert("d", ft)
	c.Assert(err, check.NotNil)
	_, err = Convert(9, ft)
	c.Assert(err, check.NotNil)
}