From 3de0078a31d9776fa71736c3b2a296ecdaffb9a7 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 1 Dec 2022 13:23:24 +0800 Subject: [PATCH] Optimize TryGetableFromJson (#1249) --- src/executor/query.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/executor/query.rs b/src/executor/query.rs index 634596861..025674de8 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -788,42 +788,42 @@ where #[allow(unused_variables, unreachable_code)] fn try_get_from_json(res: &QueryResult, pre: &str, col: &str) -> Result { let column = format!("{}{}", pre, col); - let res: Result<_, _> = match &res.row { + match &res.row { #[cfg(feature = "sqlx-mysql")] QueryResultRow::SqlxMySql(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::>, _>(column.as_str()) .map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e))) - .and_then(|opt| opt.ok_or(TryGetError::Null(column))) + .and_then(|opt| opt.ok_or(TryGetError::Null(column)).map(|json| json.0)) } #[cfg(feature = "sqlx-postgres")] QueryResultRow::SqlxPostgres(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::>, _>(column.as_str()) .map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e))) - .and_then(|opt| opt.ok_or(TryGetError::Null(column))) + .and_then(|opt| opt.ok_or(TryGetError::Null(column)).map(|json| json.0)) } #[cfg(feature = "sqlx-sqlite")] QueryResultRow::SqlxSqlite(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::>, _>(column.as_str()) .map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e))) - .and_then(|opt| opt.ok_or(TryGetError::Null(column))) + .and_then(|opt| opt.ok_or(TryGetError::Null(column)).map(|json| json.0)) } #[cfg(feature = "mock")] - QueryResultRow::Mock(row) => { - row.try_get::(column.as_str()) - .map_err(|e| { - debug_print!("{:#?}", e.to_string()); - TryGetError::Null(column) - }) - } + QueryResultRow::Mock(row) => row + .try_get::(column.as_str()) + .map_err(|e| { + debug_print!("{:#?}", e.to_string()); + TryGetError::Null(column) + }) + .and_then(|json| { + serde_json::from_value(json) + .map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) + }), #[allow(unreachable_patterns)] _ => unreachable!(), - }; - res.and_then(|json| { - serde_json::from_value(json).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) - }) + } } }