Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select into tuple #1311

Merged
merged 12 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ impl ActiveEnum {

#[automatically_derived]
impl sea_orm::TryGetable for #ident {
fn try_get(res: &sea_orm::QueryResult, pre: &str, col: &str) -> std::result::Result<Self, sea_orm::TryGetError> {
let value = <<Self as sea_orm::ActiveEnum>::Value as sea_orm::TryGetable>::try_get(res, pre, col)?;
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I) -> std::result::Result<Self, sea_orm::TryGetError> {
let value = <<Self as sea_orm::ActiveEnum>::Value as sea_orm::TryGetable>::try_get_by(res, idx)?;
<Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(sea_orm::TryGetError::DbErr)
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/database/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,25 @@ impl MockDatabaseTrait for MockDatabase {
}

impl MockRow {
/// Try to get the values of a [MockRow] and fail gracefully on error
pub fn try_get<T>(&self, col: &str) -> Result<T, DbErr>
/// Get a value from the [MockRow]
pub fn try_get<T, I: crate::ColIdx>(&self, index: I) -> Result<T, DbErr>
where
T: ValueType,
{
T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Type(e.to_string()))
if let Some(index) = index.as_str() {
T::try_from(self.values.get(index).unwrap().clone())
.map_err(|e| DbErr::Type(e.to_string()))
} else if let Some(index) = index.as_usize() {
let (_, value) = self.values.iter().nth(*index).ok_or_else(|| {
DbErr::Query(RuntimeErr::Internal(format!(
"Column at index {} not found",
index
)))
})?;
T::try_from(value.clone()).map_err(|e| DbErr::Type(e.to_string()))
} else {
unreachable!("Missing ColIdx implementation for MockRow");
}
}

/// An iterator over the keys and values of a mock row
Expand Down Expand Up @@ -686,10 +699,10 @@ mod tests {
);
let mocked_row = row.into_mock_row();

let a_id = mocked_row.try_get::<i32>("A_id");
let a_id = mocked_row.try_get::<i32, _>("A_id");
assert!(a_id.is_ok());
assert_eq!(1, a_id.unwrap());
let b_id = mocked_row.try_get::<i32>("B_id");
let b_id = mocked_row.try_get::<i32, _>("B_id");
assert!(b_id.is_ok());
assert_eq!(2, b_id.unwrap());
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl DatabaseTransaction {
if let Err(sqlx::Error::RowNotFound) = err {
Ok(None)
} else {
err.map_err(|e| sqlx_error_to_query_err(e))
err.map_err(sqlx_error_to_query_err)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ where
T: ActiveEnum,
T::ValueVec: TryGetable,
{
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
<T::ValueVec as TryGetable>::try_get(res, pre, col)?
fn try_get_by<I: crate::ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
<T::ValueVec as TryGetable>::try_get_by(res, index)?
.into_iter()
.map(|value| T::try_from_value(&value).map_err(TryGetError::DbErr))
.collect()
Expand Down
Loading