Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlx-core/src/sqlite/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn decode_datetime(value: SqliteValueRef<'_>) -> Result<PrimitiveDateTime, BoxDy
let dt = match value.type_info().0 {
DataType::Text => decode_datetime_from_text(value.text()?),
DataType::Int | DataType::Int64 => {
let parsed = OffsetDateTime::from_unix_timestamp(value.int64()).unwrap();
let parsed = OffsetDateTime::from_unix_timestamp(value.int64())?;
Some(PrimitiveDateTime::new(parsed.date(), parsed.time()))
}

Expand Down
31 changes: 31 additions & 0 deletions tests/sqlite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ mod time_tests {
use sqlx_oldapi::types::time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
use time::macros::{date, datetime, time};

#[sqlx_macros::test]
async fn out_of_range_unix_timestamps_return_decode_errors() -> anyhow::Result<()> {
use sqlx_oldapi::Connection;

let mut conn = sqlx_oldapi::SqliteConnection::connect("sqlite::memory:").await?;
for timestamp in [i64::MIN, i64::MAX] {
let result = sqlx_oldapi::query_scalar::<_, PrimitiveDateTime>("SELECT ?")
.bind(timestamp)
.fetch_one(&mut conn)
.await;

match result {
Err(sqlx_oldapi::Error::ColumnDecode { source, .. }) => {
assert!(source.is::<time::error::ComponentRange>());
}
other => panic!("expected a timestamp range error for {timestamp}, got {other:?}"),
}
}

// A rejected value must not prevent subsequent queries on the connection.
for timestamp in [-1, 0, 1] {
let decoded = sqlx_oldapi::query_scalar::<_, PrimitiveDateTime>("SELECT ?")
.bind(timestamp)
.fetch_one(&mut conn)
.await?;
assert_eq!(decoded.assume_utc().unix_timestamp(), timestamp);
}

Ok(())
}

test_type!(time_offset_date_time<OffsetDateTime>(
Sqlite,
"SELECT datetime({0}) is datetime(?), {0}, ?",
Expand Down
Loading