@@ -2,7 +2,7 @@ use crate::myc::constants::{ColumnFlags, ColumnType};
22use crate :: myc:: io:: WriteMysqlExt ;
33use crate :: Column ;
44use byteorder:: { LittleEndian , WriteBytesExt } ;
5- use std:: io:: { self , Write } ;
5+ use std:: io:: { self , ErrorKind :: Other , Write } ;
66
77/// Implementors of this trait can be sent as a single resultset value to a MySQL/MariaDB client.
88pub trait ToMysqlValue {
@@ -392,10 +392,10 @@ impl ToMysqlValue for [u8] {
392392
393393impl ToMysqlValue for Vec < u8 > {
394394 fn to_mysql_text < W : Write > ( & self , w : & mut W ) -> io:: Result < ( ) > {
395- ( & self [ ..] ) . to_mysql_text ( w)
395+ ( self [ ..] ) . to_mysql_text ( w)
396396 }
397397 fn to_mysql_bin < W : Write > ( & self , w : & mut W , c : & Column ) -> io:: Result < ( ) > {
398- ( & self [ ..] ) . to_mysql_bin ( w, c)
398+ ( self [ ..] ) . to_mysql_bin ( w, c)
399399 }
400400}
401401
@@ -563,16 +563,22 @@ impl ToMysqlValue for myc::value::Value {
563563 myc:: value:: Value :: Float ( f) => f. to_mysql_text ( w) ,
564564 myc:: value:: Value :: Double ( f) => f. to_mysql_text ( w) ,
565565 myc:: value:: Value :: Date ( y, mo, d, h, mi, s, us) => {
566- NaiveDate :: from_ymd ( i32:: from ( y) , u32:: from ( mo) , u32:: from ( d) )
567- . and_hms_micro ( u32:: from ( h) , u32:: from ( mi) , u32:: from ( s) , us)
566+ NaiveDate :: from_ymd_opt ( i32:: from ( y) , u32:: from ( mo) , u32:: from ( d) )
567+ . ok_or_else ( || {
568+ io:: Error :: new ( Other , format ! ( "invalid date: y {} mo {} d {}" , y, mo, d) )
569+ } ) ?
570+ . and_hms_micro_opt ( u32:: from ( h) , u32:: from ( mi) , u32:: from ( s) , us)
571+ . ok_or_else ( || {
572+ io:: Error :: new (
573+ Other ,
574+ format ! ( "invalid date: h {} mi {} s {} us {}" , h, mi, s, us) ,
575+ )
576+ } ) ?
568577 . to_mysql_text ( w)
569578 }
570579 myc:: value:: Value :: Time ( neg, d, h, m, s, us) => {
571580 if neg {
572- return Err ( io:: Error :: new (
573- io:: ErrorKind :: Other ,
574- "negative times not yet supported" ,
575- ) ) ;
581+ return Err ( io:: Error :: new ( Other , "negative times not yet supported" ) ) ;
576582 }
577583 ( chrono:: Duration :: days ( i64:: from ( d) )
578584 + chrono:: Duration :: hours ( i64:: from ( h) )
@@ -628,16 +634,22 @@ impl ToMysqlValue for myc::value::Value {
628634 myc:: value:: Value :: Float ( f) => f. to_mysql_bin ( w, c) ,
629635 myc:: value:: Value :: Double ( f) => f. to_mysql_bin ( w, c) ,
630636 myc:: value:: Value :: Date ( y, mo, d, h, mi, s, us) => {
631- NaiveDate :: from_ymd ( i32:: from ( y) , u32:: from ( mo) , u32:: from ( d) )
632- . and_hms_micro ( u32:: from ( h) , u32:: from ( mi) , u32:: from ( s) , us)
637+ NaiveDate :: from_ymd_opt ( i32:: from ( y) , u32:: from ( mo) , u32:: from ( d) )
638+ . ok_or_else ( || {
639+ io:: Error :: new ( Other , format ! ( "invalid date: y {} mo {} d {}" , y, mo, d) )
640+ } ) ?
641+ . and_hms_micro_opt ( u32:: from ( h) , u32:: from ( mi) , u32:: from ( s) , us)
642+ . ok_or_else ( || {
643+ io:: Error :: new (
644+ Other ,
645+ format ! ( "invalid date: h {} mi {} s {} us {}" , h, mi, s, us) ,
646+ )
647+ } ) ?
633648 . to_mysql_bin ( w, c)
634649 }
635650 myc:: value:: Value :: Time ( neg, d, h, m, s, us) => {
636651 if neg {
637- return Err ( io:: Error :: new (
638- io:: ErrorKind :: Other ,
639- "negative times not yet supported" ,
640- ) ) ;
652+ return Err ( io:: Error :: new ( Other , "negative times not yet supported" ) ) ;
641653 }
642654 ( chrono:: Duration :: days ( i64:: from ( d) )
643655 + chrono:: Duration :: hours ( i64:: from ( h) )
@@ -719,15 +731,14 @@ mod tests {
719731 rt ! ( opt_none, Option <u8 >, None ) ;
720732 rt ! ( opt_some, Option <u8 >, Some ( 1 ) ) ;
721733
722- rt ! (
723- time,
724- chrono:: NaiveDate ,
725- chrono:: Local :: today( ) . naive_local( )
726- ) ;
734+ rt ! ( time, chrono:: NaiveDate , chrono:: Local :: now( ) . date_naive( ) ) ;
727735 rt ! (
728736 datetime,
729737 chrono:: NaiveDateTime ,
730- chrono:: Utc . ymd( 1989 , 12 , 7 ) . and_hms( 8 , 0 , 4 ) . naive_utc( )
738+ chrono:: Utc
739+ . with_ymd_and_hms( 1989 , 12 , 7 , 8 , 0 , 4 )
740+ . unwrap( )
741+ . naive_utc( )
731742 ) ;
732743 rt ! ( dur, time:: Duration , time:: Duration :: from_secs( 1893 ) ) ;
733744 rt ! ( dur_micro, time:: Duration , time:: Duration :: new( 1893 , 5000 ) ) ;
@@ -911,13 +922,16 @@ mod tests {
911922 rt ! (
912923 time,
913924 chrono:: NaiveDate ,
914- chrono:: Local :: today ( ) . naive_local ( ) ,
925+ chrono:: Local :: now ( ) . date_naive ( ) ,
915926 ColumnType :: MYSQL_TYPE_DATE
916927 ) ;
917928 rt ! (
918929 datetime,
919930 chrono:: NaiveDateTime ,
920- chrono:: Utc . ymd( 1989 , 12 , 7 ) . and_hms( 8 , 0 , 4 ) . naive_utc( ) ,
931+ chrono:: Utc
932+ . with_ymd_and_hms( 1989 , 12 , 7 , 8 , 0 , 4 )
933+ . unwrap( )
934+ . naive_utc( ) ,
921935 ColumnType :: MYSQL_TYPE_DATETIME
922936 ) ;
923937 rt ! (
0 commit comments