pub struct AggregateOperation<M: Model, E: QueryEngine> { /* private fields */ }Expand description
Aggregate operation builder.
§Engine ownership
The builder stores an Option<E> rather than the engine directly
so existing unit tests that construct an AggregateOperation just
to exercise SQL emission (AggregateOperation::<Model, MockEngine>::new()) keep working without a real engine.
Production code always goes through AggregateOperation::with_engine
(what the generated Client<E>::aggregate() accessor calls), and
Self::exec refuses to run when the engine slot is empty.
Implementations§
Source§impl<M: Model, E: QueryEngine> AggregateOperation<M, E>
impl<M: Model, E: QueryEngine> AggregateOperation<M, E>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new aggregate operation without an engine.
Useful for unit tests that only exercise Self::build_sql.
Self::exec will refuse to run on a builder created this way.
Sourcepub fn with_engine(engine: E) -> Self
pub fn with_engine(engine: E) -> Self
Create a new aggregate operation bound to a concrete engine.
This is what the generated Client<E>::aggregate() accessor
calls.
Sourcepub fn count_column(self, column: impl Into<String>) -> Self
pub fn count_column(self, column: impl Into<String>) -> Self
Add a count of non-null values in a column.
Sourcepub fn count_distinct(self, column: impl Into<String>) -> Self
pub fn count_distinct(self, column: impl Into<String>) -> Self
Add a count of distinct values in a column.
Sourcepub fn build_sql(&self, dialect: &dyn SqlDialect) -> (String, Vec<FilterValue>)
pub fn build_sql(&self, dialect: &dyn SqlDialect) -> (String, Vec<FilterValue>)
Build the SQL for this operation.
Sourcepub async fn exec(self) -> QueryResult<AggregateResult>
pub async fn exec(self) -> QueryResult<AggregateResult>
Execute the aggregate operation.
Routes the single-row aggregate result through
crate::traits::QueryEngine::aggregate_query and folds the
column→value map into an AggregateResult. Returns an empty
result (all fields None/empty) if the query yields zero rows
— aggregates on empty tables do this on Postgres/MySQL/SQLite.
Errors with QueryError::internal if the builder was
constructed via Self::new without an engine.