summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2019-08-30 22:23:44 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2019-08-30 22:23:44 +0000
commitb83c41c44bad0b166ad9a2355d10641b0310e2fe (patch)
tree9870ea0f1195da751a2c08d33288f74cd3c663e8 /lib/sqlalchemy/sql
parent520f8579d1785e6f906947ff103aaa8db8330621 (diff)
parentf6c9b20a04d183d86078252048563b14e27fb6d2 (diff)
downloadsqlalchemy-b83c41c44bad0b166ad9a2355d10641b0310e2fe.tar.gz
Merge "Annotate session-bind-lookup entity in Query-produced selectables"
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/annotation.py74
-rw-r--r--lib/sqlalchemy/sql/elements.py40
-rw-r--r--lib/sqlalchemy/sql/selectable.py2
3 files changed, 83 insertions, 33 deletions
diff --git a/lib/sqlalchemy/sql/annotation.py b/lib/sqlalchemy/sql/annotation.py
index 7fc9245ab..a0264845e 100644
--- a/lib/sqlalchemy/sql/annotation.py
+++ b/lib/sqlalchemy/sql/annotation.py
@@ -15,8 +15,80 @@ from . import operators
from .. import util
+class SupportsCloneAnnotations(object):
+ _annotations = util.immutabledict()
+
+ def _annotate(self, values):
+ """return a copy of this ClauseElement with annotations
+ updated by the given dictionary.
+
+ """
+ new = self._clone()
+ new._annotations = new._annotations.union(values)
+ return new
+
+ def _with_annotations(self, values):
+ """return a copy of this ClauseElement with annotations
+ replaced by the given dictionary.
+
+ """
+ new = self._clone()
+ new._annotations = util.immutabledict(values)
+ return new
+
+ def _deannotate(self, values=None, clone=False):
+ """return a copy of this :class:`.ClauseElement` with annotations
+ removed.
+
+ :param values: optional tuple of individual values
+ to remove.
+
+ """
+ if clone or self._annotations:
+ # clone is used when we are also copying
+ # the expression for a deep deannotation
+ new = self._clone()
+ new._annotations = {}
+ return new
+ else:
+ return self
+
+
+class SupportsWrappingAnnotations(object):
+ def _annotate(self, values):
+ """return a copy of this ClauseElement with annotations
+ updated by the given dictionary.
+
+ """
+ return Annotated(self, values)
+
+ def _with_annotations(self, values):
+ """return a copy of this ClauseElement with annotations
+ replaced by the given dictionary.
+
+ """
+ return Annotated(self, values)
+
+ def _deannotate(self, values=None, clone=False):
+ """return a copy of this :class:`.ClauseElement` with annotations
+ removed.
+
+ :param values: optional tuple of individual values
+ to remove.
+
+ """
+ if clone:
+ # clone is used when we are also copying
+ # the expression for a deep deannotation
+ return self._clone()
+ else:
+ # if no clone, since we have no annotations we return
+ # self
+ return self
+
+
class Annotated(object):
- """clones a ClauseElement and applies an 'annotations' dictionary.
+ """clones a SupportsAnnotated and applies an 'annotations' dictionary.
Unlike regular clones, this clone also mimics __hash__() and
__cmp__() of the original element so that it takes its place
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 42e7522ae..bc6f51b8c 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -22,6 +22,7 @@ from . import operators
from . import roles
from . import type_api
from .annotation import Annotated
+from .annotation import SupportsWrappingAnnotations
from .base import _clone
from .base import _generative
from .base import Executable
@@ -161,7 +162,7 @@ def not_(clause):
@inspection._self_inspects
-class ClauseElement(roles.SQLRole, Visitable):
+class ClauseElement(roles.SQLRole, SupportsWrappingAnnotations, Visitable):
"""Base class for elements of a programmatically constructed SQL
expression.
@@ -276,37 +277,6 @@ class ClauseElement(roles.SQLRole, Visitable):
d.pop("_is_clone_of", None)
return d
- def _annotate(self, values):
- """return a copy of this ClauseElement with annotations
- updated by the given dictionary.
-
- """
- return Annotated(self, values)
-
- def _with_annotations(self, values):
- """return a copy of this ClauseElement with annotations
- replaced by the given dictionary.
-
- """
- return Annotated(self, values)
-
- def _deannotate(self, values=None, clone=False):
- """return a copy of this :class:`.ClauseElement` with annotations
- removed.
-
- :param values: optional tuple of individual values
- to remove.
-
- """
- if clone:
- # clone is used when we are also copying
- # the expression for a deep deannotation
- return self._clone()
- else:
- # if no clone, since we have no annotations we return
- # self
- return self
-
def _execute_on_connection(self, connection, multiparams, params):
if self.supports_execution:
return connection._execute_clauseelement(self, multiparams, params)
@@ -4230,6 +4200,12 @@ class ColumnClause(roles.LabeledColumnExprRole, Immutable, ColumnElement):
self._memoized_property.expire_instance(self)
self.__dict__["table"] = table
+ def get_children(self, column_tables=False, **kw):
+ if column_tables and self.table is not None:
+ return [self.table]
+ else:
+ return []
+
table = property(_get_table, _set_table)
def _cache_key(self, **kw):
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index 03dbcd449..97c49f8fc 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -19,6 +19,7 @@ from . import operators
from . import roles
from . import type_api
from .annotation import Annotated
+from .annotation import SupportsCloneAnnotations
from .base import _clone
from .base import _cloned_difference
from .base import _cloned_intersection
@@ -2068,6 +2069,7 @@ class SelectBase(
roles.InElementRole,
HasCTE,
Executable,
+ SupportsCloneAnnotations,
Selectable,
):
"""Base class for SELECT statements.