diesel/expression/functions/
aggregate_ordering.rs

1use self::private::SqlOrdAggregate;
2use crate::expression::functions::define_sql_function;
3
4define_sql_function! {
5    /// Represents a SQL `MAX` function. This function can only take types which are
6    /// ordered.
7    ///
8    /// # Examples
9    ///
10    /// ```rust
11    /// # include!("../../doctest_setup.rs");
12    /// # use diesel::dsl::*;
13    /// #
14    /// # fn main() {
15    /// #     use schema::animals::dsl::*;
16    /// #     let connection = &mut establish_connection();
17    /// assert_eq!(Ok(Some(8)), animals.select(max(legs)).first(connection));
18    /// # }
19    #[aggregate]
20    fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
21}
22
23define_sql_function! {
24    /// Represents a SQL `MIN` function. This function can only take types which are
25    /// ordered.
26    ///
27    /// # Examples
28    ///
29    /// ```rust
30    /// # include!("../../doctest_setup.rs");
31    /// # use diesel::dsl::*;
32    /// #
33    /// # fn main() {
34    /// #     use schema::animals::dsl::*;
35    /// #     let connection = &mut establish_connection();
36    /// assert_eq!(Ok(Some(4)), animals.select(min(legs)).first(connection));
37    /// # }
38    #[aggregate]
39    fn min<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
40}
41
42mod private {
43    use crate::sql_types::{IntoNullable, SingleValue, SqlOrd, SqlType};
44    pub trait SqlOrdAggregate: SingleValue {
45        type Ret: SqlType + SingleValue;
46    }
47
48    impl<T> SqlOrdAggregate for T
49    where
50        T: SqlOrd + IntoNullable + SingleValue,
51        T::Nullable: SqlType + SingleValue,
52    {
53        type Ret = T::Nullable;
54    }
55}