From 0721a6bede7222386b2a2508aac5590909fbb148 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 1 Aug 2020 13:57:04 -0400 Subject: Genericize str() for types Remove lookup logic that attempts to locate a dialect for a type, just use StrSQLTypeCompiler. Cleaned up the internal ``str()`` for datatypes so that all types produce a string representation without any dialect present, including that it works for third-party dialect types without that dialect being present. The string representation defaults to being the UPPERCASE name of that type with nothing else. Fixes: #4262 Change-Id: I02149e8a1ba1e7336149e962939b07ae0df83c6b --- lib/sqlalchemy/sql/compiler.py | 24 +++++++++++++++++++++++- lib/sqlalchemy/sql/type_api.py | 7 +------ 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 61e26b003..a8bd1de33 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -4270,6 +4270,14 @@ class GenericTypeCompiler(TypeCompiler): class StrSQLTypeCompiler(GenericTypeCompiler): + def process(self, type_, **kw): + try: + _compiler_dispatch = type_._compiler_dispatch + except AttributeError: + return self._visit_unknown(type_, **kw) + else: + return _compiler_dispatch(self, **kw) + def __getattr__(self, key): if key.startswith("visit_"): return self._visit_unknown @@ -4277,7 +4285,21 @@ class StrSQLTypeCompiler(GenericTypeCompiler): raise AttributeError(key) def _visit_unknown(self, type_, **kw): - return "%s" % type_.__class__.__name__ + if type_.__class__.__name__ == type_.__class__.__name__.upper(): + return type_.__class__.__name__ + else: + return repr(type_) + + def visit_null(self, type_, **kw): + return "NULL" + + def visit_user_defined(self, type_, **kw): + try: + get_col_spec = type_.get_col_spec + except AttributeError: + return repr(type_) + else: + return get_col_spec(**kw) class IdentifierPreparer(object): diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py index 2d23c56e1..1284ef515 100644 --- a/lib/sqlalchemy/sql/type_api.py +++ b/lib/sqlalchemy/sql/type_api.py @@ -626,12 +626,7 @@ class TypeEngine(Traversible): @util.preload_module("sqlalchemy.engine.default") def _default_dialect(self): default = util.preloaded.engine_default - 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: - return default.DefaultDialect() + return default.StrCompileDialect() def __str__(self): if util.py2k: -- cgit v1.2.1