use std::borrow::Cow;
use crate::column::IntoExpr;
use crate::error::{OrmError, Result};
use crate::exec::Target;
use crate::ir::{AggregateFunction, Expr, SelectItem};
use crate::model::Model;
use crate::query::Builder;
use crate::record::Record;
use crate::relations::existence::{ExistsRequest, ExistsSelect, Refine};
use crate::relations::keys::key_of;
use crate::relations::path::{ParsedPath, RelationPath, RelationPathTo};
use crate::relations::registry;
#[derive(Clone, Debug)]
pub struct AggregateSpec {
kind: AggregateKind,
}
#[derive(Clone, Debug)]
enum AggregateKind {
Count,
Exists,
Function {
function: AggregateFunction,
column: Expr,
name: String,
},
}
impl AggregateSpec {
pub(crate) fn count() -> Self {
AggregateSpec {
kind: AggregateKind::Count,
}
}
pub(crate) fn exists() -> Self {
AggregateSpec {
kind: AggregateKind::Exists,
}
}
pub(crate) fn function(function: AggregateFunction, column: impl IntoExpr) -> Result<Self> {
let column = column.into_expr();
let name = match &column {
Expr::Column(column) => column.name.to_string(),
other => {
return Err(OrmError::invalid(format!(
"a relation aggregate takes a column, not {other:?}"
)));
}
};
Ok(AggregateSpec {
kind: AggregateKind::Function {
function,
column,
name,
},
})
}
fn alias(&self, relation: &str) -> String {
match &self.kind {
AggregateKind::Count => format!("{relation}_count"),
AggregateKind::Exists => format!("{relation}_exists"),
AggregateKind::Function { function, name, .. } => format!(
"{relation}_{}_{name}",
function.sql_name().to_ascii_lowercase()
),
}
}
fn select(&self) -> ExistsSelect {
match &self.kind {
AggregateKind::Count => ExistsSelect::Count,
AggregateKind::Exists => ExistsSelect::One,
AggregateKind::Function {
function, column, ..
} => ExistsSelect::Aggregate {
function: *function,
column: column.clone(),
},
}
}
}
impl<M: Model> Builder<M> {
pub(crate) fn push_aggregate(
self,
text: &str,
spec: &AggregateSpec,
refine: impl FnOnce(u32) -> Refine,
) -> Self {
let path = match ParsedPath::parse(text) {
Ok(path) => path,
Err(error) => return self.with_error(error.to_string()),
};
if path.segments.len() != 1 || path.columns.is_some() {
return self.with_error(format!(
"`{text}`: a relation aggregate takes one relation of {} (optionally `as alias`), not a path or a column selection",
M::meta().name()
));
}
let name = &path.segments[0];
let alias = path.alias.clone().unwrap_or_else(|| spec.alias(name));
let relation = match registry::find::<M>(name) {
Ok(relation) => relation,
Err(error) => return self.with_error(error.to_string()),
};
let depth = self.eager.depth;
let query = match relation.inner.exists_query(ExistsRequest {
depth,
select: spec.select(),
refine: refine(depth + 1),
}) {
Ok(query) => query,
Err(error) => return self.with_error(error.to_string()),
};
match spec.kind {
AggregateKind::Exists => self.select_expr(Expr::Exists(Box::new(query)), alias),
AggregateKind::Count | AggregateKind::Function { .. } => self.select_sub(query, alias),
}
}
pub fn with_count(self, path: impl RelationPath<M>) -> Self {
let text = path.relation_path().into_owned();
self.push_aggregate(&text, &AggregateSpec::count(), |_| Refine::None)
}
pub fn with_count_constrained<T: Model>(
self,
path: impl RelationPathTo<M, T>,
constraint: impl FnOnce(Builder<T>) -> Builder<T>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_aggregate(&text, &AggregateSpec::count(), move |depth| {
let mut empty = Builder::<T>::new();
empty.eager.depth = depth;
Refine::Builder(Box::new(constraint(empty)))
})
}
pub fn with_sum(self, path: impl RelationPath<M>, column: impl IntoExpr) -> Self {
self.with_aggregate(path, column, AggregateFunction::Sum)
}
pub fn with_avg(self, path: impl RelationPath<M>, column: impl IntoExpr) -> Self {
self.with_aggregate(path, column, AggregateFunction::Avg)
}
pub fn with_min(self, path: impl RelationPath<M>, column: impl IntoExpr) -> Self {
self.with_aggregate(path, column, AggregateFunction::Min)
}
pub fn with_max(self, path: impl RelationPath<M>, column: impl IntoExpr) -> Self {
self.with_aggregate(path, column, AggregateFunction::Max)
}
pub fn with_exists(self, path: impl RelationPath<M>) -> Self {
let text = path.relation_path().into_owned();
self.push_aggregate(&text, &AggregateSpec::exists(), |_| Refine::None)
}
pub fn with_aggregate(
self,
path: impl RelationPath<M>,
column: impl IntoExpr,
function: AggregateFunction,
) -> Self {
let text = path.relation_path().into_owned();
match AggregateSpec::function(function, column) {
Ok(spec) => self.push_aggregate(&text, &spec, |_| Refine::None),
Err(error) => self.with_error(error.to_string()),
}
}
}
pub(crate) async fn load_aggregate_on<M: Model>(
target: &Target<'_>,
records: &mut [Record<M>],
paths: &[String],
spec: &AggregateSpec,
) -> Result<()> {
let keys: Vec<M::Key> = records
.iter()
.filter(|record| record.exists())
.map(Record::key)
.collect();
if keys.is_empty() || paths.is_empty() {
return Ok(());
}
let meta = M::meta();
let mut query = Builder::<M>::new()
.without_global_scopes()
.where_key_in(keys)
.select([SelectItem {
expr: Expr::Column(crate::relations::core::qualified(
meta.table(),
meta.key_column(),
)),
alias: Some(Cow::Borrowed("navree_key")),
}]);
for path in paths {
query = query.push_aggregate(path, spec, |_| Refine::None);
}
let rows = target.fetch(&query.into_ir()).await?;
for row in rows {
let values = row.values()?;
let Some(key) = values
.iter()
.find(|(column, _)| column == "navree_key")
.and_then(|(_, value)| key_of(value))
else {
continue;
};
for record in records.iter_mut() {
let record_key = crate::Field::to_value(&record.key());
if key_of(&record_key).as_ref() == Some(&key) {
for (column, value) in &values {
if column != "navree_key" {
record.set_extra(column.clone(), value.clone());
}
}
}
}
}
Ok(())
}