@@ -4,7 +4,9 @@ use crate::error::BoxDynError;
44use crate :: odbc:: { DataTypeExt , Odbc , OdbcArgumentValue , OdbcTypeInfo , OdbcValueRef } ;
55use crate :: type_info:: TypeInfo ;
66use crate :: types:: Type ;
7- use chrono:: { DateTime , FixedOffset , Local , NaiveDate , NaiveDateTime , NaiveTime , Utc } ;
7+ use chrono:: {
8+ DateTime , Datelike , FixedOffset , Local , NaiveDate , NaiveDateTime , NaiveTime , Timelike , Utc ,
9+ } ;
810use odbc_api:: DataType ;
911
1012impl Type < Odbc > for NaiveDate {
@@ -80,88 +82,112 @@ impl Type<Odbc> for DateTime<Local> {
8082
8183impl < ' q > Encode < ' q , Odbc > for NaiveDate {
8284 fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
83- buf. push ( OdbcArgumentValue :: Text ( self . format ( "%Y-%m-%d" ) . to_string ( ) ) ) ;
85+ buf. push ( OdbcArgumentValue :: Date ( odbc_api:: sys:: Date {
86+ year : self . year ( ) as i16 ,
87+ month : self . month ( ) as u16 ,
88+ day : self . day ( ) as u16 ,
89+ } ) ) ;
8490 crate :: encode:: IsNull :: No
8591 }
8692
8793 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
88- buf. push ( OdbcArgumentValue :: Text ( self . format ( "%Y-%m-%d" ) . to_string ( ) ) ) ;
94+ buf. push ( OdbcArgumentValue :: Date ( odbc_api:: sys:: Date {
95+ year : self . year ( ) as i16 ,
96+ month : self . month ( ) as u16 ,
97+ day : self . day ( ) as u16 ,
98+ } ) ) ;
8999 crate :: encode:: IsNull :: No
90100 }
91101}
92102
93103impl < ' q > Encode < ' q , Odbc > for NaiveTime {
94104 fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
95- buf. push ( OdbcArgumentValue :: Text ( self . format ( "%H:%M:%S" ) . to_string ( ) ) ) ;
105+ buf. push ( OdbcArgumentValue :: Time ( odbc_api:: sys:: Time {
106+ hour : self . hour ( ) as u16 ,
107+ minute : self . minute ( ) as u16 ,
108+ second : self . second ( ) as u16 ,
109+ } ) ) ;
96110 crate :: encode:: IsNull :: No
97111 }
98112
99113 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
100- buf. push ( OdbcArgumentValue :: Text ( self . format ( "%H:%M:%S" ) . to_string ( ) ) ) ;
114+ buf. push ( OdbcArgumentValue :: Time ( odbc_api:: sys:: Time {
115+ hour : self . hour ( ) as u16 ,
116+ minute : self . minute ( ) as u16 ,
117+ second : self . second ( ) as u16 ,
118+ } ) ) ;
101119 crate :: encode:: IsNull :: No
102120 }
103121}
104122
105123impl < ' q > Encode < ' q , Odbc > for NaiveDateTime {
106124 fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
107- buf. push ( OdbcArgumentValue :: Text (
108- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
109- ) ) ;
125+ buf. push ( OdbcArgumentValue :: Timestamp ( odbc_api:: sys:: Timestamp {
126+ year : self . year ( ) as i16 ,
127+ month : self . month ( ) as u16 ,
128+ day : self . day ( ) as u16 ,
129+ hour : self . hour ( ) as u16 ,
130+ minute : self . minute ( ) as u16 ,
131+ second : self . second ( ) as u16 ,
132+ fraction : self . nanosecond ( ) ,
133+ } ) ) ;
110134 crate :: encode:: IsNull :: No
111135 }
112136
113137 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
114- buf. push ( OdbcArgumentValue :: Text (
115- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
116- ) ) ;
138+ buf. push ( OdbcArgumentValue :: Timestamp ( odbc_api:: sys:: Timestamp {
139+ year : self . year ( ) as i16 ,
140+ month : self . month ( ) as u16 ,
141+ day : self . day ( ) as u16 ,
142+ hour : self . hour ( ) as u16 ,
143+ minute : self . minute ( ) as u16 ,
144+ second : self . second ( ) as u16 ,
145+ fraction : self . nanosecond ( ) ,
146+ } ) ) ;
117147 crate :: encode:: IsNull :: No
118148 }
119149}
120150
121151impl < ' q > Encode < ' q , Odbc > for DateTime < Utc > {
122- fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
123- buf. push ( OdbcArgumentValue :: Text (
124- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
125- ) ) ;
126- crate :: encode:: IsNull :: No
127- }
128-
129152 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
130- buf. push ( OdbcArgumentValue :: Text (
131- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
132- ) ) ;
153+ buf. push ( OdbcArgumentValue :: Text ( self . to_rfc3339 ( ) ) ) ;
133154 crate :: encode:: IsNull :: No
134155 }
135156}
136157
137158impl < ' q > Encode < ' q , Odbc > for DateTime < FixedOffset > {
138- fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
139- buf. push ( OdbcArgumentValue :: Text (
140- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
141- ) ) ;
142- crate :: encode:: IsNull :: No
143- }
144-
145159 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
146- buf. push ( OdbcArgumentValue :: Text (
147- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
148- ) ) ;
160+ buf. push ( OdbcArgumentValue :: Text ( self . to_rfc3339 ( ) ) ) ;
149161 crate :: encode:: IsNull :: No
150162 }
151163}
152164
153165impl < ' q > Encode < ' q , Odbc > for DateTime < Local > {
154166 fn encode ( self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
155- buf. push ( OdbcArgumentValue :: Text (
156- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
157- ) ) ;
167+ let naive = self . naive_local ( ) ;
168+ buf. push ( OdbcArgumentValue :: Timestamp ( odbc_api:: sys:: Timestamp {
169+ year : naive. year ( ) as i16 ,
170+ month : naive. month ( ) as u16 ,
171+ day : naive. day ( ) as u16 ,
172+ hour : naive. hour ( ) as u16 ,
173+ minute : naive. minute ( ) as u16 ,
174+ second : naive. second ( ) as u16 ,
175+ fraction : naive. nanosecond ( ) ,
176+ } ) ) ;
158177 crate :: encode:: IsNull :: No
159178 }
160179
161180 fn encode_by_ref ( & self , buf : & mut Vec < OdbcArgumentValue > ) -> crate :: encode:: IsNull {
162- buf. push ( OdbcArgumentValue :: Text (
163- self . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
164- ) ) ;
181+ let naive = self . naive_local ( ) ;
182+ buf. push ( OdbcArgumentValue :: Timestamp ( odbc_api:: sys:: Timestamp {
183+ year : naive. year ( ) as i16 ,
184+ month : naive. month ( ) as u16 ,
185+ day : naive. day ( ) as u16 ,
186+ hour : naive. hour ( ) as u16 ,
187+ minute : naive. minute ( ) as u16 ,
188+ second : naive. second ( ) as u16 ,
189+ fraction : naive. nanosecond ( ) ,
190+ } ) ) ;
165191 crate :: encode:: IsNull :: No
166192 }
167193}
@@ -621,11 +647,14 @@ mod tests {
621647 let result = <NaiveDate as Encode < Odbc > >:: encode ( date, & mut buf) ;
622648 assert ! ( matches!( result, crate :: encode:: IsNull :: No ) ) ;
623649 assert_eq ! ( buf. len( ) , 1 ) ;
624- if let OdbcArgumentValue :: Text ( text) = & buf[ 0 ] {
625- assert_eq ! ( text, "2020-01-02" ) ;
626- } else {
627- panic ! ( "Expected Text argument" ) ;
628- }
650+ assert_eq ! (
651+ buf[ 0 ] ,
652+ OdbcArgumentValue :: Date ( odbc_api:: sys:: Date {
653+ year: 2020 ,
654+ month: 1 ,
655+ day: 2 ,
656+ } )
657+ ) ;
629658 }
630659
631660 #[ test]
0 commit comments