summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-08-01 13:57:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-08-01 19:49:15 -0400
commit0721a6bede7222386b2a2508aac5590909fbb148 (patch)
tree28593583f5d15ebb301af1993bd8c8e110e2c751 /lib/sqlalchemy/sql
parent3d5a64ac09b55514da6fd30f0f085348c2d10496 (diff)
downloadsqlalchemy-0721a6bede7222386b2a2508aac5590909fbb148.tar.gz
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
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py24
-rw-r--r--lib/sqlalchemy/sql/type_api.py7
2 files changed, 24 insertions, 7 deletions
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: