Skip to content

Commit cd3c053

Browse files
committed
odbc(chrono): accept numeric-reported types; parse compact YYYYMMDD for NaiveDateTime via SQLite ODBC; fmt
1 parent f15a3cc commit cd3c053

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sqlx-core/src/odbc/types/chrono.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ impl Type<Odbc> for NaiveDate {
1313
fn compatible(ty: &OdbcTypeInfo) -> bool {
1414
matches!(ty.data_type(), DataType::Date)
1515
|| ty.data_type().accepts_character_data()
16+
|| ty.data_type().accepts_numeric_data()
1617
|| matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
1718
}
1819
}
@@ -24,6 +25,7 @@ impl Type<Odbc> for NaiveTime {
2425
fn compatible(ty: &OdbcTypeInfo) -> bool {
2526
matches!(ty.data_type(), DataType::Time { .. })
2627
|| ty.data_type().accepts_character_data()
28+
|| ty.data_type().accepts_numeric_data()
2729
|| matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
2830
}
2931
}
@@ -35,6 +37,7 @@ impl Type<Odbc> for NaiveDateTime {
3537
fn compatible(ty: &OdbcTypeInfo) -> bool {
3638
matches!(ty.data_type(), DataType::Timestamp { .. })
3739
|| ty.data_type().accepts_character_data()
40+
|| ty.data_type().accepts_numeric_data()
3841
|| matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
3942
}
4043
}
@@ -46,6 +49,7 @@ impl Type<Odbc> for DateTime<Utc> {
4649
fn compatible(ty: &OdbcTypeInfo) -> bool {
4750
matches!(ty.data_type(), DataType::Timestamp { .. })
4851
|| ty.data_type().accepts_character_data()
52+
|| ty.data_type().accepts_numeric_data()
4953
|| matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
5054
}
5155
}
@@ -57,6 +61,7 @@ impl Type<Odbc> for DateTime<FixedOffset> {
5761
fn compatible(ty: &OdbcTypeInfo) -> bool {
5862
matches!(ty.data_type(), DataType::Timestamp { .. })
5963
|| ty.data_type().accepts_character_data()
64+
|| ty.data_type().accepts_numeric_data()
6065
|| matches!(ty.data_type(), DataType::Other { .. } | DataType::Unknown)
6166
}
6267
}
@@ -163,6 +168,18 @@ impl<'q> Encode<'q, Odbc> for DateTime<Local> {
163168
impl<'r> Decode<'r, Odbc> for NaiveDate {
164169
fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
165170
let s = <String as Decode<'r, Odbc>>::decode(value)?;
171+
// Accept YYYYMMDD (some SQLite ODBC configs) as a date as well
172+
if s.len() == 8 && s.chars().all(|c| c.is_ascii_digit()) {
173+
if let (Ok(y), Ok(m), Ok(d)) = (
174+
s[0..4].parse::<i32>(),
175+
s[4..6].parse::<u32>(),
176+
s[6..8].parse::<u32>(),
177+
) {
178+
if let Some(date) = NaiveDate::from_ymd_opt(y, m, d) {
179+
return Ok(date.and_hms_opt(0, 0, 0).unwrap());
180+
}
181+
}
182+
}
166183
Ok(s.parse()?)
167184
}
168185
}

0 commit comments

Comments
 (0)