From ff8d921343e4c4c1097cfbdd8ce7d90907bc2261 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 10 Aug 2021 16:56:11 +0800 Subject: [PATCH] DateTimeWithTimeZone Postgres support --- src/executor/query.rs | 62 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/executor/query.rs b/src/executor/query.rs index 99ebaba63..b1ecd988d 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -237,6 +237,64 @@ macro_rules! try_getable_mysql { }; } +macro_rules! try_getable_postgres { + ( $type: ty ) => { + impl TryGetable for $type { + fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { + let column = format!("{}{}", pre, col); + match &res.row { + #[cfg(feature = "sqlx-mysql")] + QueryResultRow::SqlxMySql(_) => { + panic!("{} unsupported by sqlx-mysql", stringify!($type)) + } + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => { + use sqlx::Row; + row.try_get(column.as_str()) + .map_err(crate::sqlx_error_to_query_err) + } + #[cfg(feature = "sqlx-sqlite")] + QueryResultRow::SqlxSqlite(_) => { + panic!("{} unsupported by sqlx-sqlite", stringify!($type)) + } + #[cfg(feature = "mock")] + QueryResultRow::Mock(row) => Ok(row.try_get(column.as_str())?), + } + } + } + + impl TryGetable for Option<$type> { + fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { + let column = format!("{}{}", pre, col); + match &res.row { + #[cfg(feature = "sqlx-mysql")] + QueryResultRow::SqlxMySql(_) => { + panic!("{} unsupported by sqlx-mysql", stringify!($type)) + } + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => { + use sqlx::Row; + row.try_get::, _>(column.as_str()) + .map_err(crate::sqlx_error_to_query_err) + } + #[cfg(feature = "sqlx-sqlite")] + QueryResultRow::SqlxSqlite(_) => { + panic!("{} unsupported by sqlx-sqlite", stringify!($type)) + } + #[cfg(feature = "mock")] + QueryResultRow::Mock(row) => match row.try_get(column.as_str()) { + Ok(v) => Ok(Some(v)), + Err(e) => { + debug_print!("{:#?}", e.to_string()); + Ok(None) + } + }, + } + } + } + }; +} + try_getable_all!(bool); try_getable_all!(i8); try_getable_all!(i16); @@ -256,8 +314,8 @@ try_getable_all!(serde_json::Value); #[cfg(feature = "with-chrono")] try_getable_all!(chrono::NaiveDateTime); -// #[cfg(feature = "with-chrono")] -// try_getable_all!(chrono::DateTime); +#[cfg(feature = "with-chrono")] +try_getable_postgres!(chrono::DateTime); #[cfg(feature = "with-rust_decimal")] use rust_decimal::Decimal;