SimpleQuery.py 5.31 KB
Newer Older
Ivan Tyagov's avatar
Ivan Tyagov committed
1 2
##############################################################################
#
3 4 5 6
# 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>
Ivan Tyagov's avatar
Ivan Tyagov committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# 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.
#
##############################################################################

31
from Query import Query
32
from Products.ZSQLCatalog.interfaces.query import IQuery
33 34
from Interface.Verify import verifyClass
from Products.ZSQLCatalog.SQLCatalog import profiler_decorator
35
from zLOG import LOG, WARNING
Ivan Tyagov's avatar
Ivan Tyagov committed
36

37
class SimpleQuery(Query):
Ivan Tyagov's avatar
Ivan Tyagov committed
38
  """
39 40
    A SimpleQuery represents a single comparison between a single column and
    one or more values.
Ivan Tyagov's avatar
Ivan Tyagov committed
41
  """
42
  @profiler_decorator
43
  def __init__(self, search_key=None, comparison_operator='=', group=None, **kw):
44 45 46 47
    """
      search_key (None, SearchKey instance)
        If given, the instance of SearchKey which is responsible for column
        map registration and rendering (SQL and SearchText).
48
      comparison_operator (string)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        The comparison operator which will be applied between column and
        values.
        See Operator/ComparisonOperator.py for possible values.
      group
        See ColumnMap.
      **kw
        Must contain exactly one item.
        item key (string)
          column name
        item value
          one or more values
    """
    self.search_key = search_key
    if len(kw) != 1:
      raise ValueError, 'SimpleQuery can support one and one only column. Got %r.' % (kw, )
    self.column, value = kw.popitem()
65
    # Usability improvement code (those changes should not be needed when
66 67
    # this Query is instanciated by a SearchKey, as operator should be correct
    # already).
68 69
    comparison_operator = comparison_operator.lower()
    if comparison_operator == 'in':
70 71 72 73 74
      if isinstance(value, (list, tuple)):
        if len(value) == 0:
          raise ValueError, 'Empty lists are not allowed.'
        elif len(value) == 1:
          value = value[0]
75
          comparison_operator = '='
76
      else:
77 78
        comparison_operator = '='
    elif comparison_operator == '=':
79 80 81 82 83 84
      if isinstance(value, (list, tuple)):
        if len(value) == 0:
          raise ValueError, 'Empty lists are not allowed.'
        elif len(value) == 1:
          value = value[0]
        else:
85
          comparison_operator = 'in'
86 87 88 89 90 91 92
    if value is None:
      if comparison_operator == '=':
        comparison_operator = 'is'
      elif comparison_operator != 'is':
        raise ValueError, 'None value with a non-"=" comparison_operator (%r). Not sure what to do.' % (comparison_operator, )
    elif comparison_operator == 'is':
      raise ValueError, 'Non-None value (%r) with "is" comparison_operator. Not sure what to do.' % (value, )
93
    self.value = value
94
    self.comparison_operator = comparison_operator
95
    self.group = group
Ivan Tyagov's avatar
Ivan Tyagov committed
96

97 98 99
  @profiler_decorator
  def asSearchTextExpression(self, sql_catalog, column=None):
    return self.getSearchKey(sql_catalog).buildSearchTextExpression(self.getOperator(sql_catalog), self.getValue(), column=column)
Ivan Tyagov's avatar
Ivan Tyagov committed
100

101 102 103 104 105
  @profiler_decorator
  def asSQLExpression(self, sql_catalog, column_map, only_group_columns):
    return self.getSearchKey(sql_catalog).buildSQLExpression(
      self.getOperator(sql_catalog), self.getValue(),
      column_map, only_group_columns, group=self.group)
Ivan Tyagov's avatar
Ivan Tyagov committed
106

107 108 109
  @profiler_decorator
  def registerColumnMap(self, sql_catalog, column_map):
    self.group = self.getSearchKey(sql_catalog).registerColumnMap(column_map, group=self.group, simple_query=self)
Ivan Tyagov's avatar
Ivan Tyagov committed
110

111
  def getOperator(self, sql_catalog):
Ivan Tyagov's avatar
Ivan Tyagov committed
112
    """
113
      Return an instance of OperatorBase class.
Ivan Tyagov's avatar
Ivan Tyagov committed
114
    """
115
    return sql_catalog.getComparisonOperator(self.comparison_operator)
Ivan Tyagov's avatar
Ivan Tyagov committed
116

117
  def getSearchKey(self, sql_catalog):
Ivan Tyagov's avatar
Ivan Tyagov committed
118
    """
119
      Return an instance of SearchKey class.
Ivan Tyagov's avatar
Ivan Tyagov committed
120
    """
121 122 123
    if self.search_key is None:
      self.search_key = sql_catalog.getSearchKey(self.getColumn())
    return self.search_key
Ivan Tyagov's avatar
Ivan Tyagov committed
124

125 126
  def getColumn(self):
    return self.column
127

128 129
  def getValue(self):
    return self.value
Ivan Tyagov's avatar
Ivan Tyagov committed
130

131
  def __repr__(self):
132
    return '<%s %r %s %r>' % (self.__class__.__name__, self.getColumn(), self.comparison_operator, self.getValue())
133 134 135

  def setGroup(self, group):
    self.group = group
Ivan Tyagov's avatar
Ivan Tyagov committed
136

137
verifyClass(IQuery, SimpleQuery)
Ivan Tyagov's avatar
Ivan Tyagov committed
138