Skip to content

Commit

Permalink
DateTimeWithTimeZone Postgres support
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 10, 2021
1 parent ba226a2 commit ff8d921
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(i8);
try_getable_all!(i16);
Expand All @@ -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<chrono::FixedOffset>);
#[cfg(feature = "with-chrono")]
try_getable_postgres!(chrono::DateTime<chrono::FixedOffset>);

#[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal;
Expand Down

0 comments on commit ff8d921

Please sign in to comment.