Commit 426b4ff2 authored by Vincent Pelletier's avatar Vincent Pelletier

Modify SimpleQuery API to deprecate "operator" parameter in favor of...

Modify SimpleQuery API to deprecate "operator" parameter in favor of "comparison_operator". Preserve backward compatibility.
Update all callers.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25979 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 46bf014a
...@@ -32,6 +32,7 @@ from Query import Query ...@@ -32,6 +32,7 @@ from Query import Query
from Products.ZSQLCatalog.Interface.IQuery import IQuery from Products.ZSQLCatalog.Interface.IQuery import IQuery
from Interface.Verify import verifyClass from Interface.Verify import verifyClass
from Products.ZSQLCatalog.SQLCatalog import profiler_decorator from Products.ZSQLCatalog.SQLCatalog import profiler_decorator
from zLOG import LOG, WARNING
class SimpleQuery(Query): class SimpleQuery(Query):
""" """
...@@ -39,12 +40,12 @@ class SimpleQuery(Query): ...@@ -39,12 +40,12 @@ class SimpleQuery(Query):
one or more values. one or more values.
""" """
@profiler_decorator @profiler_decorator
def __init__(self, search_key=None, operator='=', group=None, **kw): def __init__(self, search_key=None, comparison_operator='=', group=None, **kw):
""" """
search_key (None, SearchKey instance) search_key (None, SearchKey instance)
If given, the instance of SearchKey which is responsible for column If given, the instance of SearchKey which is responsible for column
map registration and rendering (SQL and SearchText). map registration and rendering (SQL and SearchText).
operator (string) comparison_operator (string)
The comparison operator which will be applied between column and The comparison operator which will be applied between column and
values. values.
See Operator/ComparisonOperator.py for possible values. See Operator/ComparisonOperator.py for possible values.
...@@ -56,6 +57,10 @@ class SimpleQuery(Query): ...@@ -56,6 +57,10 @@ class SimpleQuery(Query):
column name column name
item value item value
one or more values one or more values
Deprecated:
operator (string)
Use comparison_operator instead.
""" """
self.search_key = search_key self.search_key = search_key
if len(kw) != 1: if len(kw) != 1:
...@@ -64,26 +69,32 @@ class SimpleQuery(Query): ...@@ -64,26 +69,32 @@ class SimpleQuery(Query):
# Backward compatibility code (those changes should not be needed when # Backward compatibility code (those changes should not be needed when
# this Query is instanciated by a SearchKey, as operator should be correct # this Query is instanciated by a SearchKey, as operator should be correct
# already). # already).
operator = operator.lower() if len(kw) == 2 and 'operator' in kw:
if operator == 'in': operator = kw.pop('operator')
if comparison_operator not in (None, operator):
LOG('SimpleQuery', WARNING, 'Both "operator" and "comparison_operator" are provided, ignoring "operator".')
else:
comparison_operator = operator
comparison_operator = comparison_operator.lower()
if comparison_operator == 'in':
if isinstance(value, (list, tuple)): if isinstance(value, (list, tuple)):
if len(value) == 0: if len(value) == 0:
raise ValueError, 'Empty lists are not allowed.' raise ValueError, 'Empty lists are not allowed.'
elif len(value) == 1: elif len(value) == 1:
value = value[0] value = value[0]
operator = '=' comparison_operator = '='
else: else:
operator = '=' comparison_operator = '='
elif operator == '=': elif comparison_operator == '=':
if isinstance(value, (list, tuple)): if isinstance(value, (list, tuple)):
if len(value) == 0: if len(value) == 0:
raise ValueError, 'Empty lists are not allowed.' raise ValueError, 'Empty lists are not allowed.'
elif len(value) == 1: elif len(value) == 1:
value = value[0] value = value[0]
else: else:
operator = 'in' comparison_operator = 'in'
self.value = value self.value = value
self.operator = operator self.comparison_operator = comparison_operator
self.group = group self.group = group
@profiler_decorator @profiler_decorator
...@@ -104,7 +115,7 @@ class SimpleQuery(Query): ...@@ -104,7 +115,7 @@ class SimpleQuery(Query):
""" """
Return an instance of OperatorBase class. Return an instance of OperatorBase class.
""" """
return sql_catalog.getComparisonOperator(self.operator) return sql_catalog.getComparisonOperator(self.comparison_operator)
def getSearchKey(self, sql_catalog): def getSearchKey(self, sql_catalog):
""" """
...@@ -121,7 +132,7 @@ class SimpleQuery(Query): ...@@ -121,7 +132,7 @@ class SimpleQuery(Query):
return self.value return self.value
def __repr__(self): def __repr__(self):
return '<%s %r %s %r>' % (self.__class__.__name__, self.getColumn(), self.operator, self.getValue()) return '<%s %r %s %r>' % (self.__class__.__name__, self.getColumn(), self.comparison_operator, self.getValue())
def setGroup(self, group): def setGroup(self, group):
self.group = group self.group = group
......
...@@ -139,8 +139,8 @@ def wholePeriod(search_key, group, column, value_list, exclude=False): ...@@ -139,8 +139,8 @@ def wholePeriod(search_key, group, column, value_list, exclude=False):
append = query_list.append append = query_list.append
for value in value_list: for value in value_list:
first_date, second_date = getPeriodBoundaries(value) first_date, second_date = getPeriodBoundaries(value)
append(ComplexQuery([SimpleQuery(search_key=search_key, operator=first_operator, group=group, **{column: first_date}), append(ComplexQuery([SimpleQuery(search_key=search_key, comparison_operator=first_operator, group=group, **{column: first_date}),
SimpleQuery(search_key=search_key, operator=second_operator, group=group, **{column: second_date})], SimpleQuery(search_key=search_key, comparison_operator=second_operator, group=group, **{column: second_date})],
operator=logical_operator)) operator=logical_operator))
return query_list return query_list
...@@ -156,9 +156,9 @@ def matchExact(search_key, group, column, value_list, comparison_operator, logic ...@@ -156,9 +156,9 @@ def matchExact(search_key, group, column, value_list, comparison_operator, logic
comparison_operator = '=' comparison_operator = '='
value_list = [castDate(x) for x in value_list] value_list = [castDate(x) for x in value_list]
if logical_operator == 'or' and comparison_operator == '=': if logical_operator == 'or' and comparison_operator == '=':
query_list = [SimpleQuery(search_key=search_key, operator='in', group=group, **{column: value_list})] query_list = [SimpleQuery(search_key=search_key, comparison_operator='in', group=group, **{column: value_list})]
else: else:
query_list = [SimpleQuery(search_key=search_key, operator=comparison_operator, group=group, **{column: x}) for x in value_list] query_list = [SimpleQuery(search_key=search_key, comparison_operator=comparison_operator, group=group, **{column: x}) for x in value_list]
return query_list return query_list
def getNextPeriod(value): def getNextPeriod(value):
......
...@@ -58,7 +58,7 @@ class KeywordKey(SearchKey): ...@@ -58,7 +58,7 @@ class KeywordKey(SearchKey):
different_list = [] different_list = []
for value in original_different_list: for value in original_different_list:
if isinstance(value, basestring) and '%' in value: if isinstance(value, basestring) and '%' in value:
result.append(SimpleQuery(search_key=self, group=group, operator='not like', **{column: value})) result.append(SimpleQuery(search_key=self, group=group, comparison_operator='not like', **{column: value}))
else: else:
different_list.append(value) different_list.append(value)
if len(different_list): if len(different_list):
......
...@@ -324,10 +324,10 @@ class SearchKey(object): ...@@ -324,10 +324,10 @@ class SearchKey(object):
append = query_list.append append = query_list.append
if logical_operator == 'or' and '=' in operator_value_dict: if logical_operator == 'or' and '=' in operator_value_dict:
# Special case for equality with an 'or' logical operator: use SQL 'in'. # Special case for equality with an 'or' logical operator: use SQL 'in'.
append(SimpleQuery(search_key=self, operator='in', group=group, **{column: operator_value_dict.pop('=')})) append(SimpleQuery(search_key=self, comparison_operator='in', group=group, **{column: operator_value_dict.pop('=')}))
for comparison_operator, value_list in operator_value_dict.iteritems(): for comparison_operator, value_list in operator_value_dict.iteritems():
for value in value_list: for value in value_list:
append(SimpleQuery(search_key=self, operator=comparison_operator, group=group, **{column: value})) append(SimpleQuery(search_key=self, comparison_operator=comparison_operator, group=group, **{column: value}))
return query_list return query_list
@profiler_decorator @profiler_decorator
......
...@@ -60,7 +60,7 @@ class ReferenceQuery: ...@@ -60,7 +60,7 @@ class ReferenceQuery:
return self.column is not None and \ return self.column is not None and \
other.getColumn() == self.column and \ other.getColumn() == self.column and \
other.getValue() == self.value and \ other.getValue() == self.value and \
other.operator == self.operator other.comparison_operator == self.operator
elif isinstance(other, ComplexQuery): elif isinstance(other, ComplexQuery):
if not (len(other.query_list) == len(self.args) and \ if not (len(other.query_list) == len(self.args) and \
other.logical_operator == self.operator): other.logical_operator == self.operator):
...@@ -136,7 +136,7 @@ class DummyCatalog(SQLCatalog): ...@@ -136,7 +136,7 @@ class DummyCatalog(SQLCatalog):
return '%(table_0)s.uid = %(query_table)s.uid AND %(table_0)s.other_uid = %(table_1)s' % kw return '%(table_0)s.uid = %(query_table)s.uid AND %(table_0)s.other_uid = %(table_1)s' % kw
def scriptableKeyScript(self, value): def scriptableKeyScript(self, value):
return SimpleQuery(operator='=', keyword=value) return SimpleQuery(comparison_operator='=', keyword=value)
class TestSQLCatalog(unittest.TestCase): class TestSQLCatalog(unittest.TestCase):
def setUp(self): def setUp(self):
......
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