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
use expression::count::*;
use super::SelectDsl;

/// Adds a simple `count` function to queries. Automatically implemented for all
/// types which implement `SelectDsl`.
///
/// # Example
///
/// ```rust
/// # #[macro_use] extern crate diesel;
/// # include!("src/doctest_setup.rs");
/// #
/// # table! {
/// #     users {
/// #         id -> Serial,
/// #         name -> VarChar,
/// #     }
/// # }
/// #
/// # fn main() {
/// #     use self::users::dsl::*;
/// #     let connection = establish_connection();
/// let count = users.count().first(&connection);
/// assert_eq!(Ok(2), count);
/// # }
/// ```
pub trait CountDsl: SelectDsl<CountStar> + Sized {
    /// Get the count of a query. This is equivalent to `.select(count_star())`
    fn count(self) -> <Self as SelectDsl<CountStar>>::Output {
        self.select(count_star())
    }
}

impl<T: SelectDsl<CountStar>> CountDsl for T {}