Commit bb2d0ee6 authored by Vincent Pelletier's avatar Vincent Pelletier Committed by Sebastien Robin

ZSQLCatalog: Allow retrieving the entire schema in a single query.

parent 6ca7d1a6
......@@ -497,8 +497,7 @@ class SimulationTool(BaseTool):
# Sort on
if 'sort_on' in new_kw:
table_column_list = ctool.getSQLCatalog()._getCatalogSchema(
table=table)
table_column_list = ctool.getSQLCatalog().getTableColumnList(table)
sort_on = new_kw['sort_on']
new_sort_on = []
for column_id, sort_direction in sort_on:
......
......@@ -479,12 +479,12 @@ class Catalog(Folder,
'type' : 'selection',
'select_variable' : 'getCatalogMethodIds',
'mode' : 'w' },
{ 'id' : 'sql_catalog_tables',
'description' : 'Method to get the main catalog tables',
{ 'id' : 'sql_catalog_schema',
'description' : 'Method to get the main catalog schema',
'type' : 'selection',
'select_variable' : 'getCatalogMethodIds',
'mode' : 'w' },
{ 'id' : 'sql_catalog_schema',
{ 'id' : 'sql_catalog_multi_schema',
'description' : 'Method to get the main catalog schema',
'type' : 'selection',
'select_variable' : 'getCatalogMethodIds',
......@@ -605,9 +605,9 @@ class Catalog(Folder,
sql_getitem_by_path = ''
sql_getitem_by_uid = ''
sql_optimizer_switch = ''
sql_catalog_tables = ''
sql_search_tables = ()
sql_catalog_schema = ''
sql_catalog_multi_schema = ''
sql_catalog_index = ''
sql_unique_values = ''
sql_catalog_paths = ''
......@@ -1082,30 +1082,46 @@ class Catalog(Folder,
"""
return self.sql_search_result_keys
def _getCatalogSchema(self, table=None):
method_name = self.sql_catalog_schema
try:
method = getattr(self, method_name)
except AttributeError:
pass
else:
@transactional_cache_decorator('SQLCatalog._getCatalogSchema')
def _getCatalogSchema(self):
method = getattr(self, self.sql_catalog_multi_schema, None)
if method is None:
# BBB: deprecated
method_name = self.sql_catalog_schema
try:
return tuple(c.Field for c in method(table=table))
except (ConflictError, DatabaseError):
raise
except Exception:
pass
method = getattr(self, method_name)
except AttributeError:
return {}
result = {}
for table in table_list:
try:
result[table] = [c.Field for c in method(table=table)]
except (ConflictError, DatabaseError):
raise
except Exception:
LOG('SQLCatalog', WARNING, '_getCatalogSchema failed with the method %s'
% method_name, error=sys.exc_info())
return result
result = {}
for row in method():
result.setdefault(row.TABLE_NAME, []).append(row.COLUMN_NAME)
return result
LOG('SQLCatalog', WARNING, '_getCatalogSchema failed with the method %s'
% method_name, error=sys.exc_info())
return ()
security.declarePrivate('getTableColumnList')
def getTableColumnList(self, table):
"""
Returns the list of columns in given table.
Raises KeyError on unknown table.
"""
return self._getCatalogSchema()[table]
@transactional_cache_decorator('SQLCatalog.getColumnIds')
def _getColumnIds(self):
keys = set()
add_key = keys.add
table_dict = self._getCatalogSchema()
for table in self.getCatalogSearchTableIds():
for field in self._getCatalogSchema(table=table):
for field in table_dict[table]:
add_key(field)
add_key('%s.%s' % (table, field)) # Is this inconsistent ?
for related in self.getSQLCatalogRelatedKeyList():
......@@ -1132,8 +1148,9 @@ class Catalog(Folder,
Field Ids
"""
result = {}
table_dict = self._getCatalogSchema()
for table in self.getCatalogSearchTableIds():
for field in self._getCatalogSchema(table=table):
for field in table_dict[table]:
result.setdefault(field, []).append(table)
result.setdefault('%s.%s' % (table, field), []).append(table) # Is this inconsistent ?
return result
......@@ -1146,8 +1163,9 @@ class Catalog(Folder,
Field Ids
"""
keys = set()
table_dict = self._getCatalogSchema()
for table in self.getCatalogSearchTableIds():
for field in self._getCatalogSchema(table=table):
for field in table_dict[table]:
keys.add('%s.%s' % (table, field))
return sorted(keys)
......@@ -1159,8 +1177,9 @@ class Catalog(Folder,
Field Ids that can be used for a sort
"""
keys = set()
table_dict = self._getCatalogSchema()
for table in self.getTableIds():
for field in self._getCatalogSchema(table=table):
for field in table_dict[table]:
keys.add('%s.%s' % (table, field))
return sorted(keys)
......@@ -1170,22 +1189,7 @@ class Catalog(Folder,
Calls the show table method and returns dictionnary of
Field Ids
"""
method_name = self.sql_catalog_tables
try:
method = getattr(self, method_name)
except AttributeError:
pass
else:
try:
return [c[0] for c in method()]
except (ConflictError, DatabaseError):
raise
except Exception:
pass
LOG('SQLCatalog', WARNING, 'getTableIds failed with the method %s'
% method_name, error=sys.exc_info())
return []
return self._getCatalogSchema().keys()
security.declarePrivate('getUIDBuffer')
def getUIDBuffer(self, force_new_buffer=False):
......
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