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
guessComparisonOperator = self._guessComparisonOperator
preprocessValue = self._preprocessValue
for value in search_value:
is_dict = isinstance(value, dict)
if is_dict:
base_value = value['query']
else:
base_value = value
if isinstance(base_value, basestring):
if get_operator_from_value: if get_operator_from_value:
parsed = True parsed = True
for value in search_value: operator, base_value = getComparisonOperator(base_value)
if isinstance(value, dict):
operator, value['query'] = self._getComparisonOperator(
value['query'])
else: else:
operator, value = self._getComparisonOperator(value) operator = guessComparisonOperator(base_value)
operator_value_dict.setdefault(operator, []).append( elif base_value is None:
self._preprocessValue(value, operator)) operator = 'is'
else: else:
for value in search_value: # XXX: comparison operator is hardcoded for non-strings.
if isinstance(value, dict): operator = '='
operator = self._guessComparisonOperator(value['query']) if is_dict:
value['query'] = base_value
else: else:
operator = self._guessComparisonOperator(value) value = base_value
operator_value_dict.setdefault(operator, []).append( operator_value_dict.setdefault(operator, []).append(
self._preprocessValue(value, operator)) preprocessValue(value, operator),
else: )
# XXX: comparison operator is hardcoded for non-strings.
operator_value_dict['='] = search_value
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