From 69dcd805d2c6ed92adf81da443c5c06d23d3423a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 17 Apr 2011 15:37:12 -0400 Subject: - Added explicit true()/false() constructs to expression lib - coercion rules will intercept "False"/"True" into these constructs. In 0.6, the constructs were typically converted straight to string, which was no longer accepted in 0.7. [ticket:2117] --- lib/sqlalchemy/sql/expression.py | 54 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/sql/expression.py') diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index b7e543850..c552f055f 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -1046,12 +1046,25 @@ def over(func, partition_by=None, order_by=None): return _Over(func, partition_by=partition_by, order_by=order_by) def null(): - """Return a :class:`_Null` object, which compiles to ``NULL`` in a sql - statement. + """Return a :class:`_Null` object, which compiles to ``NULL``. """ return _Null() +def true(): + """Return a :class:`_True` object, which compiles to ``true``, or the + boolean equivalent for the target dialect. + + """ + return _True() + +def false(): + """Return a :class:`_False` object, which compiles to ``false``, or the + boolean equivalent for the target dialect. + + """ + return _False() + class _FunctionGenerator(object): """Generate :class:`.Function` objects based on getattr calls.""" @@ -1199,11 +1212,25 @@ def _literal_as_text(element): return element.__clause_element__() elif isinstance(element, basestring): return _TextClause(unicode(element)) + elif isinstance(element, (util.NoneType, bool)): + return _const_expr(element) else: raise exc.ArgumentError( "SQL expression object or string expected." ) +def _const_expr(element): + if element is None: + return null() + elif element is False: + return false() + elif element is True: + return true() + else: + raise exc.ArgumentError( + "Expected None, False, or True" + ) + def _clause_element_as_expr(element): if hasattr(element, '__clause_element__'): return element.__clause_element__() @@ -2840,10 +2867,31 @@ class _Null(ColumnElement): """ __visit_name__ = 'null' - def __init__(self): self.type = sqltypes.NULLTYPE +class _False(ColumnElement): + """Represent the ``false`` keyword in a SQL statement. + + Public constructor is the :func:`false()` function. + + """ + + __visit_name__ = 'false' + def __init__(self): + self.type = sqltypes.BOOLEANTYPE + +class _True(ColumnElement): + """Represent the ``true`` keyword in a SQL statement. + + Public constructor is the :func:`true()` function. + + """ + + __visit_name__ = 'true' + def __init__(self): + self.type = sqltypes.BOOLEANTYPE + class ClauseList(ClauseElement): """Describe a list of clauses, separated by an operator. -- cgit v1.2.1