Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/types: support convert year #10

Merged
merged 4 commits into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions util/types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { //
}
// TODO: check Flen
return x, nil
case mysql.TypeYear:
var (
intVal int64
err error
)
switch x := val.(type) {
case string:
intVal, err = StrToInt(x)
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql> insert into t select cast("2015-11-11 11:11:11" as datetime);
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

I think you should support time type separately. ToInt64 will not work for time type.

intVal, err = ToInt64(x)
}
if err != nil {
return InvConv(val, tp)
}
y, err := mysql.AdjustYear(int(intVal))
if err != nil {
return InvConv(val, tp)
}
return int16(y), nil
default:
panic("should never happen")
}
Expand Down
11 changes: 11 additions & 0 deletions util/types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
v, err = Convert(3.1415926, ft)
c.Assert(err, IsNil)
c.Assert(v.(mysql.Decimal).String(), Equals, "3.14159")

// For TypeYear
ft = NewFieldType(mysql.TypeYear)
v, err = Convert("2015-11-11", ft)
c.Assert(err, IsNil)
c.Assert(v, Equals, int16(2015))
v, err = Convert(2015, ft)
c.Assert(err, IsNil)
c.Assert(v, Equals, int16(2015))
v, err = Convert(1800, ft)
c.Assert(err, NotNil)
}

func testToInt64(c *C, val interface{}, expect int64) {
Expand Down