diff options
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/sql/base.py | 20 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/elements.py | 14 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/functions.py | 11 |
3 files changed, 44 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index d9f05e823..6d65d9061 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -15,6 +15,7 @@ import operator import re from . import roles +from . import visitors from .traversals import HasCacheKey # noqa from .traversals import HasCopyInternals # noqa from .traversals import MemoizedHasCacheKey # noqa @@ -1575,6 +1576,23 @@ def _bind_or_error(schemaitem, msg=None): return bind +def _entity_namespace(entity): + """Return the nearest .entity_namespace for the given entity. + + If not immediately available, does an iterate to find a sub-element + that has one, if any. + + """ + try: + return entity.entity_namespace + except AttributeError: + for elem in visitors.iterate(entity): + if hasattr(elem, "entity_namespace"): + return elem.entity_namespace + else: + raise + + def _entity_namespace_key(entity, key): """Return an entry from an entity_namespace. @@ -1584,8 +1602,8 @@ def _entity_namespace_key(entity, key): """ - ns = entity.entity_namespace try: + ns = _entity_namespace(entity) return getattr(ns, key) except AttributeError as err: util.raise_( diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index e27b97802..416a4e82e 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -300,6 +300,13 @@ class ClauseElement( f = f._is_clone_of return s + @property + def entity_namespace(self): + raise AttributeError( + "This SQL expression has no entity namespace " + "with which to filter from." + ) + def __getstate__(self): d = self.__dict__.copy() d.pop("_is_clone_of", None) @@ -4664,6 +4671,13 @@ class ColumnClause( # expect the columns of tables and subqueries to be leaf nodes. return [] + @property + def entity_namespace(self): + if self.table is not None: + return self.table.entity_namespace + else: + return super(ColumnClause, self).entity_namespace + @HasMemoized.memoized_attribute def _from_objects(self): t = self.table diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index 02ed55100..0aa870ce4 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -15,6 +15,7 @@ from . import roles from . import schema from . import sqltypes from . import util as sqlutil +from .base import _entity_namespace from .base import ColumnCollection from .base import Executable from .base import Generative @@ -618,6 +619,16 @@ class FunctionElement(Executable, ColumnElement, FromClause, Generative): else: return super(FunctionElement, self).self_group(against=against) + @property + def entity_namespace(self): + """overrides FromClause.entity_namespace as functions are generally + column expressions and not FromClauses. + + """ + # ideally functions would not be fromclauses but we failed to make + # this adjustment in 1.4 + return _entity_namespace(self.clause_expr) + class FunctionAsBinary(BinaryExpression): _traverse_internals = [ |
