diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-04-17 15:37:12 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-04-17 15:37:12 -0400 |
| commit | 69dcd805d2c6ed92adf81da443c5c06d23d3423a (patch) | |
| tree | 9368e15176efa0d6b5a04164ff0558c7d9ae0ae1 /lib/sqlalchemy | |
| parent | fbcfd079debf27665f23a996853903aa3b2ef23a (diff) | |
| download | sqlalchemy-69dcd805d2c6ed92adf81da443c5c06d23d3423a.tar.gz | |
- 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]
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/expression.py | 54 |
2 files changed, 58 insertions, 4 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index fe04da1a1..82c1748cc 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -426,9 +426,15 @@ class SQLCompiler(engine.Compiled): self.post_process_text(textclause.text)) ) - def visit_null(self, null, **kwargs): + def visit_null(self, expr, **kw): return 'NULL' + def visit_true(self, expr, **kw): + return 'true' + + def visit_false(self, expr, **kw): + return 'false' + def visit_clauselist(self, clauselist, **kwargs): sep = clauselist.operator if sep is None: 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. |
