diesel/query_dsl/
filter_dsl.rs

1use crate::dsl::{Filter, OrFilter};
2use crate::expression_methods::*;
3use crate::query_source::*;
4
5/// The `filter` method
6///
7/// This trait should not be relied on directly by most apps. Its behavior is
8/// provided by [`QueryDsl`]. However, you may need a where clause on this trait
9/// to call `filter` from generic code.
10///
11/// [`QueryDsl`]: crate::QueryDsl
12pub trait FilterDsl<Predicate> {
13    /// The type returned by `.filter`.
14    type Output;
15
16    /// See the trait documentation.
17    fn filter(self, predicate: Predicate) -> Self::Output;
18}
19
20impl<T, Predicate> FilterDsl<Predicate> for T
21where
22    T: Table,
23    T::Query: FilterDsl<Predicate>,
24{
25    type Output = Filter<T::Query, Predicate>;
26
27    fn filter(self, predicate: Predicate) -> Self::Output {
28        self.as_query().filter(predicate)
29    }
30}
31
32/// The `find` method
33///
34/// This trait should not be relied on directly by most apps. Its behavior is
35/// provided by [`QueryDsl`]. However, you may need a where clause on this trait
36/// to call `find` from generic code.
37///
38/// [`QueryDsl`]: crate::QueryDsl
39pub trait FindDsl<PK> {
40    /// The type returned by `.find`.
41    type Output;
42
43    /// See the trait documentation.
44    fn find(self, id: PK) -> Self::Output;
45}
46
47impl<T, PK> FindDsl<PK> for T
48where
49    T: Table + FilterDsl<<<T as Table>::PrimaryKey as EqAll<PK>>::Output>,
50    T::PrimaryKey: EqAll<PK>,
51{
52    type Output = Filter<Self, <T::PrimaryKey as EqAll<PK>>::Output>;
53
54    fn find(self, id: PK) -> Self::Output {
55        let primary_key = self.primary_key();
56        self.filter(primary_key.eq_all(id))
57    }
58}
59
60/// The `or_filter` method
61///
62/// This trait should not be relied on directly by most apps. Its behavior is
63/// provided by [`QueryDsl`]. However, you may need a where clause on this trait
64/// to call `or_filter` from generic code.
65///
66/// [`QueryDsl`]: crate::QueryDsl
67pub trait OrFilterDsl<Predicate> {
68    /// The type returned by `.filter`.
69    type Output;
70
71    /// See the trait documentation.
72    fn or_filter(self, predicate: Predicate) -> Self::Output;
73}
74
75impl<T, Predicate> OrFilterDsl<Predicate> for T
76where
77    T: Table,
78    T::Query: OrFilterDsl<Predicate>,
79{
80    type Output = OrFilter<T::Query, Predicate>;
81
82    fn or_filter(self, predicate: Predicate) -> Self::Output {
83        self.as_query().or_filter(predicate)
84    }
85}