Skip to content

Commit

Permalink
Merge pull request #1311 from SeaQL/select-into-tuple
Browse files Browse the repository at this point in the history
Select into tuple
  • Loading branch information
tyt2y3 authored Jan 10, 2023
2 parents ecb299a + 3ae44a0 commit 4210526
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 127 deletions.
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

0 comments on commit 4210526

Please sign in to comment.