summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-09-14 17:46:54 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-09-14 17:46:54 -0400
commit9d7158a2c3869ad7a1ab07d3a41e831f6806a68c (patch)
tree2464edb66a6bea6fa5a2956dbcd179772c999de6 /lib/sqlalchemy/sql
parent904466a29320844ccc164bad4699198d3916159d (diff)
parent46196ea723484f354ac17204ccd489004baaac95 (diff)
downloadsqlalchemy-9d7158a2c3869ad7a1ab07d3a41e831f6806a68c.tar.gz
merge tip
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/expression.py42
-rw-r--r--lib/sqlalchemy/sql/util.py25
2 files changed, 61 insertions, 6 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index d184816ab..1b1cfee8a 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -1269,10 +1269,15 @@ class ClauseElement(Visitable):
return engine
else:
return None
-
+
+ @util.pending_deprecation('0.7',
+ 'Only SQL expressions which subclass '
+ ':class:`.Executable` may provide the '
+ ':func:`.execute` method.')
def execute(self, *multiparams, **params):
- """Compile and execute this :class:`ClauseElement`."""
-
+ """Compile and execute this :class:`ClauseElement`.
+
+ """
e = self.bind
if e is None:
label = getattr(self, 'description', self.__class__.__name__)
@@ -1284,9 +1289,13 @@ class ClauseElement(Visitable):
raise exc.UnboundExecutionError(msg)
return e._execute_clauseelement(self, multiparams, params)
+ @util.pending_deprecation('0.7',
+ 'Only SQL expressions which subclass '
+ ':class:`.Executable` may provide the '
+ ':func:`.scalar` method.')
def scalar(self, *multiparams, **params):
- """Compile and execute this :class:`ClauseElement`, returning the
- result's scalar representation.
+ """Compile and execute this :class:`ClauseElement`, returning
+ the result's scalar representation.
"""
return self.execute(*multiparams, **params).scalar()
@@ -2401,7 +2410,7 @@ class Executable(_Generative):
COMMIT will be invoked in order to provide its "autocommit" feature.
Typically, all INSERT/UPDATE/DELETE statements as well as
CREATE/DROP statements have autocommit behavior enabled; SELECT
- constructs do not. Use this option when invokving a SELECT or other
+ constructs do not. Use this option when invoking a SELECT or other
specific SQL construct where COMMIT is desired (typically when
calling stored procedures and such).
@@ -2436,6 +2445,27 @@ class Executable(_Generative):
"""
self._execution_options = self._execution_options.union(kw)
+ def execute(self, *multiparams, **params):
+ """Compile and execute this :class:`.Executable`."""
+
+ e = self.bind
+ if e is None:
+ label = getattr(self, 'description', self.__class__.__name__)
+ msg = ('This %s is not bound and does not support direct '
+ 'execution. Supply this statement to a Connection or '
+ 'Engine for execution. Or, assign a bind to the statement '
+ 'or the Metadata of its underlying tables to enable '
+ 'implicit execution via this method.' % label)
+ raise exc.UnboundExecutionError(msg)
+ return e._execute_clauseelement(self, multiparams, params)
+
+ def scalar(self, *multiparams, **params):
+ """Compile and execute this :class:`.Executable`, returning the
+ result's scalar representation.
+
+ """
+ return self.execute(*multiparams, **params).scalar()
+
# legacy, some outside users may be calling this
_Executable = Executable
diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py
index c999ab786..bd4f70247 100644
--- a/lib/sqlalchemy/sql/util.py
+++ b/lib/sqlalchemy/sql/util.py
@@ -92,6 +92,31 @@ def find_columns(clause):
visitors.traverse(clause, {}, {'column':cols.add})
return cols
+def bind_values(clause):
+ """Return an ordered list of "bound" values in the given clause.
+
+ E.g.::
+
+ >>> expr = and_(
+ ... table.c.foo==5, table.c.foo==7
+ ... )
+ >>> bind_values(expr)
+ [5, 7]
+ """
+
+ v = []
+ def visit_bindparam(bind):
+ value = bind.value
+
+ # evaluate callables
+ if callable(value):
+ value = value()
+
+ v.append(value)
+
+ visitors.traverse(clause, {}, {'bindparam':visit_bindparam})
+ return v
+
def _quote_ddl_expr(element):
if isinstance(element, basestring):
element = element.replace("'", "''")