use crate::{
db::{EntityResponse, PagedGroupedExecutionWithTrace, query::intent::QueryError},
traits::EntityKind,
};
#[derive(Debug)]
pub enum LoadQueryResult<E: EntityKind> {
Rows(EntityResponse<E>),
Grouped(PagedGroupedExecutionWithTrace),
}
impl<E: EntityKind> LoadQueryResult<E> {
pub(crate) const fn rows(rows: EntityResponse<E>) -> Self {
Self::Rows(rows)
}
pub(crate) const fn grouped(grouped: PagedGroupedExecutionWithTrace) -> Self {
Self::Grouped(grouped)
}
fn grouped_count(grouped: &PagedGroupedExecutionWithTrace) -> u32 {
u32::try_from(grouped.rows().len()).unwrap_or(u32::MAX)
}
fn grouped_rows_required_error() -> QueryError {
QueryError::unsupported_query(
"grouped queries return grouped rows; call execute() and inspect the grouped result",
)
}
fn scalar_rows_required_error() -> QueryError {
QueryError::unsupported_query(
"scalar queries return entity rows; grouped results are not available",
)
}
#[must_use]
pub fn count(&self) -> u32 {
match self {
Self::Rows(rows) => rows.count(),
Self::Grouped(grouped) => Self::grouped_count(grouped),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.count() == 0
}
pub fn into_rows(self) -> Result<EntityResponse<E>, QueryError> {
match self {
Self::Rows(rows) => Ok(rows),
Self::Grouped(_) => Err(Self::grouped_rows_required_error()),
}
}
pub fn into_grouped(self) -> Result<PagedGroupedExecutionWithTrace, QueryError> {
match self {
Self::Grouped(grouped) => Ok(grouped),
Self::Rows(_) => Err(Self::scalar_rows_required_error()),
}
}
}