Commit f57a4789 authored by Vincent Pelletier's avatar Vincent Pelletier

Replace asSearchTextExpression implementations with _asSearchTextExpression...

Replace asSearchTextExpression implementations with _asSearchTextExpression and a default asSearchTextExpression wrapping _asSearchTextExpression call.
This fixes unneeded parentheses in generated search text expressions.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27662 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2fd15edd
...@@ -110,6 +110,12 @@ class AutoQuery(Query): ...@@ -110,6 +110,12 @@ class AutoQuery(Query):
query.setTableAliasList(self.table_alias_list) query.setTableAliasList(self.table_alias_list)
self.wrapped_query = query self.wrapped_query = query
@profiler_decorator
def _asSearchTextExpression(self, sql_catalog, column=None):
if self.wrapped_query is None:
self._createWrappedQuery(sql_catalog)
return self.wrapped_query._asSearchTextExpression(sql_catalog, column=column)
@profiler_decorator @profiler_decorator
def asSearchTextExpression(self, sql_catalog, column=None): def asSearchTextExpression(self, sql_catalog, column=None):
if self.wrapped_query is None: if self.wrapped_query is None:
......
...@@ -93,29 +93,48 @@ class ComplexQuery(Query): ...@@ -93,29 +93,48 @@ class ComplexQuery(Query):
self.query_list = new_query_list self.query_list = new_query_list
@profiler_decorator @profiler_decorator
def asSearchTextExpression(self, sql_catalog, column=None): def _asSearchTextExpression(self, sql_catalog, column=None):
if column in (None, ''): if column in (None, ''):
query_column = column query_column = column
else: else:
query_column = '' query_column = ''
search_text_list = [y for y in [x.asSearchTextExpression(sql_catalog, column=query_column) for x in self.query_list] if y is not None] search_text_list = []
composition_list = []
for query in self.query_list:
is_composed, search_text = query._asSearchTextExpression(sql_catalog, column=query_column)
if search_text is not None:
search_text_list.append(search_text)
composition_list.append(is_composed)
self_is_composed = False
if len(search_text_list) == 0: if len(search_text_list) == 0:
result = '' result = ''
else: else:
if self.logical_operator in logical_operator_search_text_dict: if self.logical_operator in logical_operator_search_text_dict:
if len(search_text_list) == 1: if len(search_text_list) == 1:
result = search_text_list[0] result = search_text_list[0]
self_is_composed = composition_list[0]
else: else:
self_is_composed = True
logical_operator = ' %s ' % (logical_operator_search_text_dict[self.logical_operator], ) logical_operator = ' %s ' % (logical_operator_search_text_dict[self.logical_operator], )
result = '(%s)' % (logical_operator.join(search_text_list), ) parenthesed_search_text_list = []
append = parenthesed_search_text_list.append
for is_composed, search_text in zip(composition_list, search_text_list):
if is_composed:
append('(%s)' % (search_text, ))
else:
append(search_text)
result = logical_operator.join(parenthesed_search_text_list)
elif self.logical_operator == 'not': elif self.logical_operator == 'not':
assert len(search_text_list) == 1 assert len(search_text_list) == 1
result = '(NOT %s)' % (search_text_list[0], ) result = 'NOT %s' % (search_text_list[0], )
else: else:
raise ValueError, 'Unknown operator %r' % (self.logical_operator, ) raise ValueError, 'Unknown operator %r' % (self.logical_operator, )
if column not in (None, ''): if column not in (None, ''):
if self_is_composed:
result = '(%s)' % (result, )
self_is_composed = False
result = '%s:%s' % (column, result) result = '%s:%s' % (column, result)
return result return self_is_composed, result
@profiler_decorator @profiler_decorator
def asSQLExpression(self, sql_catalog, column_map, only_group_columns): def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
......
...@@ -46,12 +46,15 @@ class Query(object): ...@@ -46,12 +46,15 @@ class Query(object):
""" """
raise TypeError, 'A %s cannot be rendered as an SQL expression.' % (self.__class__.__name__, ) raise TypeError, 'A %s cannot be rendered as an SQL expression.' % (self.__class__.__name__, )
def asSearchTextExpression(self, sql_catalog, column=None): def _asSearchTextExpression(self, sql_catalog, column=None):
""" """
To enable Search Text rendering, overload this method in a subclass. To enable Search Text rendering, overload this method in a subclass.
""" """
raise TypeError, 'A %s cannot be rendered as a SearchText expression.' % (self.__class__.__name__, ) raise TypeError, 'A %s cannot be rendered as a SearchText expression.' % (self.__class__.__name__, )
def asSearchTextExpression(self, sql_catalog, column=None):
return self._asSearchTextExpression(sql_catalog, column=column)[1]
def registerColumnMap(self, sql_catalog, column_map): def registerColumnMap(self, sql_catalog, column_map):
""" """
This method must always be overloaded by subclasses. This method must always be overloaded by subclasses.
......
...@@ -65,14 +65,14 @@ class RelatedQuery(Query): ...@@ -65,14 +65,14 @@ class RelatedQuery(Query):
self.table_alias_list = table_alias_list self.table_alias_list = table_alias_list
@profiler_decorator @profiler_decorator
def asSearchTextExpression(self, sql_catalog, column=None): def _asSearchTextExpression(self, sql_catalog, column=None):
assert column is None assert column is None
join_condition = self.join_condition join_condition = self.join_condition
if join_condition is None: if join_condition is None:
result = None result = None
else: else:
result = join_condition.asSearchTextExpression(sql_catalog, column=self.search_key.getColumn()) result = join_condition.asSearchTextExpression(sql_catalog, column=self.search_key.getColumn())
return result return False, result
@profiler_decorator @profiler_decorator
def asSQLExpression(self, sql_catalog, column_map, only_group_columns): def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
......
...@@ -49,8 +49,8 @@ class SQLQuery(Query): ...@@ -49,8 +49,8 @@ class SQLQuery(Query):
assert len(payload) assert len(payload)
self.payload = '(' + payload + ')' self.payload = '(' + payload + ')'
def asSearchTextExpression(self, sql_catalog): def _asSearchTextExpression(self, sql_catalog, column=None):
return None return False, None
def asSQLExpression(self, sql_catalog, column_map, only_group_columns): def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
return SQLExpression(self, where_expression=self.payload) return SQLExpression(self, where_expression=self.payload)
......
...@@ -95,8 +95,8 @@ class SimpleQuery(Query): ...@@ -95,8 +95,8 @@ class SimpleQuery(Query):
self.group = group self.group = group
@profiler_decorator @profiler_decorator
def asSearchTextExpression(self, sql_catalog, column=None): def _asSearchTextExpression(self, sql_catalog, column=None):
return self.getSearchKey(sql_catalog).buildSearchTextExpression(self.getOperator(sql_catalog), self.getValue(), column=column) return False, self.getSearchKey(sql_catalog).buildSearchTextExpression(self.getOperator(sql_catalog), self.getValue(), column=column)
@profiler_decorator @profiler_decorator
def asSQLExpression(self, sql_catalog, column_map, only_group_columns): def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
......
...@@ -47,6 +47,15 @@ class IQuery(Interface): ...@@ -47,6 +47,15 @@ class IQuery(Interface):
format. format.
""" """
def _asSearchTextExpression(sql_catalog, column=None):
"""
Same as asSearchTextExpression, but also returns a boolean telling
caller if returned value must be parenthesed.
Returns a 2-tuple:
- a boolean (True if returned string must be parenthesed)
- a string (a SearchText expression)
"""
def asSearchTextExpression(sql_catalog, column=None): def asSearchTextExpression(sql_catalog, column=None):
""" """
Render a query in a user-oriented SearchText. Render a query in a user-oriented SearchText.
......
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