diesel/pg/query_builder/
distinct_on.rs

1use expression::SelectableExpression;
2use pg::Pg;
3use query_builder::{AstPass, QueryFragment, SelectQuery, SelectStatement};
4use query_dsl::methods::DistinctOnDsl;
5use result::QueryResult;
6
7/// Represents `DISTINCT ON (...)`
8#[derive(Debug, Clone, Copy, QueryId)]
9pub struct DistinctOnClause<T>(pub(crate) T);
10
11impl<T> QueryFragment<Pg> for DistinctOnClause<T>
12where
13    T: QueryFragment<Pg>,
14{
15    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
16        out.push_sql("DISTINCT ON (");
17        self.0.walk_ast(out.reborrow())?;
18        out.push_sql(")");
19        Ok(())
20    }
21}
22
23impl<ST, F, S, D, W, O, L, Of, G, Selection> DistinctOnDsl<Selection>
24    for SelectStatement<F, S, D, W, O, L, Of, G>
25where
26    Selection: SelectableExpression<F>,
27    Self: SelectQuery<SqlType = ST>,
28    SelectStatement<F, S, DistinctOnClause<Selection>, W, O, L, Of, G>: SelectQuery<SqlType = ST>,
29{
30    type Output = SelectStatement<F, S, DistinctOnClause<Selection>, W, O, L, Of, G>;
31
32    fn distinct_on(self, selection: Selection) -> Self::Output {
33        SelectStatement::new(
34            self.select,
35            self.from,
36            DistinctOnClause(selection),
37            self.where_clause,
38            self.order,
39            self.limit,
40            self.offset,
41            self.group_by,
42            self.locking,
43        )
44    }
45}