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 6 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
5 changes: 5 additions & 0 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ impl ActiveEnum {
let value = <<Self as sea_orm::ActiveEnum>::Value as sea_orm::TryGetable>::try_get(res, pre, col)?;
<Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(sea_orm::TryGetError::DbErr)
}

fn try_get_by_index(res: &sea_orm::QueryResult, idx: usize) -> std::result::Result<Self, sea_orm::TryGetError> {
let value = <<Self as sea_orm::ActiveEnum>::Value as sea_orm::TryGetable>::try_get_by_index(res, idx)?;
<Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(sea_orm::TryGetError::DbErr)
}
}

#[automatically_derived]
Expand Down
18 changes: 17 additions & 1 deletion src/database/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,30 @@ impl MockDatabaseTrait for MockDatabase {
}

impl MockRow {
/// Try to get the values of a [MockRow] and fail gracefully on error
/// Get a value from the [MockRow]
pub fn try_get<T>(&self, col: &str) -> Result<T, DbErr>
where
T: ValueType,
{
T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Type(e.to_string()))
}

/// Get a value from the [MockRow] based on the order of column name
pub fn try_get_by_index<T>(&self, idx: usize) -> Result<T, DbErr>
where
T: ValueType,
{
let (_, value) = self
.values
.iter()
.nth(idx)
.ok_or(DbErr::Query(RuntimeErr::Internal(format!(
"Column at index {} not found",
idx
))))?;
T::try_from(value.clone()).map_err(|e| DbErr::Type(e.to_string()))
}

/// An iterator over the keys and values of a mock row
pub fn into_column_value_tuples(self) -> impl Iterator<Item = (String, Value)> {
self.values.into_iter()
Expand Down
7 changes: 7 additions & 0 deletions src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ where
.map(|value| T::try_from_value(&value).map_err(TryGetError::DbErr))
.collect()
}

fn try_get_by_index(res: &QueryResult, idx: usize) -> Result<Self, TryGetError> {
<T::ValueVec as TryGetable>::try_get_by_index(res, idx)?
.into_iter()
.map(|value| T::try_from_value(&value).map_err(TryGetError::DbErr))
.collect()
}
}

#[cfg(test)]
Expand Down
Loading