Commit 66f13089 authored by Vincent Pelletier's avatar Vincent Pelletier

SQLCatalog: Assorted cleanups and simplifications.

No backslash at EOL.
Avoid splitting messages, to ease code lookup.
LOG's "error" argument accepts a boolean, which removes the need to touch
sys.exc_info.
Use modern exception raising syntax.
parent b0978eaf
...@@ -22,8 +22,11 @@ from App.special_dtml import DTMLFile ...@@ -22,8 +22,11 @@ from App.special_dtml import DTMLFile
from thread import allocate_lock, get_ident from thread import allocate_lock, get_ident
from OFS.Folder import Folder from OFS.Folder import Folder
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from AccessControl.Permissions import access_contents_information, \ from AccessControl.Permissions import (
import_export_objects, manage_zcatalog_entries access_contents_information,
import_export_objects,
manage_zcatalog_entries,
)
from AccessControl.SimpleObjectPolicies import ContainerAssertions from AccessControl.SimpleObjectPolicies import ContainerAssertions
from BTrees.OIBTree import OIBTree from BTrees.OIBTree import OIBTree
from App.config import getConfiguration from App.config import getConfiguration
...@@ -38,7 +41,6 @@ from Products.CMFCore import permissions ...@@ -38,7 +41,6 @@ from Products.CMFCore import permissions
from Products.PythonScripts.Utility import allow_class from Products.PythonScripts.Utility import allow_class
import time import time
import sys
import urllib import urllib
import string import string
import pprint import pprint
...@@ -76,8 +78,7 @@ except ImportError: ...@@ -76,8 +78,7 @@ except ImportError:
def noReadOnlyTransactionCache(): def noReadOnlyTransactionCache():
yield yield
try: try:
from Products.ERP5Type.Cache import \ from Products.ERP5Type.Cache import readOnlyTransactionCache
readOnlyTransactionCache
except ImportError: except ImportError:
LOG('SQLCatalog', WARNING, 'Count not import readOnlyTransactionCache, expect slowness.') LOG('SQLCatalog', WARNING, 'Count not import readOnlyTransactionCache, expect slowness.')
readOnlyTransactionCache = noReadOnlyTransactionCache readOnlyTransactionCache = noReadOnlyTransactionCache
...@@ -546,8 +547,7 @@ class Catalog(Folder, ...@@ -546,8 +547,7 @@ class Catalog(Folder,
'mode': 'w' }, 'mode': 'w' },
{ 'id': 'sql_catalog_local_role_keys', { 'id': 'sql_catalog_local_role_keys',
'title': 'Local Role keys', 'title': 'Local Role keys',
'description': 'Columns which should be used to map' \ 'description': 'Columns which should be used to map a monovalued local role',
'a monovalued local role',
'type': 'lines', 'type': 'lines',
'mode': 'w' }, 'mode': 'w' },
{ 'id': 'sql_catalog_security_uid_columns', { 'id': 'sql_catalog_security_uid_columns',
...@@ -732,15 +732,12 @@ class Catalog(Folder, ...@@ -732,15 +732,12 @@ class Catalog(Folder,
for key in wrapped_object.getLocalRolesGroupIdDict().iteritems(): for key in wrapped_object.getLocalRolesGroupIdDict().iteritems():
local_roles_group_id, allowed_roles_and_users = key local_roles_group_id, allowed_roles_and_users = key
if key in self.security_uid_dict: if key in self.security_uid_dict:
local_roles_group_id_to_security_uid_mapping[local_roles_group_id] \ local_roles_group_id_to_security_uid_mapping[local_roles_group_id] = self.security_uid_dict[key]
= self.security_uid_dict[key] elif allowed_roles_and_users in self.security_uid_dict and not local_roles_group_id:
elif allowed_roles_and_users in self.security_uid_dict \
and not local_roles_group_id:
# This key is present in security_uid_dict without # This key is present in security_uid_dict without
# local_roles_group_id, it has been inserted before # local_roles_group_id, it has been inserted before
# local_roles_group_id were introduced. # local_roles_group_id were introduced.
local_roles_group_id_to_security_uid_mapping[local_roles_group_id] = \ local_roles_group_id_to_security_uid_mapping[local_roles_group_id] = self.security_uid_dict[allowed_roles_and_users]
self.security_uid_dict[allowed_roles_and_users]
else: else:
if not security_uid: if not security_uid:
getTransactionalVariable().pop('getSecurityUidDictAndRoleColumnDict', getTransactionalVariable().pop('getSecurityUidDictAndRoleColumnDict',
...@@ -761,8 +758,7 @@ class Catalog(Folder, ...@@ -761,8 +758,7 @@ class Catalog(Folder,
) )
self.security_uid_dict[key] = security_uid self.security_uid_dict[key] = security_uid
local_roles_group_id_to_security_uid_mapping[local_roles_group_id]\ local_roles_group_id_to_security_uid_mapping[local_roles_group_id] = security_uid
= security_uid
# If some optimised_roles_and_users are returned by this method it # If some optimised_roles_and_users are returned by this method it
# means that new entries will have to be added to roles_and_users table. # means that new entries will have to be added to roles_and_users table.
...@@ -847,7 +843,7 @@ class Catalog(Folder, ...@@ -847,7 +843,7 @@ class Catalog(Folder,
raise raise
except: except:
LOG('SQLCatalog', WARNING, LOG('SQLCatalog', WARNING,
'could not clear catalog with %s' % method_name, error=sys.exc_info()) 'could not clear catalog with %s' % method_name, error=True)
raise raise
# Reserved uids have been removed. # Reserved uids have been removed.
self._clearReserved() self._clearReserved()
...@@ -866,9 +862,12 @@ class Catalog(Folder, ...@@ -866,9 +862,12 @@ class Catalog(Folder,
except ConflictError: except ConflictError:
raise raise
except: except:
LOG('SQLCatalog', WARNING, LOG(
'could not clear reserved catalog with %s' % \ 'SQLCatalog',
method_id, error=sys.exc_info()) WARNING,
'could not clear reserved catalog with %s' % method_id,
error=True,
)
raise raise
self._last_clear_reserved_time += 1 self._last_clear_reserved_time += 1
...@@ -966,8 +965,12 @@ class Catalog(Folder, ...@@ -966,8 +965,12 @@ class Catalog(Folder,
except (ConflictError, DatabaseError): except (ConflictError, DatabaseError):
raise raise
except Exception: except Exception:
LOG('SQLCatalog', WARNING, '_getCatalogSchema failed with the method %s' LOG(
% method_name, error=sys.exc_info()) 'SQLCatalog',
WARNING,
'_getCatalogSchema failed with the method %s' % method_name,
error=True,
)
return result return result
for row in method(): for row in method():
result.setdefault(row.TABLE_NAME, []).append(row.COLUMN_NAME) result.setdefault(row.TABLE_NAME, []).append(row.COLUMN_NAME)
...@@ -1224,8 +1227,9 @@ class Catalog(Folder, ...@@ -1224,8 +1227,9 @@ class Catalog(Folder,
self._clear() self._clear()
if RESPONSE and URL1: if RESPONSE and URL1:
RESPONSE.redirect('%s/manage_catalogAdvanced?' \ RESPONSE.redirect(
'manage_tabs_message=Catalog%%20Cleared' % URL1) '%s/manage_catalogAdvanced?manage_tabs_message=Catalog%%20Cleared' % URL1,
)
security.declareProtected(manage_zcatalog_entries, 'manage_catalogClearReserved') security.declareProtected(manage_zcatalog_entries, 'manage_catalogClearReserved')
def manage_catalogClearReserved(self, REQUEST=None, RESPONSE=None, URL1=None): def manage_catalogClearReserved(self, REQUEST=None, RESPONSE=None, URL1=None):
...@@ -1233,8 +1237,9 @@ class Catalog(Folder, ...@@ -1233,8 +1237,9 @@ class Catalog(Folder,
self._clearReserved() self._clearReserved()
if RESPONSE and URL1: if RESPONSE and URL1:
RESPONSE.redirect('%s/manage_catalogAdvanced?' \ RESPONSE.redirect(
'manage_tabs_message=Catalog%%20Cleared' % URL1) '%s/manage_catalogAdvanced?manage_tabs_message=Catalog%%20Cleared' % URL1,
)
security.declareProtected(manage_zcatalog_entries, 'manage_catalogFoundItems') security.declareProtected(manage_zcatalog_entries, 'manage_catalogFoundItems')
def manage_catalogFoundItems(self, REQUEST, RESPONSE, URL2, URL1, def manage_catalogFoundItems(self, REQUEST, RESPONSE, URL2, URL1,
...@@ -1477,7 +1482,7 @@ class Catalog(Folder, ...@@ -1477,7 +1482,7 @@ class Catalog(Folder,
object_list, object_list,
method_name, method_name,
), ),
error=sys.exc_info(), error=True,
) )
raise raise
...@@ -1513,8 +1518,7 @@ class Catalog(Folder, ...@@ -1513,8 +1518,7 @@ class Catalog(Folder,
return None return None
if method_name in (None,''): if method_name in (None,''):
# This should exist only if the site is not up to date. # This should exist only if the site is not up to date.
LOG('ZSQLCatalog.beforeUncatalogObject',0,'The sql_catalog_delete_uid'\ LOG('ZSQLCatalog.beforeUncatalogObject', INFO, 'The sql_catalog_delete_uid method is not defined')
+ ' method is not defined')
return self.uncatalogObject(path=path,uid=uid) return self.uncatalogObject(path=path,uid=uid)
method = self._getOb(method_name) method = self._getOb(method_name)
method(uid = uid) method(uid = uid)
...@@ -1575,7 +1579,7 @@ class Catalog(Folder, ...@@ -1575,7 +1579,7 @@ class Catalog(Folder,
except ConflictError: except ConflictError:
raise raise
except: except:
LOG('SQLCatalog', WARNING, 'could not delete translations', error=sys.exc_info()) LOG('SQLCatalog', WARNING, 'could not delete translations', error=True)
def getSqlUniqueValues(self): def getSqlUniqueValues(self):
return self.sql_unique_values return self.sql_unique_values
...@@ -1719,8 +1723,7 @@ class Catalog(Folder, ...@@ -1719,8 +1723,7 @@ class Catalog(Folder,
LOG('SQLCatalog', WARNING, 'Malformed related key definition: %r. Ignored.' % (related_key, )) LOG('SQLCatalog', WARNING, 'Malformed related key definition: %r. Ignored.' % (related_key, ))
continue continue
related_key_id = split_entire_definition[0].strip() related_key_id = split_entire_definition[0].strip()
if related_key_id in column_set and \ if related_key_id in column_set and related_key_id not in related_key_warned_column_set:
related_key_id not in related_key_warned_column_set:
related_key_warned_column_set.add(related_key_id) related_key_warned_column_set.add(related_key_id)
if related_key_id in column_map: if related_key_id in column_map:
LOG('SQLCatalog', WARNING, 'Related key %r has the same name as an existing column on tables %r' % (related_key_id, column_map[related_key_id])) LOG('SQLCatalog', WARNING, 'Related key %r has the same name as an existing column on tables %r' % (related_key_id, column_map[related_key_id]))
...@@ -1838,8 +1841,7 @@ class Catalog(Folder, ...@@ -1838,8 +1841,7 @@ class Catalog(Folder,
key, script_id = [x.strip() for x in split_scriptable_key_definition] key, script_id = [x.strip() for x in split_scriptable_key_definition]
script = getattr(self, script_id, None) script = getattr(self, script_id, None)
if script is None: if script is None:
LOG('SQLCatalog', WARNING, 'Scriptable key %r script %r is missing.' \ LOG('SQLCatalog', WARNING, 'Scriptable key %r script %r is missing. Skipped.' % (key, script_id))
' Skipped.' % (key, script_id))
else: else:
result[key] = script result[key] = script
return result return result
...@@ -2028,8 +2030,7 @@ class Catalog(Folder, ...@@ -2028,8 +2030,7 @@ class Catalog(Folder,
def parseSearchText(self, search_text, column=None, search_key=None, def parseSearchText(self, search_text, column=None, search_key=None,
is_valid=None): is_valid=None):
if column is None and search_key is None: if column is None and search_key is None:
raise ValueError, 'One of column and search_key must be different '\ raise ValueError('One of column and search_key must be different from None')
'from None'
return self._parseSearchText(self.getSearchKey( return self._parseSearchText(self.getSearchKey(
column, search_key=search_key), search_text, is_valid=is_valid) column, search_key=search_key), search_text, is_valid=is_valid)
...@@ -2415,8 +2416,9 @@ class Catalog(Folder, ...@@ -2415,8 +2416,9 @@ class Catalog(Folder,
""" Returns a list of brains from a set of constraints on variables """ """ Returns a list of brains from a set of constraints on variables """
if 'only_group_columns' in kw: if 'only_group_columns' in kw:
# searchResults must be consistent in API with countResults # searchResults must be consistent in API with countResults
raise ValueError('only_group_columns does not belong to this API ' raise ValueError(
'level, use queryResults directly') 'only_group_columns does not belong to this API level, use queryResults directly',
)
return self.queryResults( return self.queryResults(
self.getSearchResultsMethod(), self.getSearchResultsMethod(),
REQUEST=REQUEST, REQUEST=REQUEST,
...@@ -2489,53 +2491,54 @@ class Catalog(Folder, ...@@ -2489,53 +2491,54 @@ class Catalog(Folder,
If 'deferred' is True, then returns the deferred connection If 'deferred' is True, then returns the deferred connection
""" """
for method in self.objectValues(): for method in self.objectValues():
if method.meta_type in ('Z SQL Method', 'ERP5 SQL Method',): if method.meta_type in ('Z SQL Method', 'ERP5 SQL Method') and ('deferred' in method.connection_id) == deferred:
if ('deferred' in method.connection_id) == deferred:
return method.connection_id return method.connection_id
def getSqlCatalogObjectList(self): def getSqlCatalogObjectList(self):
try: try:
method_id_list = self.sql_catalog_object return self.sql_catalog_object
except AttributeError: except AttributeError:
method_id_list = () return ()
return method_id_list
def getSqlUncatalogObjectList(self): def getSqlUncatalogObjectList(self):
try: try:
method_id_list = self.sql_uncatalog_object return self.sql_uncatalog_object
except AttributeError: except AttributeError:
method_id_list = () return ()
return method_id_list
def getSqlUpdateObjectList(self): def getSqlUpdateObjectList(self):
try: try:
method_id_list = self.sql_update_object return self.sql_update_object
except AttributeError: except AttributeError:
method_id_list = () return ()
return method_id_list
def getSqlCatalogObjectListList(self): def getSqlCatalogObjectListList(self):
try: try:
method_id_list = self.sql_catalog_object_list return self.sql_catalog_object_list
except AttributeError: except AttributeError:
method_id_list = () return ()
return method_id_list
security.declarePrivate('getFilterableMethodList') security.declarePrivate('getFilterableMethodList')
def getFilterableMethodList(self): def getFilterableMethodList(self):
""" """
Returns only zsql methods wich catalog or uncatalog objets Returns only zsql methods wich catalog or uncatalog objets
""" """
method_dict = {} method_id_set = set()
if withCMF: if withCMF:
method_id_list = self.getSqlCatalogObjectList() +\ self.update(
self.getSqlUncatalogObjectList() +\ self.getSqlCatalogObjectList() +
self.getSqlUpdateObjectList() +\ self.getSqlUncatalogObjectList() +
self.getSqlUpdateObjectList() +
self.getSqlCatalogObjectListList() self.getSqlCatalogObjectListList()
for method_id in method_id_list: )
method_dict[method_id] = 1 return [
method_list = map(lambda method_id: getattr(self, method_id, None), method_dict.keys()) method
return filter(lambda method: method is not None, method_list) for method in (
getattr(self, method_id, None)
for method_id in method_id_set
)
if method is not None
]
security.declarePrivate('getExpressionContext') security.declarePrivate('getExpressionContext')
def getExpressionContext(self, ob): def getExpressionContext(self, ob):
...@@ -2576,16 +2579,21 @@ class Catalog(Folder, ...@@ -2576,16 +2579,21 @@ class Catalog(Folder,
raise raise
except Exception: except Exception:
pass pass
LOG(
LOG('SQLCatalog', WARNING, 'getTableIds failed with the method %s' 'SQLCatalog',
% method_name, error=sys.exc_info()) WARNING,
'getTableIds failed with the method %s' % method_name,
error=True,
)
return '' return ''
security.declarePublic('getOptimizerSwitchKeyList') security.declarePublic('getOptimizerSwitchKeyList')
@transactional_cache_decorator('SQLCatalog.getOptimizerSwitchKeyList') @transactional_cache_decorator('SQLCatalog.getOptimizerSwitchKeyList')
def getOptimizerSwitchKeyList(self): def getOptimizerSwitchKeyList(self):
return [pair.split('=', 1)[0] for pair in \ return [
self._getOptimizerSwitch().split(',')] pair.split('=', 1)[0]
for pair in self._getOptimizerSwitch().split(',')
]
InitializeClass(Catalog) InitializeClass(Catalog)
...@@ -2648,14 +2656,17 @@ class SearchKeyWrapperForScriptableKey(SearchKey.SearchKey.SearchKey): ...@@ -2648,14 +2656,17 @@ class SearchKeyWrapperForScriptableKey(SearchKey.SearchKey.SearchKey):
# XXX: It would be better to extend ScriptableKey API to support other # XXX: It would be better to extend ScriptableKey API to support other
# parameters. # parameters.
if group is not None: if group is not None:
raise ValueError, 'ScriptableKey cannot be used inside a group ' \ raise ValueError(
'(%r given).' % (group, ) 'ScriptableKey cannot be used inside a group (%r given).' % (group, ),
)
if logical_operator is not None: if logical_operator is not None:
raise ValueError, 'ScriptableKey ignores logical operators ' \ raise ValueError(
'(%r given).' % (logical_operator, ) 'ScriptableKey ignores logical operators (%r given).' % (logical_operator, ),
)
if comparison_operator != '': if comparison_operator != '':
raise ValueError, 'ScriptableKey ignores comparison operators ' \ raise ValueError(
'(%r given).' % (comparison_operator, ) 'ScriptableKey ignores comparison operators (%r given).' % (comparison_operator, ),
)
return self.script(search_value) return self.script(search_value)
from Operator import operator_dict from Operator import operator_dict
......
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