summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/selectable.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-02-11 14:05:49 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2021-02-12 18:58:53 -0500
commit1d2b49bc991ca866fd71da3ccfbcde5093482512 (patch)
tree45d83e2a1ae0b4356670f2600e8f3b34d0776911 /lib/sqlalchemy/sql/selectable.py
parent4aa0c7ae76894048c5c30c89c403c7cbf5d844ff (diff)
downloadsqlalchemy-1d2b49bc991ca866fd71da3ccfbcde5093482512.tar.gz
Further refine labeling for renamed columns
Forked from I22f6cf0f0b3360e55299cdcb2452cead2b2458ea we are attempting to decide the case for columns mapped under a different name. since the .key feature of Column seems to support this fully, see if an annotation can be used to indicate an effective .key for a column. The effective change is that the labeling of column expressions in rows has been improved to retain the original name of the ORM attribute even if used in a subquery. References: #5933 Change-Id: If251f556f7d723f50d349f765f1690d6c679d2ef
Diffstat (limited to 'lib/sqlalchemy/sql/selectable.py')
-rw-r--r--lib/sqlalchemy/sql/selectable.py45
1 files changed, 21 insertions, 24 deletions
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index 7e2c5dd3b..23fdf7e12 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -4078,51 +4078,42 @@ class SelectState(util.MemoizedSlots, CompileState):
@classmethod
def _column_naming_convention(cls, label_style):
- names = set()
- pa = []
-
if label_style is LABEL_STYLE_NONE:
def go(c, col_name=None):
- return col_name or c._proxy_key
+ return c._proxy_key
elif label_style is LABEL_STYLE_TABLENAME_PLUS_COL:
+ names = set()
+ pa = [] # late-constructed as needed, python 2 has no "nonlocal"
def go(c, col_name=None):
# we use key_label since this name is intended for targeting
# within the ColumnCollection only, it's not related to SQL
# rendering which always uses column name for SQL label names
- if col_name:
- name = c._gen_label(col_name)
- else:
- name = c._key_label
+ name = c._key_label
if name in names:
if not pa:
pa.append(prefix_anon_map())
- name = c._label_anon_label % pa[0]
+ name = c._label_anon_key_label % pa[0]
else:
names.add(name)
return name
else:
+ names = set()
+ pa = [] # late-constructed as needed, python 2 has no "nonlocal"
def go(c, col_name=None):
- # we use key_label since this name is intended for targeting
- # within the ColumnCollection only, it's not related to SQL
- # rendering which always uses column name for SQL label names
- if col_name:
- name = col_name
- else:
- name = c._proxy_key
+ name = c._proxy_key
if name in names:
if not pa:
pa.append(prefix_anon_map())
-
- name = c.anon_label % pa[0]
+ name = c.anon_key_label % pa[0]
else:
names.add(name)
@@ -5617,6 +5608,14 @@ class Select(
return self
def _generate_columns_plus_names(self, anon_for_dupe_key):
+ """Generate column names as rendered in a SELECT statement by
+ the compiler.
+
+ This is distinct from other name generators that are intended for
+ population of .c collections and similar, which may have slightly
+ different rules.
+
+ """
cols = self._exported_columns_iterator()
# when use_labels is on:
@@ -5732,19 +5731,17 @@ class Select(
if key is not None and key in keys_seen:
if pa is None:
pa = prefix_anon_map()
- key = c._label_anon_label % pa
+ key = c._label_anon_key_label % pa
keys_seen.add(key)
elif disambiguate_only:
- key = c.key
+ key = c._proxy_key
if key is not None and key in keys_seen:
if pa is None:
pa = prefix_anon_map()
- key = c.anon_label % pa
+ key = c.anon_key_label % pa
keys_seen.add(key)
else:
- # one of the above label styles is set for subqueries
- # as of #5221 so this codepath is likely not called now.
- key = None
+ key = c._proxy_key
prox.append(
c._make_proxy(
subquery, key=key, name=name, name_is_truncatable=True