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