Commit 32a30fb1 authored by Vincent Pelletier's avatar Vincent Pelletier Committed by Eteri

ZSQLCatalog: Lift item type limitation

Allows searching for, for example:
  some_relation_uid=[123, None]
without having to resort to:
  ComplexQuery(
    SimpleQuery(some_relation_uid=123),
    SimpleQuery(some_relation_uid=None),
    logical_operator='OR',
  )
parent 6a4fb4c0
...@@ -265,57 +265,39 @@ class SearchKey(object): ...@@ -265,57 +265,39 @@ class SearchKey(object):
else: else:
logical_operator = value_operator logical_operator = value_operator
search_value = actual_value search_value = actual_value
# Cast to list if not isinstance(search_value, list_type_list):
if isinstance(search_value, list_type_list):
# Check list content (not empty, homogenous)
# Note: neither is strictly required, but it helps spotting easy mistakes.
klass_set = {
int if x.__class__ in numeric_type_set else x.__class__
for x in search_value
}
try:
reference_class, = klass_set
except ValueError:
if search_value:
raise TypeError(
'List elements must be of the same class: %r' % (search_value, )
)
raise ValueError(
'Value cannot be an empty list/tuple: %r' % (search_value, )
)
else:
assert logical_operator is None
if isinstance(search_value, dict):
reference_class = search_value['query'].__class__
else:
reference_class = search_value.__class__
search_value = [search_value] search_value = [search_value]
if logical_operator is None: if logical_operator is None:
logical_operator = default_logical_operator logical_operator = default_logical_operator
operator_value_dict = {} operator_value_dict = {}
if comparison_operator is None: if comparison_operator is None:
if issubclass(reference_class, basestring): getComparisonOperator = self._getComparisonOperator
if get_operator_from_value: guessComparisonOperator = self._guessComparisonOperator
parsed = True preprocessValue = self._preprocessValue
for value in search_value: for value in search_value:
if isinstance(value, dict): is_dict = isinstance(value, dict)
operator, value['query'] = self._getComparisonOperator( if is_dict:
value['query']) base_value = value['query']
else:
operator, value = self._getComparisonOperator(value)
operator_value_dict.setdefault(operator, []).append(
self._preprocessValue(value, operator))
else: else:
for value in search_value: base_value = value
if isinstance(value, dict): if isinstance(base_value, basestring):
operator = self._guessComparisonOperator(value['query']) if get_operator_from_value:
else: parsed = True
operator = self._guessComparisonOperator(value) operator, base_value = getComparisonOperator(base_value)
operator_value_dict.setdefault(operator, []).append( else:
self._preprocessValue(value, operator)) operator = guessComparisonOperator(base_value)
else: elif base_value is None:
# XXX: comparison operator is hardcoded for non-strings. operator = 'is'
operator_value_dict['='] = search_value else:
# XXX: comparison operator is hardcoded for non-strings.
operator = '='
if is_dict:
value['query'] = base_value
else:
value = base_value
operator_value_dict.setdefault(operator, []).append(
preprocessValue(value, operator),
)
elif isinstance(comparison_operator, (tuple, list)): elif isinstance(comparison_operator, (tuple, list)):
assert len(comparison_operator) == len(search_value) assert len(comparison_operator) == len(search_value)
for operator, value in zip(comparison_operator, search_value): for operator, value in zip(comparison_operator, search_value):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment