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> {
#[must_use]
pub fn count(&self) -> u32 {
match self {
Self::Rows(rows) => rows.count(),
Self::Grouped(grouped) => u32::try_from(grouped.rows().len()).unwrap_or(u32::MAX),
}
}
#[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(QueryError::unsupported_query(
"grouped queries return grouped rows; call execute() and inspect the grouped result",
)),
}
}
pub fn into_grouped(self) -> Result<PagedGroupedExecutionWithTrace, QueryError> {
match self {
Self::Grouped(grouped) => Ok(grouped),
Self::Rows(_) => Err(QueryError::unsupported_query(
"scalar queries return entity rows; grouped results are not available",
)),
}
}
}