|
| 1 | +use crate::decode::Decode; |
| 2 | +use crate::encode::Encode; |
| 3 | +use crate::error::BoxDynError; |
| 4 | +use crate::odbc::{DataTypeExt, Odbc, OdbcArgumentValue, OdbcTypeInfo, OdbcValueRef}; |
| 5 | +use crate::types::Type; |
| 6 | +use time::{Date, OffsetDateTime, PrimitiveDateTime, Time}; |
| 7 | + |
| 8 | +impl Type<Odbc> for OffsetDateTime { |
| 9 | + fn type_info() -> OdbcTypeInfo { |
| 10 | + OdbcTypeInfo::timestamp(6) |
| 11 | + } |
| 12 | + fn compatible(ty: &OdbcTypeInfo) -> bool { |
| 13 | + ty.data_type().accepts_datetime_data() || ty.data_type().accepts_character_data() |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl Type<Odbc> for PrimitiveDateTime { |
| 18 | + fn type_info() -> OdbcTypeInfo { |
| 19 | + OdbcTypeInfo::timestamp(6) |
| 20 | + } |
| 21 | + fn compatible(ty: &OdbcTypeInfo) -> bool { |
| 22 | + ty.data_type().accepts_datetime_data() || ty.data_type().accepts_character_data() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl Type<Odbc> for Date { |
| 27 | + fn type_info() -> OdbcTypeInfo { |
| 28 | + OdbcTypeInfo::new(odbc_api::DataType::Date) |
| 29 | + } |
| 30 | + fn compatible(ty: &OdbcTypeInfo) -> bool { |
| 31 | + ty.data_type().accepts_datetime_data() || ty.data_type().accepts_character_data() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl Type<Odbc> for Time { |
| 36 | + fn type_info() -> OdbcTypeInfo { |
| 37 | + OdbcTypeInfo::time(6) |
| 38 | + } |
| 39 | + fn compatible(ty: &OdbcTypeInfo) -> bool { |
| 40 | + ty.data_type().accepts_datetime_data() || ty.data_type().accepts_character_data() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'q> Encode<'q, Odbc> for OffsetDateTime { |
| 45 | + fn encode(self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 46 | + let utc_dt = self.to_offset(time::UtcOffset::UTC); |
| 47 | + let primitive_dt = PrimitiveDateTime::new(utc_dt.date(), utc_dt.time()); |
| 48 | + buf.push(OdbcArgumentValue::Text(primitive_dt.to_string())); |
| 49 | + crate::encode::IsNull::No |
| 50 | + } |
| 51 | + |
| 52 | + fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 53 | + let utc_dt = self.to_offset(time::UtcOffset::UTC); |
| 54 | + let primitive_dt = PrimitiveDateTime::new(utc_dt.date(), utc_dt.time()); |
| 55 | + buf.push(OdbcArgumentValue::Text(primitive_dt.to_string())); |
| 56 | + crate::encode::IsNull::No |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<'q> Encode<'q, Odbc> for PrimitiveDateTime { |
| 61 | + fn encode(self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 62 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 63 | + crate::encode::IsNull::No |
| 64 | + } |
| 65 | + |
| 66 | + fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 67 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 68 | + crate::encode::IsNull::No |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'q> Encode<'q, Odbc> for Date { |
| 73 | + fn encode(self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 74 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 75 | + crate::encode::IsNull::No |
| 76 | + } |
| 77 | + |
| 78 | + fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 79 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 80 | + crate::encode::IsNull::No |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<'q> Encode<'q, Odbc> for Time { |
| 85 | + fn encode(self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 86 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 87 | + crate::encode::IsNull::No |
| 88 | + } |
| 89 | + |
| 90 | + fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue>) -> crate::encode::IsNull { |
| 91 | + buf.push(OdbcArgumentValue::Text(self.to_string())); |
| 92 | + crate::encode::IsNull::No |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<'r> Decode<'r, Odbc> for OffsetDateTime { |
| 97 | + fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> { |
| 98 | + if let Some(text) = value.text { |
| 99 | + // Try parsing as ISO-8601 timestamp with timezone |
| 100 | + if let Ok(dt) = OffsetDateTime::parse( |
| 101 | + text, |
| 102 | + &time::format_description::well_known::Iso8601::DEFAULT, |
| 103 | + ) { |
| 104 | + return Ok(dt); |
| 105 | + } |
| 106 | + // Try parsing as primitive datetime and assume UTC |
| 107 | + if let Ok(dt) = PrimitiveDateTime::parse( |
| 108 | + text, |
| 109 | + &time::format_description::well_known::Iso8601::DEFAULT, |
| 110 | + ) { |
| 111 | + return Ok(dt.assume_utc()); |
| 112 | + } |
| 113 | + // Try custom formats that ODBC might return |
| 114 | + if let Ok(dt) = time::PrimitiveDateTime::parse( |
| 115 | + text, |
| 116 | + &time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"), |
| 117 | + ) { |
| 118 | + return Ok(dt.assume_utc()); |
| 119 | + } |
| 120 | + } |
| 121 | + Err("ODBC: cannot decode OffsetDateTime".into()) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl<'r> Decode<'r, Odbc> for PrimitiveDateTime { |
| 126 | + fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> { |
| 127 | + if let Some(text) = value.text { |
| 128 | + // Try parsing as ISO-8601 |
| 129 | + if let Ok(dt) = PrimitiveDateTime::parse( |
| 130 | + text, |
| 131 | + &time::format_description::well_known::Iso8601::DEFAULT, |
| 132 | + ) { |
| 133 | + return Ok(dt); |
| 134 | + } |
| 135 | + // Try custom formats that ODBC might return |
| 136 | + if let Ok(dt) = PrimitiveDateTime::parse( |
| 137 | + text, |
| 138 | + &time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"), |
| 139 | + ) { |
| 140 | + return Ok(dt); |
| 141 | + } |
| 142 | + if let Ok(dt) = PrimitiveDateTime::parse( |
| 143 | + text, |
| 144 | + &time::macros::format_description!( |
| 145 | + "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]" |
| 146 | + ), |
| 147 | + ) { |
| 148 | + return Ok(dt); |
| 149 | + } |
| 150 | + } |
| 151 | + Err("ODBC: cannot decode PrimitiveDateTime".into()) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl<'r> Decode<'r, Odbc> for Date { |
| 156 | + fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> { |
| 157 | + if let Some(text) = value.text { |
| 158 | + if let Ok(date) = Date::parse( |
| 159 | + text, |
| 160 | + &time::macros::format_description!("[year]-[month]-[day]"), |
| 161 | + ) { |
| 162 | + return Ok(date); |
| 163 | + } |
| 164 | + if let Ok(date) = Date::parse( |
| 165 | + text, |
| 166 | + &time::format_description::well_known::Iso8601::DEFAULT, |
| 167 | + ) { |
| 168 | + return Ok(date); |
| 169 | + } |
| 170 | + } |
| 171 | + Err("ODBC: cannot decode Date".into()) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +impl<'r> Decode<'r, Odbc> for Time { |
| 176 | + fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> { |
| 177 | + if let Some(text) = value.text { |
| 178 | + if let Ok(time) = Time::parse( |
| 179 | + text, |
| 180 | + &time::macros::format_description!("[hour]:[minute]:[second]"), |
| 181 | + ) { |
| 182 | + return Ok(time); |
| 183 | + } |
| 184 | + if let Ok(time) = Time::parse( |
| 185 | + text, |
| 186 | + &time::macros::format_description!("[hour]:[minute]:[second].[subsecond]"), |
| 187 | + ) { |
| 188 | + return Ok(time); |
| 189 | + } |
| 190 | + } |
| 191 | + Err("ODBC: cannot decode Time".into()) |
| 192 | + } |
| 193 | +} |
0 commit comments