diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-29 11:11:17 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-29 11:14:28 -0400 |
commit | acd8b1c107d536669ca7200edbca0f039f1e79fb (patch) | |
tree | 8b18ab41e44dc4f5c80f11229b9d28a1f6a9f999 | |
parent | 7c74d702a9632a8c7264d6972e46985de3fb2487 (diff) | |
download | sqlalchemy-acd8b1c107d536669ca7200edbca0f039f1e79fb.tar.gz |
Ensure @compiles calls down to the original compilation scheme
Made a slight behavioral change in the ``sqlalchemy.ext.compiler``
extension, whereby the existing compilation schemes for an established
construct would be removed if that construct was itself didn't already
have its own dedicated ``__visit_name__``. This was a
rare occurrence in 1.0, however in 1.1 :class:`.postgresql.ARRAY`
subclasses :class:`.sqltypes.ARRAY` and has this behavior.
As a result, setting up a compilation handler for another dialect
such as SQLite would render the main :class:`.postgresql.ARRAY`
object no longer compilable.
Fixes: #3732
Change-Id: If2c1ada4eeb09157885888e41f529173902f2b49
-rw-r--r-- | doc/build/changelog/changelog_11.rst | 14 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/compiler.py | 17 | ||||
-rw-r--r-- | test/ext/test_compiler.py | 51 |
3 files changed, 76 insertions, 6 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 9ae17fde0..7fea05d4d 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -22,6 +22,20 @@ :version: 1.1.0b2 .. change:: + :tags: bug, ext, postgresql + :tickets: 3732 + + Made a slight behavioral change in the ``sqlalchemy.ext.compiler`` + extension, whereby the existing compilation schemes for an established + construct would be removed if that construct was itself didn't already + have its own dedicated ``__visit_name__``. This was a + rare occurrence in 1.0, however in 1.1 :class:`.postgresql.ARRAY` + subclasses :class:`.sqltypes.ARRAY` and has this behavior. + As a result, setting up a compilation handler for another dialect + such as SQLite would render the main :class:`.postgresql.ARRAY` + object no longer compilable. + + .. change:: :tags: bug, sql :tickets: 3730 diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index 86156be1f..5ef4e1d2a 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -410,13 +410,25 @@ def compiles(class_, *specs): given :class:`.ClauseElement` type.""" def decorate(fn): + # get an existing @compiles handler existing = class_.__dict__.get('_compiler_dispatcher', None) - existing_dispatch = class_.__dict__.get('_compiler_dispatch') + + # get the original handler. All ClauseElement classes have one + # of these, but some TypeEngine classes will not. + existing_dispatch = getattr(class_, '_compiler_dispatch', None) + if not existing: existing = _dispatcher() if existing_dispatch: - existing.specs['default'] = existing_dispatch + def _wrap_existing_dispatch(element, compiler, **kw): + try: + return existing_dispatch(element, compiler, **kw) + except exc.UnsupportedCompilationError: + raise exc.CompileError( + "%s construct has no default " + "compilation handler." % type(element)) + existing.specs['default'] = _wrap_existing_dispatch # TODO: why is the lambda needed ? setattr(class_, '_compiler_dispatch', @@ -458,4 +470,5 @@ class _dispatcher(object): raise exc.CompileError( "%s construct has no default " "compilation handler." % type(element)) + return fn(element, compiler, **kw) diff --git a/test/ext/test_compiler.py b/test/ext/test_compiler.py index f381ca185..02b9f3a43 100644 --- a/test/ext/test_compiler.py +++ b/test/ext/test_compiler.py @@ -2,15 +2,17 @@ from sqlalchemy import * from sqlalchemy.types import TypeEngine from sqlalchemy.sql.expression import ClauseElement, ColumnClause,\ FunctionElement, Select, \ - BindParameter + BindParameter, ColumnElement from sqlalchemy.schema import DDLElement, CreateColumn, CreateTable from sqlalchemy.ext.compiler import compiles, deregister from sqlalchemy import exc -from sqlalchemy.sql import table, column, visitors +from sqlalchemy.testing import eq_ +from sqlalchemy.sql import table, column from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import fixtures, AssertsCompiledSQL + class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = 'default' @@ -123,7 +125,7 @@ class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL): "FROM mytable WHERE mytable.x > :x_1)" ) - def test_no_default_message(self): + def test_no_default_but_has_a_visit(self): class MyThingy(ColumnClause): pass @@ -131,11 +133,52 @@ class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL): def visit_thingy(thingy, compiler, **kw): return "mythingy" + eq_(str(MyThingy('x')), "x") + + def test_no_default_has_no_visit(self): + class MyThingy(TypeEngine): + pass + + @compiles(MyThingy, 'postgresql') + def visit_thingy(thingy, compiler, **kw): + return "mythingy" + assert_raises_message( exc.CompileError, "<class 'test.ext.test_compiler..*MyThingy'> " "construct has no default compilation handler.", - str, MyThingy('x') + str, MyThingy() + ) + + def test_no_default_message(self): + class MyThingy(ClauseElement): + pass + + @compiles(MyThingy, 'postgresql') + def visit_thingy(thingy, compiler, **kw): + return "mythingy" + + assert_raises_message( + exc.CompileError, + "<class 'test.ext.test_compiler..*MyThingy'> " + "construct has no default compilation handler.", + str, MyThingy() + ) + + def test_default_subclass(self): + from sqlalchemy.types import ARRAY + + class MyArray(ARRAY): + pass + + @compiles(MyArray, "sqlite") + def sl_array(elem, compiler, **kw): + return "array" + + self.assert_compile( + MyArray(Integer), + "INTEGER[]", + dialect="postgresql" ) def test_annotations(self): |