From: Mike Bayer Date: Wed, 31 Aug 2016 18:34:54 +0000 (-0400) Subject: Check for supports_execution at ClauseElement base X-Git-Tag: rel_1_1_0~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c7b9c84312b6b252e68ea704670d0ea7fc0042f0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Check for supports_execution at ClauseElement base Raise a more descriptive exception / message when ClauseElement or non-SQLAlchemy objects that are not "executable" are erroneously passed to ``.execute()``; a new exception ObjectNotExecutableError is raised consistently in all cases. Change-Id: I2dd393121e2c7e5b6b9e40286a2f25670876e8e4 Fixes: #3786 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index ba967f9762..88f1ebb244 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,15 @@ .. changelog:: :version: 1.1.0 + .. change:: + :tags: bug, sql + :tickets: 3786 + + Raise a more descriptive exception / message when ClauseElement + or non-SQLAlchemy objects that are not "executable" are erroneously + passed to ``.execute()``; a new exception ObjectNotExecutableError + is raised consistently in all cases. + .. change:: :tags: bug, orm :tickets: 3776 diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index ff855a5390..83f0f0c83a 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -940,9 +940,7 @@ class Connection(Connectable): try: meth = object._execute_on_connection except AttributeError: - raise exc.InvalidRequestError( - "Unexecutable object type: %s" % - type(object)) + raise exc.ObjectNotExecutableError(object) else: return meth(self, multiparams, params) diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 1bb575984a..bd6e37d6ca 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -554,8 +554,9 @@ class DefaultExecutionContext(interfaces.ExecutionContext): self.compiled = compiled - if not compiled.can_execute: - raise exc.ArgumentError("Not an executable clause") + # this should be caught in the engine before + # we get here + assert compiled.can_execute self.execution_options = compiled.statement._execution_options.union( connection._execution_options) diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index 2729842293..2a7f84de33 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -26,6 +26,20 @@ class ArgumentError(SQLAlchemyError): """ +class ObjectNotExecutableError(ArgumentError): + """Raised when an object is passed to .execute() that can't be + executed as SQL. + + .. versionadded:: 1.1 + + """ + + def __init__(self, target): + super(ObjectNotExecutableError, self).__init__( + "Not an executable object: %r" % target + ) + + class NoSuchModuleError(ArgumentError): """Raised when a dynamically-loaded module (usually a database dialect) of a particular name cannot be located.""" diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 095c84f03b..85d5ff6da8 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -215,7 +215,10 @@ class Compiled(object): pass def _execute_on_connection(self, connection, multiparams, params): - return connection._execute_compiled(self, multiparams, params) + if self.can_execute: + return connection._execute_compiled(self, multiparams, params) + else: + raise exc.ObjectNotExecutableError(self.statement) @property def sql_compiler(self): diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index e277b28a4c..75d5368d58 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -259,7 +259,10 @@ class ClauseElement(Visitable): return self def _execute_on_connection(self, connection, multiparams, params): - return connection._execute_clauseelement(self, multiparams, params) + if self.supports_execution: + return connection._execute_clauseelement(self, multiparams, params) + else: + raise exc.ObjectNotExecutableError(self) def unique_params(self, *optionaldict, **kwargs): """Return a copy with :func:`bindparam()` elements replaced. diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index e364b2e7f2..98a96fd56f 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -71,9 +71,6 @@ class SchemaItem(SchemaEventTarget, visitors.Visitable): __visit_name__ = 'schema_item' - def _execute_on_connection(self, connection, multiparams, params): - return connection._execute_default(self, multiparams, params) - def _init_items(self, *args): """Initialize the list of child items for this SchemaItem.""" @@ -1941,6 +1938,9 @@ class DefaultGenerator(_NotAColumnExpr, SchemaItem): bind = _bind_or_error(self) return bind._execute_default(self, **kwargs) + def _execute_on_connection(self, connection, multiparams, params): + return connection._execute_default(self, multiparams, params) + @property def bind(self): """Return the connectable associated with this default.""" diff --git a/test/engine/test_execute.py b/test/engine/test_execute.py index 8e553307fd..49b29f7f21 100644 --- a/test/engine/test_execute.py +++ b/test/engine/test_execute.py @@ -306,6 +306,26 @@ class ExecuteTest(fixtures.TestBase): finally: conn.close() + def test_not_an_executable(self): + for obj in ( + Table("foo", MetaData(), Column("x", Integer)), + Column('x', Integer), + tsa.and_(), + column('foo'), + tsa.and_().compile(), + column('foo').compile(), + MetaData(), + Integer(), + tsa.Index(name='foo'), + tsa.UniqueConstraint('x') + ): + with testing.db.connect() as conn: + assert_raises_message( + tsa.exc.ObjectNotExecutableError, + "Not an executable object", + conn.execute, obj + ) + def test_stmt_exception_non_ascii(self): name = util.u('méil') with testing.db.connect() as conn: