SQLite support time
crate (#995)
* SQLite support `time` crate * Serialize time types for SQLite query results
This commit is contained in:
parent
5e0c625ac0
commit
fdb3cff29f
@ -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);
|
||||
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
@ -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(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user