Skip to content

Commit 52fc57c

Browse files
committed
refactor(odbc): streamline error handling in date and time decoding
This commit refactors the error handling in the Decode implementations for NaiveDate, NaiveTime, NaiveDateTime, and DateTime types by simplifying the syntax for creating date and time objects. The changes enhance code readability and maintainability while ensuring robust error reporting for invalid date and time values.
1 parent 0ff2953 commit 52fc57c

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

sqlx-core/src/odbc/connection/odbc_bridge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ where
203203
| DataType::Varbinary { length }
204204
| DataType::LongVarbinary { length } => BufferDesc::Binary {
205205
length: if let Some(length) = length {
206-
std::cmp::max(std::cmp::min(length.get(), MAX_TEXT_LEN), MIN_TEXT_LEN)
206+
length.get().clamp(MIN_TEXT_LEN, MAX_TEXT_LEN)
207207
} else {
208208
MAX_TEXT_LEN
209209
},
@@ -219,7 +219,7 @@ where
219219
..
220220
} => BufferDesc::Text {
221221
max_str_len: if let Some(length) = length {
222-
std::cmp::max(std::cmp::min(length.get(), MAX_TEXT_LEN), MIN_TEXT_LEN)
222+
length.get().clamp(MIN_TEXT_LEN, MAX_TEXT_LEN)
223223
} else {
224224
MAX_TEXT_LEN
225225
},

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ impl<'r> Decode<'r, Odbc> for NaiveDate {
214214
date_val.year as i32,
215215
date_val.month as u32,
216216
date_val.day as u32,
217-
).ok_or_else(|| "ODBC: invalid date values".to_string())?);
217+
)
218+
.ok_or_else(|| "ODBC: invalid date values".to_string())?);
218219
}
219220

220221
// Handle text values first (most common for dates)
@@ -269,7 +270,8 @@ impl<'r> Decode<'r, Odbc> for NaiveTime {
269270
time_val.hour as u32,
270271
time_val.minute as u32,
271272
time_val.second as u32,
272-
).ok_or_else(|| "ODBC: invalid time values".to_string())?);
273+
)
274+
.ok_or_else(|| "ODBC: invalid time values".to_string())?);
273275
}
274276

275277
let mut s = <String as Decode<'r, Odbc>>::decode(value)?;
@@ -289,17 +291,16 @@ impl<'r> Decode<'r, Odbc> for NaiveDateTime {
289291
if let Some(ts_val) = value.timestamp() {
290292
// Convert odbc_api::sys::Timestamp to NaiveDateTime
291293
// The ODBC Timestamp structure typically has year, month, day, hour, minute, second fields
292-
let date = NaiveDate::from_ymd_opt(
293-
ts_val.year as i32,
294-
ts_val.month as u32,
295-
ts_val.day as u32,
296-
).ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?;
294+
let date =
295+
NaiveDate::from_ymd_opt(ts_val.year as i32, ts_val.month as u32, ts_val.day as u32)
296+
.ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?;
297297

298298
let time = NaiveTime::from_hms_opt(
299299
ts_val.hour as u32,
300300
ts_val.minute as u32,
301301
ts_val.second as u32,
302-
).ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?;
302+
)
303+
.ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?;
303304

304305
return Ok(NaiveDateTime::new(date, time));
305306
}
@@ -331,16 +332,14 @@ impl<'r> Decode<'r, Odbc> for DateTime<Utc> {
331332
// Convert odbc_api::sys::Timestamp to DateTime<Utc>
332333
// The ODBC Timestamp structure typically has year, month, day, hour, minute, second fields
333334
let naive_dt = NaiveDateTime::new(
334-
NaiveDate::from_ymd_opt(
335-
ts_val.year as i32,
336-
ts_val.month as u32,
337-
ts_val.day as u32,
338-
).ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
335+
NaiveDate::from_ymd_opt(ts_val.year as i32, ts_val.month as u32, ts_val.day as u32)
336+
.ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
339337
NaiveTime::from_hms_opt(
340338
ts_val.hour as u32,
341339
ts_val.minute as u32,
342340
ts_val.second as u32,
343-
).ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
341+
)
342+
.ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
344343
);
345344

346345
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc));
@@ -378,16 +377,14 @@ impl<'r> Decode<'r, Odbc> for DateTime<FixedOffset> {
378377
// Convert odbc_api::sys::Timestamp to DateTime<FixedOffset>
379378
// The ODBC Timestamp structure typically has year, month, day, hour, minute, second fields
380379
let naive_dt = NaiveDateTime::new(
381-
NaiveDate::from_ymd_opt(
382-
ts_val.year as i32,
383-
ts_val.month as u32,
384-
ts_val.day as u32,
385-
).ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
380+
NaiveDate::from_ymd_opt(ts_val.year as i32, ts_val.month as u32, ts_val.day as u32)
381+
.ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
386382
NaiveTime::from_hms_opt(
387383
ts_val.hour as u32,
388384
ts_val.minute as u32,
389385
ts_val.second as u32,
390-
).ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
386+
)
387+
.ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
391388
);
392389

393390
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).fixed_offset());
@@ -429,19 +426,19 @@ impl<'r> Decode<'r, Odbc> for DateTime<Local> {
429426
// Convert odbc_api::sys::Timestamp to DateTime<Local>
430427
// The ODBC Timestamp structure typically has year, month, day, hour, minute, second fields
431428
let naive_dt = NaiveDateTime::new(
432-
NaiveDate::from_ymd_opt(
433-
ts_val.year as i32,
434-
ts_val.month as u32,
435-
ts_val.day as u32,
436-
).ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
429+
NaiveDate::from_ymd_opt(ts_val.year as i32, ts_val.month as u32, ts_val.day as u32)
430+
.ok_or_else(|| "ODBC: invalid date values in timestamp".to_string())?,
437431
NaiveTime::from_hms_opt(
438432
ts_val.hour as u32,
439433
ts_val.minute as u32,
440434
ts_val.second as u32,
441-
).ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
435+
)
436+
.ok_or_else(|| "ODBC: invalid time values in timestamp".to_string())?,
442437
);
443438

444-
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).with_timezone(&Local));
439+
return Ok(
440+
DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).with_timezone(&Local)
441+
);
445442
}
446443

447444
let mut s = <String as Decode<'r, Odbc>>::decode(value)?;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ impl<'r> Decode<'r, Odbc> for Date {
233233
// The ODBC Date structure typically has year, month, day fields
234234
let month = time::Month::try_from(date_val.month as u8)
235235
.map_err(|_| "ODBC: invalid month value")?;
236-
return Ok(Date::from_calendar_date(date_val.year as i32, month, date_val.day as u8)?);
236+
return Ok(Date::from_calendar_date(
237+
date_val.year as i32,
238+
month,
239+
date_val.day as u8,
240+
)?);
237241
}
238242

239243
// Handle numeric YYYYMMDD format first

0 commit comments

Comments
 (0)