AutoQuery.py 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
##############################################################################
#
# Copyright (c) 2002-2006 Nexedi SARL and Contributors. All Rights Reserved.
# Copyright (c) 2007-2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
#                    Vincent Pelletier <vincent@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from Query import Query
from zLOG import LOG
33
from Products.ZSQLCatalog.interfaces.query import IQuery
34
from zope.interface.verify import verifyClass
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
from Products.ZSQLCatalog.SQLCatalog import profiler_decorator

class AutoQuery(Query):
  """
    An AutoQuery is a compatibility layer for former Query class.
    It passes parameters given at instantiation time to SQLCatalog's
    buildQuery or buildSingleQuery, and wraps resulting Query instance (proxy
    behaviour).

    This is only here for backward compatibility, and use is strongly
    discouraged. Use SQLCatalog API instead.
  """
  wrapped_query = None

  @profiler_decorator
  def __init__(self, *args, **kw):
    """
      Note: "operator" might contain a logical or a comparison operator.
    """
    if len(args):
      LOG('AutoQuery', 100, 'Got extra positional parameters (will be ignored): %r' % (args, ))
    self.table_alias_list = kw.pop('table_alias_list', None)
    self.kw = kw
    operator = kw.pop('operator', None)
    if isinstance(operator, basestring):
      operator = operator.lower()
    self.operator = operator
    self.ignore_empty_string = kw.pop('ignore_empty_string', True)
    if 'key' in kw and len(kw) > 2:
      raise ValueError, '"key" parameter cannot be used when more than one column is given. key=%r' % (self.search_key, )
    self.search_key = kw.pop('key', None)

  @profiler_decorator
  def _createWrappedQuery(self, sql_catalog):
    """
      Create wrapped query. This requires being able to reach catalog, since
      we use it as a query producer.
    """
    kw = self.kw
    operator = self.operator
    if 'range' in kw:
      # If we received a range parameter we are building a single query.
      # Recreate value as a dict and pass it to buildSingleQuery.
      range = kw.pop('range')
      assert len(kw) == 1, repr(kw)
      key, value = kw.items()[0]
      query = sql_catalog.buildSingleQuery(key, {'query': value,
                                                 'range': range})
    elif operator == 'in':
      # 'in' is a *comparison* operator, not a logical operator.
      # Transform kw into the proper form.
      assert len(kw) == 1, repr(kw)
      key, value = kw.items()[0]
      query = sql_catalog.buildSingleQuery(key, {'query': value,
                                                 'operator': operator})
    elif len(kw) == 1 and isinstance(kw.values()[0], (tuple, list)) and \
       operator in ('and', 'or'):
      # If there is only one parameter, and operator was given and is a
      # known logical operator, then operator will apply to it.
      # For example (from testDomainTool):
      #  kw = {'portal_type': ['!=a', '!=b'], 'operator': 'AND'}
      #  In such case, expected result is
      #  "portal_type!='a' AND portal_type!='b'"
      key, value = kw.items()[0]
      query = sql_catalog.buildSingleQuery(key, value, logical_operator=operator)
    else:
      # Otherwise, the operator will apply to the relationship between
      # parameters.
      if operator is None:
        operator = 'and'
      if self.search_key is not None:
        key, value = kw.items()[0]
        kw = {key: {'query': value, 'key': self.search_key}}
      query = sql_catalog.buildQuery(kw, operator=operator, ignore_empty_string=self.ignore_empty_string)
    if self.table_alias_list is not None:
      query.setTableAliasList(self.table_alias_list)
    self.wrapped_query = query

  @profiler_decorator
114 115 116 117 118 119
  def _asSearchTextExpression(self, sql_catalog, column=None):
    if self.wrapped_query is None:
      self._createWrappedQuery(sql_catalog)
    return self.wrapped_query._asSearchTextExpression(sql_catalog, column=column)

  @profiler_decorator
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  def asSearchTextExpression(self, sql_catalog, column=None):
    if self.wrapped_query is None:
      self._createWrappedQuery(sql_catalog)
    return self.wrapped_query.asSearchTextExpression(sql_catalog, column=column)

  @profiler_decorator
  def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
    if self.wrapped_query is None:
      self._createWrappedQuery(sql_catalog)
    return self.wrapped_query.asSQLExpression(sql_catalog, column_map, only_group_columns=only_group_columns)

  @profiler_decorator
  def registerColumnMap(self, sql_catalog, column_map):
    if self.wrapped_query is None:
      self._createWrappedQuery(sql_catalog)
    return self.wrapped_query.registerColumnMap(sql_catalog, column_map)

  def __repr__(self):
    if self.wrapped_query is None:
      result = '<%s(**%r) at %s>' % (self.__class__.__name__, self.kw, id(self))
    else:
      result = '<%s %r>' % (self.__class__.__name__, self.wrapped_query)
    return result

verifyClass(IQuery, AutoQuery)