summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-02-23 12:50:36 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-02-23 16:58:08 -0500
commit2b72c095732939cc96424bc2b7186d5f1723a1db (patch)
tree5b90f14e3978694df11ed64d9bdb152fd369d885 /lib/sqlalchemy/sql
parent7249f9457dae857f06af00ea8efb47cc93f1c7ea (diff)
downloadsqlalchemy-2b72c095732939cc96424bc2b7186d5f1723a1db.tar.gz
support add_cte() for TextualSelect
Fixed issue where the :meth:`.HasCTE.add_cte` method as called upon a :class:`.TextualSelect` instance was not being accommodated by the SQL compiler. The fix additionally adds more "SELECT"-like compiler behavior to :class:`.TextualSelect` including that DML CTEs such as UPDATE and INSERT may be accommodated. Fixes: #7760 Change-Id: Id97062d882e9b2a81b8e31c2bfaa9cfc5f77d5c1 (cherry picked from commit bef67e58121704a9836e1e5ec2d361cd2086036c)
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index ed2905527..6be8ae281 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1621,6 +1621,17 @@ class SQLCompiler(Compiled):
toplevel = not self.stack
entry = self._default_stack_entry if toplevel else self.stack[-1]
+ new_entry = {
+ "correlate_froms": set(),
+ "asfrom_froms": set(),
+ "selectable": taf,
+ }
+ self.stack.append(new_entry)
+
+ if taf._independent_ctes:
+ for cte in taf._independent_ctes:
+ cte._compiler_dispatch(self, **kw)
+
populate_result_map = (
toplevel
or (
@@ -1648,7 +1659,12 @@ class SQLCompiler(Compiled):
add_to_result_map=self._add_to_result_map,
)
- return self.process(taf.element, **kw)
+ text = self.process(taf.element, **kw)
+ if self.ctes:
+ nesting_level = len(self.stack) if not toplevel else None
+ text = self._render_cte_clause(nesting_level=nesting_level) + text
+
+ return text
def visit_null(self, expr, **kw):
return "NULL"