Commit d8fac823 authored by Vincent Pelletier's avatar Vincent Pelletier

ZSQLCatalog: Add parameter to raise on unknown column.

parent 2489af45
...@@ -2122,7 +2122,7 @@ class Catalog(Folder, ...@@ -2122,7 +2122,7 @@ class Catalog(Folder,
result = script(value) result = script(value)
return result return result
def _buildQueryFromAbstractSyntaxTreeNode(self, node, search_key, wrap): def _buildQueryFromAbstractSyntaxTreeNode(self, node, search_key, wrap, ignore_unknown_columns):
""" """
node node
Abstract syntax tree node (see SearchText/AdvancedSearchTextParser.py, Abstract syntax tree node (see SearchText/AdvancedSearchTextParser.py,
...@@ -2141,7 +2141,11 @@ class Catalog(Folder, ...@@ -2141,7 +2141,11 @@ class Catalog(Folder,
result = search_key.buildQuery(wrap(_dequote(node.getValue())), result = search_key.buildQuery(wrap(_dequote(node.getValue())),
comparison_operator=node.getComparisonOperator()) comparison_operator=node.getComparisonOperator())
elif node.isColumn(): elif node.isColumn():
result = self.buildQueryFromAbstractSyntaxTreeNode(node.getSubNode(), node.getColumnName()) result = self.buildQueryFromAbstractSyntaxTreeNode(
node.getSubNode(),
node.getColumnName(),
ignore_unknown_columns=ignore_unknown_columns,
)
else: else:
query_list = [] query_list = []
value_dict = {} value_dict = {}
...@@ -2151,7 +2155,12 @@ class Catalog(Folder, ...@@ -2151,7 +2155,12 @@ class Catalog(Folder,
value_dict.setdefault(subnode.getComparisonOperator(), value_dict.setdefault(subnode.getComparisonOperator(),
[]).append(wrap(_dequote(subnode.getValue()))) []).append(wrap(_dequote(subnode.getValue())))
else: else:
subquery = self._buildQueryFromAbstractSyntaxTreeNode(subnode, search_key, wrap) subquery = self._buildQueryFromAbstractSyntaxTreeNode(
subnode,
search_key,
wrap,
ignore_unknown_columns,
)
if subquery is not None: if subquery is not None:
append(subquery) append(subquery)
logical_operator = node.getLogicalOperator() logical_operator = node.getLogicalOperator()
...@@ -2169,7 +2178,7 @@ class Catalog(Folder, ...@@ -2169,7 +2178,7 @@ class Catalog(Folder,
result = None result = None
return result return result
def buildQueryFromAbstractSyntaxTreeNode(self, node, key, wrap=lambda x: x): def buildQueryFromAbstractSyntaxTreeNode(self, node, key, wrap=lambda x: x, ignore_unknown_columns=True):
""" """
Build a query from given Abstract Syntax Tree (AST) node by recursing in Build a query from given Abstract Syntax Tree (AST) node by recursing in
its childs. its childs.
...@@ -2189,8 +2198,11 @@ class Catalog(Folder, ...@@ -2189,8 +2198,11 @@ class Catalog(Folder,
search_key = SearchKeyWrapperForScriptableKey(key, script) search_key = SearchKeyWrapperForScriptableKey(key, script)
related_key_definition = None related_key_definition = None
if search_key is None: if search_key is None:
message = 'Unknown column ' + repr(key)
if not ignore_unknown_columns:
raise ValueError(message)
# Unknown, skip loudly # Unknown, skip loudly
LOG('SQLCatalog', WARNING, 'Unknown column %r, skipped.' % (key, )) LOG('SQLCatalog', WARNING, message)
result = None result = None
else: else:
if related_key_definition is None: if related_key_definition is None:
...@@ -2199,7 +2211,7 @@ class Catalog(Folder, ...@@ -2199,7 +2211,7 @@ class Catalog(Folder,
build_key = search_key.getSearchKey(sql_catalog=self, build_key = search_key.getSearchKey(sql_catalog=self,
related_key_definition=related_key_definition) related_key_definition=related_key_definition)
result = self._buildQueryFromAbstractSyntaxTreeNode(node, build_key, result = self._buildQueryFromAbstractSyntaxTreeNode(node, build_key,
wrap) wrap, ignore_unknown_columns)
if related_key_definition is not None: if related_key_definition is not None:
result = search_key.buildQuery(sql_catalog=self, result = search_key.buildQuery(sql_catalog=self,
related_key_definition=related_key_definition, related_key_definition=related_key_definition,
...@@ -2219,7 +2231,7 @@ class Catalog(Folder, ...@@ -2219,7 +2231,7 @@ class Catalog(Folder,
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)
def buildQuery(self, kw, ignore_empty_string=True, operator='and'): def buildQuery(self, kw, ignore_empty_string=True, operator='and', ignore_unknown_columns=True):
query_list = [] query_list = []
append = query_list.append append = query_list.append
# unknown_column_dict: contains all (key, value) pairs which could not be # unknown_column_dict: contains all (key, value) pairs which could not be
...@@ -2290,7 +2302,9 @@ class Catalog(Folder, ...@@ -2290,7 +2302,9 @@ class Catalog(Folder,
result = self.buildSingleQuery(key, raw_value, search_key_name) result = self.buildSingleQuery(key, raw_value, search_key_name)
else: else:
result = self.buildQueryFromAbstractSyntaxTreeNode( result = self.buildQueryFromAbstractSyntaxTreeNode(
abstract_syntax_tree, key, wrap) abstract_syntax_tree, key, wrap,
ignore_unknown_columns=ignore_unknown_columns,
)
else: else:
# Any other type, just create a query. (can be a DateTime, ...) # Any other type, just create a query. (can be a DateTime, ...)
result = self.buildSingleQuery(key, value) result = self.buildSingleQuery(key, value)
...@@ -2302,7 +2316,11 @@ class Catalog(Folder, ...@@ -2302,7 +2316,11 @@ class Catalog(Folder,
if len(empty_value_dict): if len(empty_value_dict):
LOG('SQLCatalog', WARNING, 'Discarding columns with empty values: %r' % (empty_value_dict, )) LOG('SQLCatalog', WARNING, 'Discarding columns with empty values: %r' % (empty_value_dict, ))
if len(unknown_column_dict): if len(unknown_column_dict):
LOG('SQLCatalog', WARNING, 'Unknown columns %r, skipped.' % (unknown_column_dict.keys(), )) message = 'Unknown columns ' + repr(unknown_column_dict.keys())
if ignore_unknown_columns:
LOG('SQLCatalog', WARNING, message)
else:
raise TypeError(message)
return ComplexQuery(query_list, logical_operator=operator, return ComplexQuery(query_list, logical_operator=operator,
unknown_column_dict=unknown_column_dict) unknown_column_dict=unknown_column_dict)
...@@ -2350,7 +2368,7 @@ class Catalog(Folder, ...@@ -2350,7 +2368,7 @@ class Catalog(Folder,
return order_by_list return order_by_list
def buildEntireQuery(self, kw, query_table='catalog', ignore_empty_string=1, def buildEntireQuery(self, kw, query_table='catalog', ignore_empty_string=1,
limit=None, extra_column_list=()): limit=None, extra_column_list=(), ignore_unknown_columns=True):
group_by_list = kw.pop('group_by_list', kw.pop('group_by', kw.pop('group_by_expression', ()))) group_by_list = kw.pop('group_by_list', kw.pop('group_by', kw.pop('group_by_expression', ())))
if isinstance(group_by_list, basestring): if isinstance(group_by_list, basestring):
group_by_list = [x.strip() for x in group_by_list.split(',')] group_by_list = [x.strip() for x in group_by_list.split(',')]
...@@ -2408,7 +2426,7 @@ class Catalog(Folder, ...@@ -2408,7 +2426,7 @@ class Catalog(Folder,
# new API. # new API.
order_by_override_list = kw.pop('select_expression_key', ()) order_by_override_list = kw.pop('select_expression_key', ())
return EntireQuery( return EntireQuery(
query=self.buildQuery(kw, ignore_empty_string=ignore_empty_string), query=self.buildQuery(kw, ignore_empty_string=ignore_empty_string, ignore_unknown_columns=ignore_unknown_columns),
order_by_list=order_by_list, order_by_list=order_by_list,
order_by_override_list=order_by_override_list, order_by_override_list=order_by_override_list,
group_by_list=group_by_list, group_by_list=group_by_list,
...@@ -2423,13 +2441,14 @@ class Catalog(Folder, ...@@ -2423,13 +2441,14 @@ class Catalog(Folder,
def buildSQLQuery(self, query_table='catalog', REQUEST=None, def buildSQLQuery(self, query_table='catalog', REQUEST=None,
ignore_empty_string=1, only_group_columns=False, ignore_empty_string=1, only_group_columns=False,
limit=None, extra_column_list=(), limit=None, extra_column_list=(),
**kw): ignore_unknown_columns=True, **kw):
return self.buildEntireQuery( return self.buildEntireQuery(
kw, kw,
query_table=query_table, query_table=query_table,
ignore_empty_string=ignore_empty_string, ignore_empty_string=ignore_empty_string,
limit=limit, limit=limit,
extra_column_list=extra_column_list, extra_column_list=extra_column_list,
ignore_unknown_columns=ignore_unknown_columns,
).asSQLExpression( ).asSQLExpression(
self, self,
only_group_columns, only_group_columns,
......
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