summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-06-22 12:19:41 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-06-22 12:19:41 -0400
commiteba9d1b58f38577fb71d3374a2133d6e9159c277 (patch)
tree15f7dd663b8dcbf98adcc15c065a937d327e1a89 /lib/sqlalchemy
parent38b54955118e6bb0f4e9664b50924a193f53e817 (diff)
downloadsqlalchemy-eba9d1b58f38577fb71d3374a2133d6e9159c277.tar.gz
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. [ticket:2744]
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/sql/expression.py7
-rw-r--r--lib/sqlalchemy/types.py35
2 files changed, 39 insertions, 3 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 1ceaa9191..46a08379b 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -2011,8 +2011,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
@@ -2054,7 +2056,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:
diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py
index bfff05362..2fefc348f 100644
--- a/lib/sqlalchemy/types.py
+++ b/lib/sqlalchemy/types.py
@@ -590,9 +590,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):
"""