Commit cf9481c2 authored by Sebastien Robin's avatar Sebastien Robin

- allow to define aliases used by related keys

- make buildSQLQuery a bit faster


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13718 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e36d524e
...@@ -203,13 +203,14 @@ class Query(QueryMixin): ...@@ -203,13 +203,14 @@ class Query(QueryMixin):
format - %d/%m/%Y format - %d/%m/%Y
""" """
def __init__(self, format=None, operator=None, range=None, def __init__(self, format=None, operator=None, range=None,
search_mode=None, **kw): search_mode=None, table_alias_list=None, **kw):
self.format = format self.format = format
if operator is None: if operator is None:
operator = 'OR' operator = 'OR'
self.operator = operator self.operator = operator
self.range = range self.range = range
self.search_mode = search_mode self.search_mode = search_mode
self.table_alias_list = table_alias_list
key_list = kw.keys() key_list = kw.keys()
if len(key_list) != 1: if len(key_list) != 1:
raise KeyError, 'Query must have only one key' raise KeyError, 'Query must have only one key'
...@@ -225,6 +226,16 @@ class Query(QueryMixin): ...@@ -225,6 +226,16 @@ class Query(QueryMixin):
def getFormat(self): def getFormat(self):
return self.format return self.format
def getTableAliasList(self):
return self.table_alias_list
def getRelatedTableMapDict(self):
result = {}
table_alias_list = self.getTableAliasList()
if table_alias_list is not None:
result[self.getKey()] = table_alias_list
return result
def getSearchMode(self): def getSearchMode(self):
return self.search_mode return self.search_mode
...@@ -384,6 +395,12 @@ class ComplexQuery(QueryMixin): ...@@ -384,6 +395,12 @@ class ComplexQuery(QueryMixin):
def getQueryList(self): def getQueryList(self):
return self.query_list return self.query_list
def getRelatedTableMapDict(self):
result = {}
for query in self.getQueryList():
result.update(query.getRelatedTableMapDict())
return result
def asSQLExpression(self, key_alias_dict=None, def asSQLExpression(self, key_alias_dict=None,
ignore_empty_string=1, ignore_empty_string=1,
keyword_search_keys=None, keyword_search_keys=None,
...@@ -1870,6 +1887,9 @@ class Catalog( Folder, ...@@ -1870,6 +1887,9 @@ class Catalog( Folder,
key_list = [] # the list of column keys key_list = [] # the list of column keys
key_alias_dict = {} key_alias_dict = {}
query_group_by_list = None # Useful to keep a default group_by passed by scriptable keys query_group_by_list = None # Useful to keep a default group_by passed by scriptable keys
query_related_table_map_dict = {}
if query is not None:
kw ['query'] = query
for key in kw.keys(): for key in kw.keys():
if key not in RESERVED_KEY_LIST: if key not in RESERVED_KEY_LIST:
value = kw[key] value = kw[key]
...@@ -1895,10 +1915,7 @@ class Catalog( Folder, ...@@ -1895,10 +1915,7 @@ class Catalog( Folder,
if current_query is not None: if current_query is not None:
query_dict[key] = current_query query_dict[key] = current_query
key_list.extend(current_query.getSQLKeyList()) key_list.extend(current_query.getSQLKeyList())
query_related_table_map_dict.update(current_query.getRelatedTableMapDict())
if query is not None:
query_dict['query'] = query
key_list.extend(query.getSQLKeyList())
# if we have a sort index, we must take it into account to get related # if we have a sort index, we must take it into account to get related
# keys. # keys.
...@@ -1924,23 +1941,25 @@ class Catalog( Folder, ...@@ -1924,23 +1941,25 @@ class Catalog( Folder,
for t in related_tuples: for t in related_tuples:
t_tuple = t.split('|') t_tuple = t.split('|')
key = t_tuple[0].strip() key = t_tuple[0].strip()
join_tuple = t_tuple[1].strip().split('/') if key in key_list:
#LOG('related_tuples', 0, str(join_tuple)) join_tuple = t_tuple[1].strip().split('/')
related_keys.append(key) related_keys.append(key)
# LOG('buildSqlQuery, join_tuple',0,join_tuple) method_id = join_tuple[2]
method_id = join_tuple[2] table_list = tuple(join_tuple[0].split(','))
table_list = tuple(join_tuple[0].split(',')) related_method[key] = method_id
related_method[key] = method_id related_table_list[key] = table_list
related_table_list[key] = table_list related_column[key] = join_tuple[1]
related_column[key] = join_tuple[1] # Check if some aliases where specified in queries
# Rename tables to prevent conflicts map_list = query_related_table_map_dict.get(key,None)
if not related_table_map.has_key((table_list,method_id)): # Rename tables to prevent conflicts
map_list = [] if not related_table_map.has_key((table_list,method_id)):
for table_id in table_list: if map_list is None:
map_list.append((table_id, map_list = []
"related_%s_%s" % (table_id, table_rename_index))) # We add an index in order to alias tables in the join for table_id in table_list:
table_rename_index += 1 # and prevent name conflicts map_list.append((table_id,
related_table_map[(table_list,method_id)] = map_list "related_%s_%s" % (table_id, table_rename_index))) # We add an index in order to alias tables in the join
table_rename_index += 1 # and prevent name conflicts
related_table_map[(table_list,method_id)] = map_list
# We take additional parameters from the REQUEST # We take additional parameters from the REQUEST
# and give priority to the REQUEST # and give priority to the REQUEST
......
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