diff options
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/schema.py | 20 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/visitors.py | 47 |
3 files changed, 31 insertions, 42 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 04ae2532b..9c1f50ce1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -3542,6 +3542,12 @@ class DDLCompiler(Compiled): else: return None + def visit_table_or_column_check_constraint(self, constraint, **kw): + if constraint.is_column_level: + return self.visit_column_check_constraint(constraint) + else: + return self.visit_check_constraint(constraint) + def visit_check_constraint(self, constraint): text = "" if constraint.name is not None: diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 7cece42d0..e6d3a6b05 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2238,14 +2238,6 @@ class ColumnDefault(DefaultGenerator): "positional arguments" ) - def _visit_name(self): - if self.for_update: - return "column_onupdate" - else: - return "column_default" - - __visit_name__ = property(_visit_name) - def __repr__(self): return "ColumnDefault(%r)" % (self.arg,) @@ -2861,6 +2853,8 @@ class CheckConstraint(ColumnCollectionConstraint): _allow_multiple_tables = True + __visit_name__ = "table_or_column_check_constraint" + @_document_text_coercion( "sqltext", ":class:`.CheckConstraint`", @@ -2925,13 +2919,9 @@ class CheckConstraint(ColumnCollectionConstraint): if table is not None: self._set_parent_with_dispatch(table) - def __visit_name__(self): - if isinstance(self.parent, Table): - return "check_constraint" - else: - return "column_check_constraint" - - __visit_name__ = property(__visit_name__) + @property + def is_column_level(self): + return not isinstance(self.parent, Table) def copy(self, target_table=None, **kw): if target_table is not None: diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index c946bb4ab..77e6b53a8 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -24,7 +24,6 @@ http://techspot.zzzeek.org/2008/01/23/expression-transformations/ . """ from collections import deque -import operator from .. import exc from .. import util @@ -53,32 +52,26 @@ def _generate_compiler_dispatch(cls): """ visit_name = cls.__visit_name__ - if isinstance(visit_name, util.compat.string_types): - # There is an optimization opportunity here because the - # the string name of the class's __visit_name__ is known at - # this early stage (import time) so it can be pre-constructed. - getter = operator.attrgetter("visit_%s" % visit_name) - - def _compiler_dispatch(self, visitor, **kw): - try: - meth = getter(visitor) - except AttributeError: - raise exc.UnsupportedCompilationError(visitor, cls) - else: - return meth(self, **kw) - - else: - # The optimization opportunity is lost for this case because the - # __visit_name__ is not yet a string. As a result, the visit - # string has to be recalculated with each compilation. - def _compiler_dispatch(self, visitor, **kw): - visit_attr = "visit_%s" % self.__visit_name__ - try: - meth = getattr(visitor, visit_attr) - except AttributeError: - raise exc.UnsupportedCompilationError(visitor, cls) - else: - return meth(self, **kw) + if not isinstance(visit_name, util.compat.string_types): + raise exc.InvalidRequestError( + "__visit_name__ on class %s must be a string at the class level" + % cls.__name__ + ) + + code = ( + "def _compiler_dispatch(self, visitor, **kw):\n" + " try:\n" + " meth = visitor.visit_%(name)s\n" + " except AttributeError:\n" + " util.raise_from_cause(\n" + " exc.UnsupportedCompilationError(visitor, cls))\n" + " else:\n" + " return meth(self, **kw)\n" + ) % {"name": visit_name} + + _compiler_dispatch = langhelpers._exec_code_in_env( + code, {"exc": exc, "cls": cls, "util": util}, "_compiler_dispatch" + ) _compiler_dispatch.__doc__ = """Look for an attribute named "visit_" + self.__visit_name__ on the visitor, and call it with the same |