From 630f7f70bd25dad95b85bccce725b9d1062c82b4 Mon Sep 17 00:00:00 2001 From: joamag Date: Wed, 3 Apr 2019 15:17:32 +0100 Subject: [PATCH] Initial support for the find operator --- src/quorum/model.py | 30 +++++++++++++++++++++--------- src/quorum/util.py | 2 ++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/quorum/model.py b/src/quorum/model.py index 2e5434eb..561495c3 100644 --- a/src/quorum/model.py +++ b/src/quorum/model.py @@ -1426,20 +1426,28 @@ def default(cls): return default @classmethod - def filter_merge(cls, name, filter, kwargs): + def filter_merge(cls, name, filter, kwargs, operator = None): # retrieves a possible previous filter defined for the - # provided name in case it does exists must concatenate - # that previous value in an and statement + # provided name in case it does exist must concatenate + # that previous value in a join statement according to + # the currently defined operator filter_p = kwargs.get(name, None) - if filter_p: + if filter_p or not operator == None: + # defaults the operator for the join of the names to the + # value and then ensures that the value of the operator + # is within a valid range of values + operator = operator or "$and" + util.verify(operator in ("$and", "$or")) + # retrieves the and references for the current arguments # and appends the two filter values (current and previous) # then deletes the current name reference in the arguments # and updates the name value to the and value - filter_a = kwargs.get("$and", []) - filter = filter_a + [{name : filter}, {name : filter_p}] - del kwargs[name] - name = "$and" + filter_a = kwargs.get(operator, []) + if filter_p: filter = filter_a + [{name : filter}, {name : filter_p}] + else: filter = filter_a + [{name : filter}] + if name in kwargs: del kwargs[name] + name = operator # sets the currently defined filter structures in the keyword # based arguments map for the currently defined name @@ -1769,6 +1777,10 @@ def _find_d(cls, kwargs): # meant to be done on this method if not "find_d" in kwargs: return + # tries to retrieve the value of the operator that is going + # to be used to "join" the multiple find parts (find values) + find_o = kwargs.pop("find_o", None) + # retrieves the find definition into a local variable, then # removes the find definition from the named arguments map # so that it's not going to be erroneously used by the @@ -1828,7 +1840,7 @@ def _find_d(cls, kwargs): # current set of filters for the provided (keyword) arguments find_v = {operator : value} if operator else value if insensitive: find_v["$options"] = "-i" - cls.filter_merge(name, find_v, kwargs) + cls.filter_merge(name, find_v, kwargs, operator = find_o) @classmethod def _bases(cls, subclass = None): diff --git a/src/quorum/util.py b/src/quorum/util.py index 4d9d1135..15dd51dd 100644 --- a/src/quorum/util.py +++ b/src/quorum/util.py @@ -116,6 +116,7 @@ def to_sort(sort_s): "filter_def" : "find_d", "filter_string" : "find_s", "filter_name" : "find_n", + "filter_operator" : "find_o", "insensitive" : "find_i", "order" : "sort", "offset" : "skip", @@ -133,6 +134,7 @@ def to_sort(sort_s): find_i = bool, find_t = legacy.UNICODE, find_n = legacy.UNICODE, + find_o = legacy.UNICODE, sort = to_sort, meta = bool, fields = list