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