From: Mike Bayer Date: Sun, 3 Oct 2010 17:11:41 +0000 (-0400) Subject: - default compilation of a type will check if the type class's module is X-Git-Tag: rel_0_6_5~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee9102622c8538bd7e2594b05dd8bdc72317d3d0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - default compilation of a type will check if the type class's module is inside of sqlalchemy.dialects, in which case that dialect's default will be used instead of DefaultDialect. --- diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 46e5901a31..61bff6ea6d 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -132,18 +132,28 @@ class AbstractType(Visitable): # ClauseElement.compile()....this is a mistake. if not dialect: + dialect = self._default_dialect + + return dialect.type_compiler.process(self) + + @property + def _default_dialect(self): + if self.__class__.__module__.startswith("sqlalchemy.dialects"): + tokens = self.__class__.__module__.split(".")[0:3] + mod = ".".join(tokens) + return getattr(__import__(mod).dialects, tokens[-1]).dialect() + else: global DefaultDialect if DefaultDialect is None: from sqlalchemy.engine.default import DefaultDialect - dialect = DefaultDialect() + return DefaultDialect() - return dialect.type_compiler.process(self) - def __str__(self): # Py3K #return unicode(self.compile()) # Py2K - return unicode(self.compile()).encode('ascii', 'backslashreplace') + return unicode(self.compile()).\ + encode('ascii', 'backslashreplace') # end Py2K def __init__(self, *args, **kwargs): diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 2a21ce0347..ad8af31a01 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -964,6 +964,23 @@ class ExpressionTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL): assert distinct(test_table.c.data).type == test_table.c.data.type assert test_table.c.data.distinct().type == test_table.c.data.type +class CompileTest(TestBase, AssertsCompiledSQL): + def test_default_compile(self): + """test that the base dialect of the type object is used + for default compilation. + + """ + for type_, expected in ( + (String(), "VARCHAR"), + (Integer(), "INTEGER"), + (postgresql.INET(), "INET"), + (postgresql.FLOAT(), "FLOAT"), + (mysql.REAL(precision=8, scale=2), "REAL(8, 2)"), + (postgresql.REAL(), "REAL"), + (INTEGER(), "INTEGER"), + (mysql.INTEGER(display_width=5), "INTEGER(5)") + ): + self.assert_compile(type_, expected) class DateTest(TestBase, AssertsExecutionResults): @classmethod