From: Mike Bayer Date: Fri, 12 Jul 2013 15:32:34 +0000 (-0400) Subject: Fixed bug where the expression system relied upon the ``str()`` X-Git-Tag: rel_0_8_3~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=574dba8e61f5f86e0c809c0c15640379a8847533;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixed bug where the expression system relied upon the ``str()`` form of a some expressions when referring to the ``.c`` collection on a ``select()`` construct, but the ``str()`` form isn't available since the element relies on dialect-specific compilation constructs, notably the ``__getitem__()`` operator as used with a Postgresql ``ARRAY`` element. The fix also adds a new exception class :class:`.UnsupportedCompilationError` which is raised in those cases where a compiler is asked to compile something it doesn't know how to. [ticket:2780] --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 145eb14971..a7d28430a0 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -6,6 +6,22 @@ .. changelog:: :version: 0.8.3 + .. change:: + :tags: bug, sql, postgresql + :tickets: 2780 + + Fixed bug where the expression system relied upon the ``str()`` + form of a some expressions when referring to the ``.c`` collection + on a ``select()`` construct, but the ``str()`` form isn't available + since the element relies on dialect-specific compilation constructs, + notably the ``__getitem__()`` operator as used with a Postgresql + ``ARRAY`` element. The fix also adds a new exception class + :class:`.UnsupportedCompilationError` which is raised in those cases + where a compiler is asked to compile something it doesn't know + how to. + + .. change:: + :tags: bug, engine, oracle :tickets: 2776 Dialect.initialize() is not called a second time if an :class:`.Engine` diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index 0ce5393bee..215bd45d70 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -72,6 +72,18 @@ class CircularDependencyError(SQLAlchemyError): class CompileError(SQLAlchemyError): """Raised when an error occurs during SQL compilation""" +class UnsupportedCompilationError(CompileError): + """Raised when an operation is not supported by the given compiler. + + + .. versionadded:: 0.8.3 + + """ + + def __init__(self, compiler, element_type): + super(UnsupportedCompilationError, self).__init__( + "Compiler %r can't render element of type %s" % + (compiler, element_type)) class IdentifierError(SQLAlchemyError): """Raised when a schema name is beyond the max character limit""" diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index d7430bc3ac..85e4916ffd 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -662,8 +662,12 @@ class SQLCompiler(engine.Compiled): if disp: return disp(binary, operator, **kw) else: - return self._generate_generic_binary(binary, - OPERATORS[operator], **kw) + try: + opstring = OPERATORS[operator] + except KeyError: + raise exc.UnsupportedCompilationError(self, operator) + else: + return self._generate_generic_binary(binary, opstring, **kw) def visit_custom_op_binary(self, element, operator, **kw): return self._generate_generic_binary(element, diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 67b3b80191..7cb1326e2f 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -2362,7 +2362,10 @@ class ColumnElement(ClauseElement, ColumnOperators): """ if name is None: name = self.anon_label - key = str(self) + try: + key = str(self) + except exc.UnsupportedCompilationError: + key = self.anon_label else: key = name co = ColumnClause(_as_truncated(name) if name_is_truncatable else name, diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index f1dbb9e32f..3fb90e0d65 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -26,6 +26,7 @@ http://techspot.zzzeek.org/2008/01/23/expression-transformations/ from collections import deque from .. import util import operator +from .. import exc __all__ = ['VisitableType', 'Visitable', 'ClauseVisitor', 'CloningVisitor', 'ReplacingCloningVisitor', 'iterate', @@ -71,14 +72,24 @@ def _generate_dispatch(cls): getter = operator.attrgetter("visit_%s" % visit_name) def _compiler_dispatch(self, visitor, **kw): - return getter(visitor)(self, **kw) + try: + meth = getter(visitor) + except AttributeError: + raise exc.UnsupportedCompilationError(visitor, cls) + else: + return meth(self, **kw) else: # The optimization opportunity is lost for this case because the # __visit_name__ is not yet a string. As a result, the visit # string has to be recalculated with each compilation. def _compiler_dispatch(self, visitor, **kw): visit_attr = 'visit_%s' % self.__visit_name__ - return getattr(visitor, visit_attr)(self, **kw) + try: + meth = getattr(visitor, visit_attr) + except AttributeError: + raise exc.UnsupportedCompilationError(visitor, cls) + else: + return meth(self, **kw) _compiler_dispatch.__doc__ = \ """Look for an attribute named "visit_" + self.__visit_name__ diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 125d08a0d1..7736d2335d 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -2409,6 +2409,46 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ) +class UnsupportedTest(fixtures.TestBase): + def test_unsupported_element_str_visit_name(self): + from sqlalchemy.sql.expression import ClauseElement + class SomeElement(ClauseElement): + __visit_name__ = 'some_element' + + assert_raises_message( + exc.UnsupportedCompilationError, + r"Compiler ", + SomeElement().compile + ) + + def test_unsupported_element_meth_visit_name(self): + from sqlalchemy.sql.expression import ClauseElement + class SomeElement(ClauseElement): + @classmethod + def __visit_name__(cls): + return "some_element" + + assert_raises_message( + exc.UnsupportedCompilationError, + r"Compiler ", + SomeElement().compile + ) + + def test_unsupported_operator(self): + from sqlalchemy.sql.expression import BinaryExpression + def myop(x, y): + pass + binary = BinaryExpression(column("foo"), column("bar"), myop) + assert_raises_message( + exc.UnsupportedCompilationError, + r"Compiler