Skip to content

Commit

Permalink
mysql: Fix decoding of TIME '00:00:00.000000'
Browse files Browse the repository at this point in the history
Fixes #418
  • Loading branch information
blackwolf12333 authored and mehcode committed Jun 29, 2020
1 parent e4005bb commit eda0b7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sqlx-core/src/mysql/types/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ impl<'r> Decode<'r, MySql> for NaiveTime {
// data length, expecting 8 or 12 (fractional seconds)
let len = buf.get_u8();

// MySQL specifies that if all of hours, minutes, seconds, microseconds
// are 0 then the length is 0 and no further data is send
// https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
if len == 0 {
return Ok(NaiveTime::from_hms_micro(0, 0, 0, 0));
}

// is negative : int<1>
let is_negative = buf.get_u8();
debug_assert_eq!(is_negative, 0, "Negative dates/times are not supported");
Expand Down
7 changes: 7 additions & 0 deletions sqlx-core/src/mysql/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl<'r> Decode<'r, MySql> for Time {
// data length, expecting 8 or 12 (fractional seconds)
let len = buf.get_u8();

// MySQL specifies that if all of hours, minutes, seconds, microseconds
// are 0 then the length is 0 and no further data is send
// https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
if len == 0 {
return Ok(Time::try_from_hms_micro(0, 0, 0, 0).unwrap());
}

// is negative : int<1>
let is_negative = buf.get_u8();
assert_eq!(is_negative, 0, "Negative dates/times are not supported");
Expand Down
10 changes: 10 additions & 0 deletions tests/mysql/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ mod chrono {
"DATE '2050-11-23'" == NaiveDate::from_ymd(2050, 11, 23)
));

test_type!(chrono_time_zero<NaiveTime>(
MySql,
"TIME '00:00:00.000000'" == NaiveTime::from_hms_micro(0, 0, 0, 0)
));

test_type!(chrono_time<NaiveTime>(
MySql,
"TIME '05:10:20.115100'" == NaiveTime::from_hms_micro(5, 10, 20, 115100)
Expand Down Expand Up @@ -81,6 +86,11 @@ mod time_tests {
"DATE '2050-11-23'" == date!(2050 - 11 - 23)
));

test_type!(time_time_zero<Time>(
MySql,
"TIME '00:00:00.000000'" == time!(00:00:00.000000)
));

test_type!(time_time<Time>(
MySql,
"TIME '05:10:20.115100'" == time!(5:10:20.115100)
Expand Down

0 comments on commit eda0b7d

Please sign in to comment.