DateTimeWithTimeZone Postgres support

This commit is contained in:
Chris Tsang 2021-08-10 16:56:11 +08:00 committed by Chris Tsang
parent ba226a2b62
commit ff8d921343

View File

@ -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<Self, DbErr> {
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<Self, DbErr> {
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::<Option<$type>, _>(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!(bool);
try_getable_all!(i8); try_getable_all!(i8);
try_getable_all!(i16); try_getable_all!(i16);
@ -256,8 +314,8 @@ try_getable_all!(serde_json::Value);
#[cfg(feature = "with-chrono")] #[cfg(feature = "with-chrono")]
try_getable_all!(chrono::NaiveDateTime); try_getable_all!(chrono::NaiveDateTime);
// #[cfg(feature = "with-chrono")] #[cfg(feature = "with-chrono")]
// try_getable_all!(chrono::DateTime<chrono::FixedOffset>); try_getable_postgres!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "with-rust_decimal")] #[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal; use rust_decimal::Decimal;