.. changelog::
: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
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',
raise exc.CompileError(
"%s construct has no default "
"compilation handler." % type(element))
+
return fn(element, compiler, **kw)
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'
"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
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):