diesel/pg/expression/extensions/
only_dsl.rs

1use crate::query_builder::Only;
2use crate::Table;
3
4/// The `only` method
5///
6/// This is only implemented for the Postgres backend.
7/// The `ONLY` clause is used to select only from one table and not any inherited ones.
8///
9/// Calling this function on a table (`mytable.only()`) will result in the SQL `ONLY mytable`.
10/// `mytable.only()` can be used just like any table in diesel since it implements
11/// [Table](crate::Table).
12///
13/// Example:
14///
15/// ```rust
16/// # include!("../../../doctest_setup.rs");
17/// # use schema::{posts, users};
18/// # use diesel::dsl::*;
19/// # fn main() {
20/// # let connection = &mut establish_connection();
21/// let n_sers_in_main_table = users::table
22///     .only()
23///     .select(count(users::id))
24///     .first::<i64>(connection);
25/// # }
26/// ```
27/// Selects the number of entries in the `users` table excluding any rows found in inherited
28/// tables.
29///
30/// It can also be used in inner joins:
31///
32/// ```rust
33/// # include!("../../../doctest_setup.rs");
34/// # use schema::{posts, users};
35/// # use diesel::dsl::*;
36/// # fn main() {
37/// # let connection = &mut establish_connection();
38/// # let _ =
39/// users::table
40///     .inner_join(posts::table.only())
41///     .select((users::name, posts::title))
42///     .load::<(String, String)>(connection);
43/// # }
44/// ```
45/// That query excludes any posts that reside in any inherited table.
46///
47pub trait OnlyDsl: Table {
48    /// See the trait-level docs.
49    fn only(self) -> Only<Self> {
50        Only { source: self }
51    }
52}
53
54impl<T: Table> OnlyDsl for T {}