diff --git a/common/database_helpers.py b/common/database_helpers.py index f0250dba..fe304043 100644 --- a/common/database_helpers.py +++ b/common/database_helpers.py @@ -195,6 +195,32 @@ def get_query_filter(filter): raise BadFilterError(f" Bad filter: {filter}") +class FilterOrderHandler(object): + """ + The FilterOrderHandler takes in filters, sorts them according to the order of operations, then applies them. + """ + def __init__(self): + self.filters = [] + + def add_filter(self, filter): + self.filters.append(filter) + + def sort_filters(self): + """ + Sorts the filters according to the order of operations + """ + self.filters.sort(key=lambda x: x.precedence) + + def apply_filters(self, query): + """ + Given a query apply the filters the handler has in the correct order. + :param query: The query to have filters applied to + """ + self.sort_filters() + for filter in self.filters: + filter.apply_filter(query) + + def insert_row_into_table(table, row): """ Insert the given row into its table