Commit 20ce0e42 authored by Vincent Pelletier's avatar Vincent Pelletier

Fix providing a sort direction for a full-text column.

This changes SQLExpression API, but this should not be a problem, since
this API is internal to ZSQLCatalog.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@37338 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e3776a22
...@@ -141,11 +141,8 @@ class EntireQuery(object): ...@@ -141,11 +141,8 @@ class EntireQuery(object):
LOG('SQLCatalog', 100, 'Order by %r ignored: it could not be mapped to a known column.' % (order_by, )) LOG('SQLCatalog', 100, 'Order by %r ignored: it could not be mapped to a known column.' % (order_by, ))
rendered = None rendered = None
if rendered is not None: if rendered is not None:
if len(order_by) > 1: append((rendered, ) + tuple(order_by[1:]) + (
if len(order_by) > 2 and order_by[2] not in (None, ''): None, ) * (3 - len(order_by)))
rendered = 'CAST(%s AS %s)' % (rendered, order_by[2])
rendered = '%s %s' % (rendered, order_by[1])
append(rendered)
self.order_by_list = new_order_by_list self.order_by_list = new_order_by_list
# generate SQLExpression from query # generate SQLExpression from query
sql_expression_list = [self.query.asSQLExpression(sql_catalog, column_map, only_group_columns)] sql_expression_list = [self.query.asSQLExpression(sql_catalog, column_map, only_group_columns)]
......
...@@ -238,9 +238,17 @@ class SQLExpression(object): ...@@ -238,9 +238,17 @@ class SQLExpression(object):
Returns a rendered "order by" expression. See getOrderByList. Returns a rendered "order by" expression. See getOrderByList.
""" """
result = []
append = result.append
order_by_dict = self._getOrderByDict() order_by_dict = self._getOrderByDict()
return SQL_LIST_SEPARATOR.join(conflictSafeGet(order_by_dict, x, str(x)) \ for (column, direction, cast) in self.getOrderByList():
for x in self.getOrderByList()) expression = conflictSafeGet(order_by_dict, column, str(column))
if cast not in (None, ''):
expression = 'CAST(%s AS %s)' % (expression, cast)
if direction is not None:
expression = '%s %s' % (expression, direction)
append(expression)
return SQL_LIST_SEPARATOR.join(result)
@profiler_decorator @profiler_decorator
def getWhereExpression(self): def getWhereExpression(self):
......
...@@ -76,8 +76,11 @@ class ISQLExpression(Interface): ...@@ -76,8 +76,11 @@ class ISQLExpression(Interface):
The Query instance which called this constructor. The Query instance which called this constructor.
table_alias_dict (dict, key: string, value: string) table_alias_dict (dict, key: string, value: string)
Table alias dict as returned by ColumnMap.getTableAliasDict() . Table alias dict as returned by ColumnMap.getTableAliasDict() .
order_by_list (list of strings) order_by_list (list of 3-tuples)
List of result ordering, pre-rendered. Result ordering.
- column (rendered)
- direction (string or None)
- cast (string or None)
order_by_dict (dict, key: string, value: string) order_by_dict (dict, key: string, value: string)
Column rendering replacement specific to order_by. Column rendering replacement specific to order_by.
group_by_list (list of strings) group_by_list (list of strings)
......
...@@ -583,7 +583,23 @@ class TestSQLCatalog(unittest.TestCase): ...@@ -583,7 +583,23 @@ class TestSQLCatalog(unittest.TestCase):
# order_by_list on fulltext column, resulting "ORDER BY" must be non-empty. # order_by_list on fulltext column, resulting "ORDER BY" must be non-empty.
sql_expression = self.asSQLExpression({'fulltext': 'foo', sql_expression = self.asSQLExpression({'fulltext': 'foo',
'order_by_list': [('fulltext', ), ]}) 'order_by_list': [('fulltext', ), ]})
self.assertNotEqual(sql_expression.getOrderByExpression(), '') order_by_expression = sql_expression.getOrderByExpression()
self.assertNotEqual(order_by_expression, '')
# ... and must sort by relevance
self.assertTrue('MATCH' in order_by_expression, order_by_expression)
# ordering on fulltext column with sort order specified must preserve
# sorting by relevance.
for direction in ('ASC', 'DESC'):
sql_expression = self.asSQLExpression({'fulltext': 'foo',
'order_by_list': [('fulltext', direction), ]})
order_by_expression = sql_expression.getOrderByExpression()
self.assertTrue('MATCH' in order_by_expression, (order_by_expression, direction))
# Providing a None cast should work too
for direction in ('ASC', 'DESC'):
sql_expression = self.asSQLExpression({'fulltext': 'foo',
'order_by_list': [('fulltext', direction, None), ]})
order_by_expression = sql_expression.getOrderByExpression()
self.assertTrue('MATCH' in order_by_expression, (order_by_expression, direction))
##return catalog(title=Query(title='a', operator='not')) ##return catalog(title=Query(title='a', operator='not'))
#return catalog(title={'query': 'a', 'operator': 'not'}) #return catalog(title={'query': 'a', 'operator': 'not'})
......
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