]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Provided a new attribute for :class:`.TypeDecorator`
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 22 Jun 2013 16:19:41 +0000 (12:19 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 22 Jun 2013 16:20:20 +0000 (12:20 -0400)
called :attr:`.TypeDecorator.coerce_to_is_types`,
to make it easier to control how comparisons using
``==`` or ``!=`` to ``None`` and boolean types goes
about producing an ``IS`` expression, or a plain
equality expression with a bound parameter.
[ticket:2744]

Conflicts:
doc/build/changelog/changelog_09.rst

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/sql/expression.py
lib/sqlalchemy/types.py
test/sql/test_types.py

index 5f305fe8f2ee3c6eccba8959f8ac9330817c2229..52cf908498f903930cbdbd2ecbc29398ba5fd8c9 100644 (file)
@@ -6,6 +6,17 @@
 .. changelog::
     :version: 0.8.2
 
+    .. change::
+        :tags: feature, sql
+        :tickets: 2744, 2734
+
+        Provided a new attribute for :class:`.TypeDecorator`
+        called :attr:`.TypeDecorator.coerce_to_is_types`,
+        to make it easier to control how comparisons using
+        ``==`` or ``!=`` to ``None`` and boolean types goes
+        about producing an ``IS`` expression, or a plain
+        equality expression with a bound parameter.
+
     .. change::
         :tags: feature, postgresql
 
index 3f9aef2b7835052e8cbc63d9713cdbdd13457a27..7e893db27b9468300a655aea82b02aa568241b9b 100644 (file)
@@ -2013,8 +2013,10 @@ class _DefaultColumnComparator(operators.ColumnOperators):
         return op, other_comparator.type
 
     def _boolean_compare(self, expr, op, obj, negate=None, reverse=False,
+                        _python_is_types=(util.NoneType, bool),
                         **kwargs):
-        if isinstance(obj, (util.NoneType, bool, Null, True_, False_)):
+
+        if isinstance(obj, _python_is_types + (Null, True_, False_)):
 
             # allow x ==/!= True/False to be treated as a literal.
             # this comes out to "== / != true/false" or "1/0" if those
@@ -2056,7 +2058,8 @@ class _DefaultColumnComparator(operators.ColumnOperators):
                             type_=sqltypes.BOOLEANTYPE,
                             negate=negate, modifiers=kwargs)
 
-    def _binary_operate(self, expr, op, obj, reverse=False, result_type=None):
+    def _binary_operate(self, expr, op, obj, reverse=False, result_type=None,
+                            **kw):
         obj = self._check_literal(expr, op, obj)
 
         if reverse:
index 46cf9e2a145681d08c54640f7a1dff48c969e560..7ab3207bfa5cb5301deb6f5b97033e606ab8bb98 100644 (file)
@@ -591,9 +591,42 @@ class TypeDecorator(TypeEngine):
                                  "type being decorated")
         self.impl = to_instance(self.__class__.impl, *args, **kwargs)
 
+    coerce_to_is_types = (util.NoneType, )
+    """Specify those Python types which should be coerced at the expression
+    level to "IS <constant>" when compared using ``==`` (and same for
+        ``IS NOT`` in conjunction with ``!=``.
+
+    For most SQLAlchemy types, this includes ``NoneType``, as well as ``bool``.
+
+    :class:`.TypeDecorator` modifies this list to only include ``NoneType``,
+    as typedecorator implementations that deal with boolean types are common.
+
+    Custom :class:`.TypeDecorator` classes can override this attribute to
+    return an empty tuple, in which case no values will be coerced to
+    constants.
+
+    ..versionadded:: 0.8.2
+        Added :attr:`.TypeDecorator.coerce_to_is_types` to allow for easier
+        control of ``__eq__()`` ``__ne__()`` operations.
+
+    """
+
+    class Comparator(TypeEngine.Comparator):
+        def operate(self, op, *other, **kwargs):
+            kwargs['_python_is_types'] = self.expr.type.coerce_to_is_types
+            return super(TypeDecorator.Comparator, self).operate(
+                                                        op, *other, **kwargs)
+
+        def reverse_operate(self, op, other, **kwargs):
+            kwargs['_python_is_types'] = self.expr.type.coerce_to_is_types
+            return super(TypeDecorator.Comparator, self).reverse_operate(
+                                                        op, other, **kwargs)
+
     @property
     def comparator_factory(self):
-        return self.impl.comparator_factory
+        return type("TDComparator",
+                    (TypeDecorator.Comparator, self.impl.comparator_factory),
+                    {})
 
     def _gen_dialect_impl(self, dialect):
         """
index 64dbb6204322379bd86d03d54c4f9a3be90850f8..f80eb7134972c95b70f410413168a61e3f73e99a 100644 (file)
@@ -1106,7 +1106,6 @@ class ExpressionTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiled
         expr = column('foo', CHAR) == "asdf"
         eq_(expr.right.type.__class__, CHAR)
 
-
     @testing.uses_deprecated
     @testing.fails_on('firebird', 'Data type unknown on the parameter')
     @testing.fails_on('mssql', 'int is unsigned ?  not clear')
@@ -1149,6 +1148,39 @@ class ExpressionTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiled
             "BIND_INfooBIND_INhiBIND_OUT"
         )
 
+    def test_typedec_is_adapt(self):
+        class CoerceNothing(TypeDecorator):
+            coerce_to_is_types = ()
+            impl = Integer
+        class CoerceBool(TypeDecorator):
+            coerce_to_is_types = (bool, )
+            impl = Boolean
+        class CoerceNone(TypeDecorator):
+            coerce_to_is_types = (type(None),)
+            impl = Integer
+
+        c1 = column('x', CoerceNothing())
+        c2 = column('x', CoerceBool())
+        c3 = column('x', CoerceNone())
+
+        self.assert_compile(
+            and_(c1 == None, c2 == None, c3 == None),
+            "x = :x_1 AND x = :x_2 AND x IS NULL"
+        )
+        self.assert_compile(
+            and_(c1 == True, c2 == True, c3 == True),
+            "x = :x_1 AND x = true AND x = :x_2"
+        )
+        self.assert_compile(
+            and_(c1 == 3, c2 == 3, c3 == 3),
+            "x = :x_1 AND x = :x_2 AND x = :x_3"
+        )
+        self.assert_compile(
+            and_(c1.is_(True), c2.is_(True), c3.is_(True)),
+            "x IS :x_1 AND x IS true AND x IS :x_2"
+        )
+
+
     def test_typedec_righthand_coercion(self):
         class MyTypeDec(types.TypeDecorator):
             impl = String