1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
use backend::Backend; use expression::Expression; use query_builder::*; use result::QueryResult; use types::{SqlOrd, HasSqlType, IntoNullable}; macro_rules! ord_function { ($fn_name:ident, $type_name:ident, $operator:expr, $docs:expr) => { #[doc=$docs] pub fn $fn_name<ST, T>(t: T) -> $type_name<T> where ST: SqlOrd, T: Expression<SqlType=ST>, { $type_name { target: t, } } #[derive(Debug, Clone, Copy)] pub struct $type_name<T> { target: T, } impl<T: Expression> Expression for $type_name<T> where T::SqlType: IntoNullable, { type SqlType = <T::SqlType as IntoNullable>::Nullable; } impl<T, DB> QueryFragment<DB> for $type_name<T> where T: Expression + QueryFragment<DB>, DB: Backend + HasSqlType<T::SqlType>, { fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> { out.push_sql(concat!($operator, "(")); self.target.walk_ast(out.reborrow())?; out.push_sql(")"); Ok(()) } } impl_query_id!($type_name<T>); impl_selectable_expression!($type_name<T>); } } ord_function!(max, Max, "MAX", "Represents a SQL `MAX` function. This function can only take types which are ordered. # Examples ```rust # #[macro_use] extern crate diesel; # include!(\"src/doctest_setup.rs\"); # use diesel::expression::dsl::*; # # table! { # users { # id -> Integer, # name -> VarChar, # } # } # # fn main() { # use self::animals::dsl::*; # let connection = establish_connection(); assert_eq!(Ok(Some(8)), animals.select(max(legs)).first(&connection)); # } "); ord_function!(min, Min, "MIN", "Represents a SQL `MIN` function. This function can only take types which are ordered. # Examples ```rust # #[macro_use] extern crate diesel; # include!(\"src/doctest_setup.rs\"); # use diesel::expression::dsl::*; # # table! { # users { # id -> Integer, # name -> VarChar, # } # } # # fn main() { # use self::animals::dsl::*; # let connection = establish_connection(); assert_eq!(Ok(Some(4)), animals.select(min(legs)).first(&connection)); # } ");