diff --git a/src/executor/query.rs b/src/executor/query.rs index eb35a3a0..327ef99f 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -538,38 +538,35 @@ macro_rules! try_getable_uuid { ( $type: ty, $conversion_fn: expr ) => { #[allow(unused_variables, unreachable_code)] impl TryGetable for $type { - fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { - let column = format!("{}{}", pre, col); + fn try_get_by(res: &QueryResult, idx: I) -> Result { let res: Result = match &res.row { #[cfg(feature = "sqlx-mysql")] QueryResultRow::SqlxMySql(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::, _>(idx.as_sqlx_mysql_index()) .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_else(|| err_null_idx_col(idx))) } #[cfg(feature = "sqlx-postgres")] QueryResultRow::SqlxPostgres(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::, _>(idx.as_sqlx_postgres_index()) .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_else(|| err_null_idx_col(idx))) } #[cfg(feature = "sqlx-sqlite")] QueryResultRow::SqlxSqlite(row) => { use sqlx::Row; - row.try_get::, _>(column.as_str()) + row.try_get::, _>(idx.as_sqlx_sqlite_index()) .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_else(|| err_null_idx_col(idx))) } #[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) - }) - } + QueryResultRow::Mock(row) => row.try_get::(idx).map_err(|e| { + debug_print!("{:#?}", e.to_string()); + err_null_idx_col(idx) + }), #[allow(unreachable_patterns)] _ => unreachable!(), }; @@ -733,8 +730,7 @@ mod postgres_array { ( $type: ty, $conversion_fn: expr ) => { #[allow(unused_variables, unreachable_code)] impl TryGetable for Vec<$type> { - fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { - let column = format!("{}{}", pre, col); + fn try_get_by(res: &QueryResult, idx: I) -> Result { let res: Result, TryGetError> = match &res.row { #[cfg(feature = "sqlx-mysql")] QueryResultRow::SqlxMySql(row) => { @@ -743,21 +739,21 @@ mod postgres_array { #[cfg(feature = "sqlx-postgres")] QueryResultRow::SqlxPostgres(row) => { use sqlx::Row; - row.try_get::>, _>(column.as_str()) + row.try_get::>, _>(idx.as_sqlx_postgres_index()) .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_else(|| err_null_idx_col(idx))) } #[cfg(feature = "sqlx-sqlite")] QueryResultRow::SqlxSqlite(_) => { panic!("{} unsupported by sqlx-sqlite", stringify!($type)) } #[cfg(feature = "mock")] - QueryResultRow::Mock(row) => row - .try_get::>(column.as_str()) - .map_err(|e| { + QueryResultRow::Mock(row) => { + row.try_get::, _>(idx).map_err(|e| { debug_print!("{:#?}", e.to_string()); - TryGetError::Null(column) - }), + err_null_idx_col(idx) + }) + } #[allow(unreachable_patterns)] _ => unreachable!(), }; diff --git a/tests/collection_tests.rs b/tests/collection_tests.rs index 74b987c4..dc961d13 100644 --- a/tests/collection_tests.rs +++ b/tests/collection_tests.rs @@ -30,8 +30,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: Some(vec![Tea::BreakfastTea]), colors: vec![Color::Black], colors_opt: Some(vec![Color::Black]), - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } .into_active_model() .insert(db) @@ -44,8 +44,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: Some(vec![Tea::BreakfastTea]), colors: vec![Color::Black], colors_opt: Some(vec![Color::Black]), - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } ); @@ -58,8 +58,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: None, colors: vec![Color::Black], colors_opt: None, - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } .into_active_model() .insert(db) @@ -72,8 +72,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: None, colors: vec![Color::Black], colors_opt: None, - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } ); @@ -86,8 +86,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: Some(vec![]), colors: vec![], colors_opt: Some(vec![]), - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } .into_active_model() .insert(db) @@ -100,8 +100,8 @@ pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: Some(vec![]), colors: vec![], colors_opt: Some(vec![]), - uuid: vec![uuid.clone()], - uuid_hyphenated: vec![uuid.clone().hyphenated()], + uuid: vec![uuid], + uuid_hyphenated: vec![uuid.hyphenated()], } ); @@ -134,8 +134,8 @@ pub async fn update_collection(db: &DatabaseConnection) -> Result<(), DbErr> { teas_opt: Set(None), colors: Set(vec![Color::White]), colors_opt: Set(None), - uuid: Set(vec![uuid.clone()]), - uuid_hyphenated: Set(vec![uuid.clone().hyphenated()]), + uuid: Set(vec![uuid]), + uuid_hyphenated: Set(vec![uuid.hyphenated()]), } .update(db) .await?; diff --git a/tests/uuid_fmt_tests.rs b/tests/uuid_fmt_tests.rs index 80b6fef6..a99f35ba 100644 --- a/tests/uuid_fmt_tests.rs +++ b/tests/uuid_fmt_tests.rs @@ -24,11 +24,11 @@ pub async fn insert_uuid_fmt(db: &DatabaseConnection) -> Result<(), DbErr> { let uuid_fmt = uuid_fmt::Model { id: 1, - uuid: uuid.clone(), - uuid_braced: uuid.clone().braced(), - uuid_hyphenated: uuid.clone().hyphenated(), - uuid_simple: uuid.clone().simple(), - uuid_urn: uuid.clone().urn(), + uuid: uuid, + uuid_braced: uuid.braced(), + uuid_hyphenated: uuid.hyphenated(), + uuid_simple: uuid.simple(), + uuid_urn: uuid.urn(), }; let result = uuid_fmt.clone().into_active_model().insert(db).await?;