diff --git a/crates/storage-pg/src/filter.rs b/crates/storage-pg/src/filter.rs new file mode 100644 index 00000000..f33c6385 --- /dev/null +++ b/crates/storage-pg/src/filter.rs @@ -0,0 +1,65 @@ +// Copyright 2024 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// A filter which can be applied to a query +pub(crate) trait Filter { + /// Generate a condition for the filter + /// + /// # Parameters + /// + /// * `has_joins`: Whether the condition has relationship joined or not + fn generate_condition(&self, has_joins: bool) -> impl sea_query::IntoCondition; +} + +pub(crate) trait StatementExt { + /// Apply the filter to the query + /// + /// The query must NOT have any relationship joined + fn apply_filter(&mut self, filter: F) -> &mut Self; +} + +pub(crate) trait StatementWithJoinsExt { + /// Apply the filter to the query + /// + /// The query MUST have any relationship joined + fn apply_filter_with_joins(&mut self, filter: F) -> &mut Self; +} + +impl StatementWithJoinsExt for sea_query::SelectStatement { + fn apply_filter_with_joins(&mut self, filter: F) -> &mut Self { + let condition = filter.generate_condition(true); + self.cond_where(condition) + } +} + +impl StatementExt for sea_query::SelectStatement { + fn apply_filter(&mut self, filter: F) -> &mut Self { + let condition = filter.generate_condition(false); + self.cond_where(condition) + } +} + +impl StatementExt for sea_query::UpdateStatement { + fn apply_filter(&mut self, filter: F) -> &mut Self { + let condition = filter.generate_condition(false); + self.cond_where(condition) + } +} + +impl StatementExt for sea_query::DeleteStatement { + fn apply_filter(&mut self, filter: F) -> &mut Self { + let condition = filter.generate_condition(false); + self.cond_where(condition) + } +} diff --git a/crates/storage-pg/src/lib.rs b/crates/storage-pg/src/lib.rs index 0294ba79..94313d85 100644 --- a/crates/storage-pg/src/lib.rs +++ b/crates/storage-pg/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021-2023 The Matrix.org Foundation C.I.C. +// Copyright 2021-2024 The Matrix.org Foundation C.I.C. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -177,6 +177,7 @@ pub mod upstream_oauth2; pub mod user; mod errors; +pub(crate) mod filter; pub(crate) mod iden; pub(crate) mod pagination; pub(crate) mod repository;