Thanks to visit codestin.com
Credit goes to github.com

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
48 changes: 4 additions & 44 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,46 +261,6 @@ macro_rules! try_getable_date_time {
};
}

#[allow(unused_macros)]
macro_rules! try_getable_time {
( $type: ty ) => {
#[allow(unused_variables)]
impl TryGetable for $type {
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
let column = format!("{}{}", pre, col);
match &res.row {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => {
use sqlx::Row;
row.try_get::<Option<$type>, _>(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
.and_then(|opt| opt.ok_or(TryGetError::Null(column)))
}
#[cfg(feature = "sqlx-postgres")]
QueryResultRow::SqlxPostgres(row) => {
use sqlx::Row;
row.try_get::<Option<$type>, _>(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
.and_then(|opt| opt.ok_or(TryGetError::Null(column)))
}
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(_) => {
panic!("{} unsupported by sqlx-sqlite", stringify!($type))
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get(column.as_str()).map_err(|e| {
debug_print!("{:#?}", e.to_string());
TryGetError::Null(column)
}),
#[allow(unreachable_patterns)]
_ => unreachable!(),
}
}
}
};
}

try_getable_all!(bool);
try_getable_all!(i8);
try_getable_all!(i16);
Expand Down Expand Up @@ -336,16 +296,16 @@ try_getable_all!(chrono::DateTime<chrono::Utc>);
try_getable_all!(chrono::DateTime<chrono::Local>);

#[cfg(feature = "with-time")]
try_getable_time!(time::Date);
try_getable_all!(time::Date);

#[cfg(feature = "with-time")]
try_getable_time!(time::Time);
try_getable_all!(time::Time);

#[cfg(feature = "with-time")]
try_getable_time!(time::PrimitiveDateTime);
try_getable_all!(time::PrimitiveDateTime);

#[cfg(feature = "with-time")]
try_getable_time!(time::OffsetDateTime);
try_getable_all!(time::OffsetDateTime);

#[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal;
Expand Down
8 changes: 8 additions & 0 deletions src/query/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ impl FromQueryResult for JsonValue {
match_sqlite_type!(chrono::NaiveTime);
#[cfg(feature = "with-chrono")]
match_sqlite_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-time")]
match_sqlite_type!(time::Date);
#[cfg(feature = "with-time")]
match_sqlite_type!(time::Time);
#[cfg(feature = "with-time")]
match_sqlite_type!(time::PrimitiveDateTime);
#[cfg(feature = "with-time")]
match_sqlite_type!(time::OffsetDateTime);
try_get_type!(String, col);
#[cfg(feature = "with-uuid")]
try_get_type!(uuid::Uuid, col);
Expand Down
16 changes: 12 additions & 4 deletions tests/time_crate_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ use time::macros::{date, time};
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
#[cfg_attr(
feature = "sqlx-sqlite",
should_panic(expected = "time::Date unsupported by sqlx-sqlite")
)]
async fn main() {
let ctx = TestContext::new("time_crate_tests").await;
create_tables(&ctx.db).await.unwrap();
Expand Down Expand Up @@ -69,5 +65,17 @@ pub async fn create_transaction_log(db: &DatabaseConnection) -> Result<(), DbErr
})
);

#[cfg(feature = "sqlx-sqlite")]
assert_eq!(
json,
json!({
"id": 1,
"date": "2022-03-13",
"time": "16:24:00.0",
"date_time": "2022-03-13 16:24:00.0",
"date_time_tz": "2022-03-13T16:24:00Z",
})
);

Ok(())
}