use crate::db::{
access::{AccessPlan, AccessStrategy},
query::plan::{GroupHavingSpec, GroupPlan, GroupSpec, LogicalPlan, expr::ProjectionSelection},
};
use crate::{traits::FieldValue, value::Value};
#[cfg(test)]
use crate::db::{
access::AccessPath,
predicate::MissingRowPolicy,
query::plan::{LoadSpec, QueryMode, ScalarPlan},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AccessPlannedQuery {
pub(crate) logical: LogicalPlan,
pub(crate) access: AccessPlan<Value>,
pub(crate) projection_selection: ProjectionSelection,
}
impl AccessPlannedQuery {
#[must_use]
#[cfg(test)]
pub(crate) fn new(access: AccessPath<Value>, consistency: MissingRowPolicy) -> Self {
Self {
logical: LogicalPlan::Scalar(ScalarPlan {
mode: QueryMode::Load(LoadSpec::new()),
predicate: None,
order: None,
distinct: false,
delete_limit: None,
page: None,
consistency,
}),
access: AccessPlan::path(access),
projection_selection: ProjectionSelection::All,
}
}
#[must_use]
pub(crate) fn from_parts<K>(logical: LogicalPlan, access: AccessPlan<K>) -> Self
where
K: FieldValue,
{
Self {
logical,
access: access.into_value_plan(),
projection_selection: ProjectionSelection::All,
}
}
#[must_use]
pub(crate) fn from_parts_with_projection<K>(
logical: LogicalPlan,
access: AccessPlan<K>,
projection_selection: ProjectionSelection,
) -> Self
where
K: FieldValue,
{
let mut plan = Self::from_parts(logical, access);
plan.projection_selection = projection_selection;
plan
}
#[must_use]
pub(in crate::db) fn into_grouped(self, group: GroupSpec) -> Self {
self.into_grouped_with_having(group, None)
}
#[must_use]
pub(in crate::db) fn into_grouped_with_having(
self,
group: GroupSpec,
having: Option<GroupHavingSpec>,
) -> Self {
let Self {
logical,
access,
projection_selection,
} = self;
let scalar = match logical {
LogicalPlan::Scalar(plan) => plan,
LogicalPlan::Grouped(plan) => plan.scalar,
};
Self {
logical: LogicalPlan::Grouped(GroupPlan {
scalar,
group,
having,
}),
access,
projection_selection,
}
}
#[must_use]
pub(in crate::db) fn access_strategy(&self) -> AccessStrategy<'_, Value> {
self.access.resolve_strategy()
}
}