Commit b633c900 authored by Sebastien Robin's avatar Sebastien Robin

added format support

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13928 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 26db466a
...@@ -185,17 +185,39 @@ class QueryMixin: ...@@ -185,17 +185,39 @@ class QueryMixin:
def getOperator(self): def getOperator(self):
return self.operator return self.operator
def getLogicalOperator(self):
return self.logical_operator
def _quoteSQLString(self, value): def _quoteSQLString(self, value):
"""Return a quoted string of the value. """Return a quoted string of the value.
""" """
if hasattr(value, 'ISO'): format = self.getFormat()
value = value.ISO() type = self.getType()
elif hasattr(value, 'strftime'): if format is not None and type is not None:
value = value.strftime('%Y-%m-%d %H:%M:%S') if type == 'date':
if hasattr(value, 'strftime'):
value = value.strftime(format)
if isinstance(value, basestring):
value = "STR_TO_DATE('%s','%s')" % (value, format)
else: else:
value = sql_quote(str(value)) if hasattr(value, 'ISO'):
value = "'%s'" % value.ISO()
elif hasattr(value, 'strftime'):
value = "'%s'" % value.strftime('%Y-%m-%d %H:%M:%S')
else:
value = "'%s'" % sql_quote(str(value))
return value return value
def _quoteSQLKey(self, key):
"""Return a quoted string of the value.
"""
format = self.getFormat()
type = self.getType()
if format is not None and type is not None:
if type == 'date':
key = "STR_TO_DATE(DATE_FORMAT(%s,'%s'),'%s')" % (key, format, format)
return key
class Query(QueryMixin): class Query(QueryMixin):
""" """
This allow to define constraints on a sql column This allow to define constraints on a sql column
...@@ -203,7 +225,7 @@ class Query(QueryMixin): ...@@ -203,7 +225,7 @@ 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, table_alias_list=None, **kw): search_mode=None, table_alias_list=None, type=None, **kw):
self.format = format self.format = format
if operator is None: if operator is None:
operator = 'OR' operator = 'OR'
...@@ -216,6 +238,7 @@ class Query(QueryMixin): ...@@ -216,6 +238,7 @@ class Query(QueryMixin):
raise KeyError, 'Query must have only one key' raise KeyError, 'Query must have only one key'
self.key = key_list[0] self.key = key_list[0]
self.value = kw[self.key] self.value = kw[self.key]
self.type = type
def __call__(self): def __call__(self):
self.asSQLExpression() self.asSQLExpression()
...@@ -226,6 +249,9 @@ class Query(QueryMixin): ...@@ -226,6 +249,9 @@ class Query(QueryMixin):
def getFormat(self): def getFormat(self):
return self.format return self.format
def getType(self):
return self.type
def getTableAliasList(self): def getTableAliasList(self):
return self.table_alias_list return self.table_alias_list
...@@ -273,24 +299,31 @@ class Query(QueryMixin): ...@@ -273,24 +299,31 @@ class Query(QueryMixin):
select_expression = [] select_expression = []
# Default case: variable equality # Default case: variable equality
range_value = self.getRange() range_value = self.getRange()
format = self.getFormat()
if ignore_key: if ignore_key:
pass pass
elif range_value is not None: elif range_value is not None:
if isinstance(value, (list, tuple)): if isinstance(value, (list, tuple)):
query_min = self._quoteSQLString(min(value)) if format is None:
query_max = self._quoteSQLString(max(value)) query_min = min(value)
query_max = max(value)
else:
query_min = value[0]
query_max = value[1]
else: else:
query_min=query_max=value query_min=query_max=value
query_min = self._quoteSQLString(query_min)
query_max = self._quoteSQLString(query_max)
if range_value == 'min' : if range_value == 'min' :
where_expression.append("%s >= '%s'" % (key, query_min)) where_expression.append("%s >= %s" % (key, query_min))
elif range_value == 'max' : elif range_value == 'max' :
where_expression.append("%s < '%s'" % (key, query_max)) where_expression.append("%s < %s" % (key, query_max))
elif range_value == 'minmax' : elif range_value == 'minmax' :
where_expression.append("%s >= '%s' and %s < '%s'" % (key, query_min, key, query_max)) where_expression.append("%s >= %s and %s < %s" % (key, query_min, key, query_max))
elif range_value == 'minngt' : elif range_value == 'minngt' :
where_expression.append("%s >= '%s' and %s <= '%s'" % (key, query_min, key, query_max)) where_expression.append("%s >= %s and %s <= %s" % (key, query_min, key, query_max))
elif range_value == 'ngt' : elif range_value == 'ngt' :
where_expression.append("%s <= '%s'" % (key, query_max)) where_expression.append("%s <= %s" % (key, query_max))
elif isSimpleType(value) or isinstance(value, DateTime) \ elif isSimpleType(value) or isinstance(value, DateTime) \
or isinstance(value, (list, tuple)): or isinstance(value, (list, tuple)):
# Convert into lists any value which contain a ; # Convert into lists any value which contain a ;
...@@ -303,26 +336,27 @@ class Query(QueryMixin): ...@@ -303,26 +336,27 @@ class Query(QueryMixin):
if isSimpleType(value) or isinstance(value, DateTime): if isSimpleType(value) or isinstance(value, DateTime):
value_list = [value] value_list = [value]
# For security. # For security.
LOG('SQLCatalog.Query value_list',0,value_list)
for value in value_list: for value in value_list:
value = self._quoteSQLString(value) comparison_operator = None
if value != '' or not ignore_empty_string: if (value != '' or not ignore_empty_string) \
and isinstance(value, basestring):
LOG('SQLCatalog,Query value:',0,value)
LOG('SQLCatalog,Query value after quote:',0,value)
LOG('SQLCatalog,Query format:',0,self.getFormat())
LOG('SQLCatalog,Query type:',0,self.getType())
if '%' in value: if '%' in value:
where_expression.append("%s LIKE '%s'" % (key, value)) comparison_operator = 'LIKE'
elif value.startswith('='): elif len(value) >= 1 and value[0:2] in ('<=','!=','>='):
where_expression.append("%s = '%s'" % (key, value[1:])) comparison_operator = value[0:2]
elif value.startswith('>='): value = value[2:]
where_expression.append("%s >= '%s'" % (key, value[2:])) elif len(value) >= 1 and value[0] in ('=','>','<'):
elif value.startswith('<='): comparison_operator = value[0]
where_expression.append("%s <= '%s'" % (key, value[2:])) value = value[1:]
elif value.startswith('>'):
where_expression.append("%s > '%s'" % (key, value[1:]))
elif value.startswith('<'):
where_expression.append("%s < '%s'" % (key, value[1:]))
elif value.startswith('!='):
where_expression.append("%s != '%s'" % (key, value[2:]))
elif key in keyword_search_keys: elif key in keyword_search_keys:
# We must add % in the request to simulate the catalog # We must add % in the request to simulate the catalog
where_expression.append("%s LIKE '%%%s%%'" % (key, value)) comparison_operator = 'LIKE'
value = '%%%s%%' % value
elif key in full_text_search_keys: elif key in full_text_search_keys:
# We must add % in the request to simulate the catalog # We must add % in the request to simulate the catalog
# we first check if there is a special search_mode for this key # we first check if there is a special search_mode for this key
...@@ -334,20 +368,33 @@ class Query(QueryMixin): ...@@ -334,20 +368,33 @@ class Query(QueryMixin):
search_mode = 'natural' search_mode = 'natural'
search_mode=search_mode.lower() search_mode=search_mode.lower()
mode = full_text_search_modes.get(search_mode,'') mode = full_text_search_modes.get(search_mode,'')
where_expression.append("MATCH %s AGAINST ('%s' %s)" % (key, value, mode)) where_expression.append(
"MATCH %s AGAINST ('%s' %s)" % (key, value, mode))
if not stat__: if not stat__:
# we return relevance as Table_Key_relevance # we return relevance as Table_Key_relevance
select_expression.append("MATCH %s AGAINST ('%s' %s) AS %s_relevance" % (key, value, mode,key.replace('.','_'))) select_expression.append(
"MATCH %s AGAINST ('%s' %s) AS %s_relevance"
% (key, value, mode,key.replace('.','_')))
# and for simplicity as Key_relevance # and for simplicity as Key_relevance
if '.' in key: if '.' in key:
select_expression.append("MATCH %s AGAINST ('%s' %s) AS %s_relevance" % (key, value, mode,key.split('.')[1])) select_expression.append(
"MATCH %s AGAINST ('%s' %s) AS %s_relevance" %
(key, value, mode,key.split('.')[1]))
else: else:
where_expression.append("%s = '%s'" % (key, value)) comparison_operator = '='
else:
comparison_operator = '='
if comparison_operator is not None:
key = self._quoteSQLKey(key)
value = self._quoteSQLString(value)
where_expression.append("%s %s %s" %
(key, comparison_operator, value))
elif value is None: elif value is None:
where_expression.append("%s is NULL" % (key)) where_expression.append("%s is NULL" % (key))
else: else:
where_expression.append("%s = %s" % (key, self._quoteSQLString(value))) where_expression.append("%s = %s" %
(self._quoteSQLKey(key), self._quoteSQLString(value)))
if len(where_expression)>0: if len(where_expression)>0:
if len(where_expression)==1: if len(where_expression)==1:
......
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