Commit e2090ef8 authored by Alexandre Boeglin's avatar Alexandre Boeglin

Fixed some bugs : catch an exception, solve conflict in SQL column names, and...

Fixed some bugs : catch an exception, solve conflict in SQL column names, and return a dictionary when building SQL query.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@1432 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0c810127
...@@ -116,7 +116,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -116,7 +116,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" """
Calls the show column method and returns dictionnary of Calls the show column method and returns dictionnary of
Field Ids Field Ids
XXX This should be cached XXX This should be cached
""" """
method_name = self.sql_catalog_schema method_name = self.sql_catalog_schema
...@@ -138,7 +138,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -138,7 +138,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" """
Calls the show column method and returns dictionnary of Calls the show column method and returns dictionnary of
Field Ids Field Ids
XXX This should be cached XXX This should be cached
""" """
method_name = self.sql_catalog_schema method_name = self.sql_catalog_schema
...@@ -156,7 +156,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -156,7 +156,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
keys[key].append(table) # Is this inconsistent ? keys[key].append(table) # Is this inconsistent ?
except: except:
pass pass
return keys return keys
def getResultColumnIds(self): def getResultColumnIds(self):
""" """
...@@ -383,12 +383,23 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -383,12 +383,23 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
kw['path'] = path kw['path'] = path
kw['uid'] = index kw['uid'] = index
kw['insert_catalog_line'] = insert_catalog_line kw['insert_catalog_line'] = insert_catalog_line
# Alter/Create row # LOG
zope_root = self.getPortalObject().aq_parent # Alter row
root_indexable = int(getattr(zope_root,'isIndexable',1)) # Create row
if root_indexable: try:
#LOG("Call SQL Method %s with args:" % method_name,0, str(kw)) zope_root = self.getPortalObject().aq_parent
method(**kw) root_indexable = int(getattr(zope_root,'isIndexable',1))
if root_indexable:
#LOG("Call SQL Method %s with args:" % method_name,0, str(kw))
method(**kw)
except:
LOG("SQLCatalog Warning: could not catalog object with method %s" % method_name,100, str(path))
raise
#except:
# # # This is a real LOG message
# # # which is required in order to be able to import .zexp files
# LOG("SQLCatalog Warning: could not catalog object with method %s" % method_name,
# 100,str(path))
def uncatalogObject(self, path): def uncatalogObject(self, path):
""" """
...@@ -508,14 +519,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -508,14 +519,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" Accesses a single record for a given path """ """ Accesses a single record for a given path """
return self.getMetadataForPath(path) return self.getMetadataForPath(path)
def buildSQLQuery(self, REQUEST=None, **kw): def buildSQLQuery(self, query_table='catalog', REQUEST=None, **kw):
""" """ Builds a complex SQL query to simulate ZCalatog behaviour """
"""
def queryResults(self, sql_method, REQUEST=None, used=None, **kw):
""" Builds a complex SQL where_expression to simulate ZCalatog behaviour """
""" Returns a list of brains from a set of constraints on variables """
# Get search arguments: # Get search arguments:
if REQUEST is None and (kw is None or kw == {}): if REQUEST is None and (kw is None or kw == {}):
# We try to get the REQUEST parameter # We try to get the REQUEST parameter
...@@ -527,14 +532,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -527,14 +532,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if kw is None or kw == {}: if kw is None or kw == {}:
kw = REQUEST kw = REQUEST
acceptable_key_map = self.getColumnMap() acceptable_key_map = self.getColumnMap()
acceptable_keys = acceptable_key_map.keys() acceptable_keys = acceptable_key_map.keys()
full_text_search_keys = self.sql_catalog_full_text_search_keys full_text_search_keys = self.sql_catalog_full_text_search_keys
keyword_search_keys = self.sql_catalog_keyword_search_keys keyword_search_keys = self.sql_catalog_keyword_search_keys
# 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
if REQUEST is not None: if REQUEST is not None:
for key in acceptable_keys: for key in acceptable_keys:
if REQUEST.has_key(key): if REQUEST.has_key(key):
# Only copy a few keys from the REQUEST # Only copy a few keys from the REQUEST
...@@ -545,12 +550,21 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -545,12 +550,21 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if kw: if kw:
where_expression = [] where_expression = []
from_table_dict = {'catalog': 1} # Always include catalog table from_table_dict = {'catalog': 1} # Always include catalog table
for key, value in kw.items(): for key in kw.keys(): # Do not use kw.items() because this consumes much more memory
value = kw[key]
if key not in ('where_expression', 'sort-on', 'sort_on', 'sort-order', 'sort_order'): if key not in ('where_expression', 'sort-on', 'sort_on', 'sort-order', 'sort_order'):
# Make sure key belongs to schema # Make sure key belongs to schema
if key in acceptable_keys: if key in acceptable_keys:
# uid is always ambiguous so we can only change it here if key.find('.') < 0:
if key == 'uid': key = 'catalog.uid' # if the key is only used by one table, just append its name
if len(acceptable_key_map[key]) == 1 :
key = acceptable_key_map[key][0] + '.' + key
# query_table specifies what table name should be used
elif query_table:
key = query_table + '.' + key
elif key == 'uid':
# uid is always ambiguous so we can only change it here
key = 'catalog.uid'
# Add table to table dict # Add table to table dict
from_table_dict[acceptable_key_map[key][0]] = 1 # We use catalog by default from_table_dict[acceptable_key_map[key][0]] = 1 # We use catalog by default
# Default case: variable equality # Default case: variable equality
...@@ -601,13 +615,11 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -601,13 +615,11 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
elif key == 'where_expression': elif key == 'where_expression':
# Not implemented yet # Not implemented yet
pass pass
if kw.has_key('where_expression'): if kw.get('where_expression'):
if len(where_expression) > 0: if len(where_expression) > 0:
kw['where_expression'] = "(%s) AND (%s)" % (kw['where_expression'], join(where_expression, ' AND ') ) where_expression = "(%s) AND (%s)" % (kw['where_expression'], join(where_expression, ' AND ') )
else: else:
kw['where_expression'] = join(where_expression, ' AND ') where_expression = join(where_expression, ' AND ')
#LOG("Search Query Args:",0,str(kw))
# Compute "sort_index", which is a sort index, or none: # Compute "sort_index", which is a sort index, or none:
if kw.has_key('sort-on'): if kw.has_key('sort-on'):
...@@ -640,10 +652,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -640,10 +652,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# If sort_index is a dictionnary # If sort_index is a dictionnary
# then parse it and change it # then parse it and change it
sort_on = None
if sort_index is not None: if sort_index is not None:
try: try:
new_sort_index = [] new_sort_index = []
for (k , v) in sort_index: for (k , v) in sort_index:
if query_table: k = query_table + '.' + k
if v == 'descending' or v == 'reverse': if v == 'descending' or v == 'reverse':
from_table_dict[acceptable_key_map[k][0]] = 1 # We need this table to sort on it from_table_dict[acceptable_key_map[k][0]] = 1 # We need this table to sort on it
new_sort_index += ['%s DESC' % k] new_sort_index += ['%s DESC' % k]
...@@ -651,19 +665,30 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -651,19 +665,30 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
from_table_dict[acceptable_key_map[k][0]] = 1 # We need this table to sort on it from_table_dict[acceptable_key_map[k][0]] = 1 # We need this table to sort on it
new_sort_index += ['%s' % k] new_sort_index += ['%s' % k]
sort_index = join(new_sort_index,',') sort_index = join(new_sort_index,',')
kw['sort_on'] = str(sort_index) sort_on = str(sort_index)
except: except:
pass pass
# Use a dictionary at the moment.
return { 'from_table_list' : from_table_dict.keys(),
'order_by_expression' : sort_on,
'where_expression' : where_expression }
def queryResults(self, sql_method, REQUEST=None, used=None, **kw):
""" Returns a list of brains from a set of constraints on variables """
query = self.buildSQLQuery(REQUEST=REQUEST, **kw)
kw['where_expression'] = query['where_expression']
kw['sort_on'] = query['order_by_expression']
kw['from_table_list'] = query['from_table_list']
# Return the result # Return the result
#LOG('acceptable_keys',0,'acceptable_keys: %s' % str(acceptable_keys)) #LOG('acceptable_keys',0,'acceptable_keys: %s' % str(acceptable_keys))
#LOG('acceptable_key_map',0,'acceptable_key_map: %s' % str(acceptable_key_map)) #LOG('acceptable_key_map',0,'acceptable_key_map: %s' % str(acceptable_key_map))
#LOG('queryResults',0,'kw: %s' % str(kw)) #LOG('queryResults',0,'kw: %s' % str(kw))
#LOG('queryResults',0,'from_table_list: %s' % str(from_table_dict.keys())) #LOG('queryResults',0,'from_table_list: %s' % str(from_table_dict.keys()))
return sql_method(from_table_list = from_table_dict.keys(), **kw) return sql_method(**kw)
def searchResults(self, REQUEST=None, used=None, **kw): def searchResults(self, REQUEST=None, used=None, **kw):
""" Builds a complex SQL where_expression to simulate ZCalatog behaviour """ """ Builds a complex SQL where_expression to simulate ZCalatog behaviour """
""" Returns a list of brains from a set of constraints on variables """ """ Returns a list of brains from a set of constraints on variables """
# The used argument is deprecated and is ignored # The used argument is deprecated and is ignored
......
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