summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/expression.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-04-22 19:12:47 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-04-22 19:12:47 -0400
commit5884c2e7e5b46cee29b90aa3f7161e7380e3e2a5 (patch)
treeed8a37934eb00d4c01bfeec117ea7b1bb980db68 /lib/sqlalchemy/sql/expression.py
parent63c211f42730011760aa8e3f88b2171b23bc0a60 (diff)
downloadsqlalchemy-5884c2e7e5b46cee29b90aa3f7161e7380e3e2a5.tar.gz
Fully implemented the IS and IS NOT operators with
regards to the True/False constants. An expression like ``col.is_(True)`` will now render ``col IS true`` on the target platform, rather than converting the True/ False constant to an integer bound parameter. This allows the ``is_()`` operator to work on MySQL when given True/False constants. [ticket:2682]
Diffstat (limited to 'lib/sqlalchemy/sql/expression.py')
-rw-r--r--lib/sqlalchemy/sql/expression.py46
1 files changed, 34 insertions, 12 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 28b1c6ddd..d2e644ce2 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -1591,7 +1591,9 @@ def _interpret_as_from(element):
def _const_expr(element):
- if element is None:
+ if isinstance(element, (Null, False_, True_)):
+ return element
+ elif element is None:
return null()
elif element is False:
return false()
@@ -2011,18 +2013,33 @@ class _DefaultColumnComparator(operators.ColumnOperators):
return op, other_comparator.type
def _boolean_compare(self, expr, op, obj, negate=None, reverse=False,
- **kwargs
- ):
- if obj is None or isinstance(obj, Null):
- if op in (operators.eq, operators.is_):
- return BinaryExpression(expr, null(), operators.is_,
- negate=operators.isnot)
- elif op in (operators.ne, operators.isnot):
- return BinaryExpression(expr, null(), operators.isnot,
- negate=operators.is_)
+ **kwargs):
+ if isinstance(obj, (util.NoneType, bool, Null, True_, False_)):
+
+ # allow x ==/!= True/False to be treated as a literal.
+ # this comes out to "== / != true/false" or "1/0" if those
+ # constants aren't supported and works on all platforms
+ if op in (operators.eq, operators.ne) and \
+ isinstance(obj, (bool, True_, False_)):
+ return BinaryExpression(expr,
+ obj,
+ op,
+ type_=sqltypes.BOOLEANTYPE,
+ negate=negate, modifiers=kwargs)
else:
- raise exc.ArgumentError("Only '='/'!=' operators can "
- "be used with NULL")
+ # all other None/True/False uses IS, IS NOT
+ if op in (operators.eq, operators.is_):
+ return BinaryExpression(expr, _const_expr(obj),
+ operators.is_,
+ negate=operators.isnot)
+ elif op in (operators.ne, operators.isnot):
+ return BinaryExpression(expr, _const_expr(obj),
+ operators.isnot,
+ negate=operators.is_)
+ else:
+ raise exc.ArgumentError(
+ "Only '=', '!=', 'is_()', 'isnot()' operators can "
+ "be used with None/True/False")
else:
obj = self._check_literal(expr, op, obj)
@@ -3253,6 +3270,8 @@ class False_(ColumnElement):
def __init__(self):
self.type = sqltypes.BOOLEANTYPE
+ def compare(self, other):
+ return isinstance(other, False_)
class True_(ColumnElement):
"""Represent the ``true`` keyword in a SQL statement.
@@ -3266,6 +3285,9 @@ class True_(ColumnElement):
def __init__(self):
self.type = sqltypes.BOOLEANTYPE
+ def compare(self, other):
+ return isinstance(other, True_)
+
class ClauseList(ClauseElement):
"""Describe a list of clauses, separated by an operator.