mas_storage_pg/
filter.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7/// A filter which can be applied to a query
8pub(crate) trait Filter {
9    /// Generate a condition for the filter
10    ///
11    /// # Parameters
12    ///
13    /// * `has_joins`: Whether the condition has relationship joined or not
14    fn generate_condition(&self, has_joins: bool) -> impl sea_query::IntoCondition;
15}
16
17pub(crate) trait StatementExt {
18    /// Apply the filter to the query
19    ///
20    /// The query must NOT have any relationship joined
21    fn apply_filter<F: Filter>(&mut self, filter: F) -> &mut Self;
22}
23
24pub(crate) trait StatementWithJoinsExt {
25    /// Apply the filter to the query
26    ///
27    /// The query MUST have any relationship joined
28    fn apply_filter_with_joins<F: Filter>(&mut self, filter: F) -> &mut Self;
29}
30
31impl StatementWithJoinsExt for sea_query::SelectStatement {
32    fn apply_filter_with_joins<F: Filter>(&mut self, filter: F) -> &mut Self {
33        let condition = filter.generate_condition(true);
34        self.cond_where(condition)
35    }
36}
37
38impl StatementExt for sea_query::SelectStatement {
39    fn apply_filter<F: Filter>(&mut self, filter: F) -> &mut Self {
40        let condition = filter.generate_condition(false);
41        self.cond_where(condition)
42    }
43}
44
45impl StatementExt for sea_query::UpdateStatement {
46    fn apply_filter<F: Filter>(&mut self, filter: F) -> &mut Self {
47        let condition = filter.generate_condition(false);
48        self.cond_where(condition)
49    }
50}
51
52impl StatementExt for sea_query::DeleteStatement {
53    fn apply_filter<F: Filter>(&mut self, filter: F) -> &mut Self {
54        let condition = filter.generate_condition(false);
55        self.cond_where(condition)
56    }
57}