summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-05-08 12:37:55 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-05-08 12:37:55 -0400
commiteb1bb84fbc10c801c7269a3d38c9e0235327857e (patch)
treed498ddd719456996a8169d48d18271acb291a069 /lib/sqlalchemy/sql/compiler.py
parent5c897d9c6a84ac0cc1d1300dc2456a14b6e6bdd8 (diff)
downloadsqlalchemy-eb1bb84fbc10c801c7269a3d38c9e0235327857e.tar.gz
- Added official support for a CTE used by the SELECT present
inside of :meth:`.Insert.from_select`. This behavior worked accidentally up until 0.9.9, when it no longer worked due to unrelated changes as part of :ticket:`3248`. Note that this is the rendering of the WITH clause after the INSERT, before the SELECT; the full functionality of CTEs rendered at the top level of INSERT, UPDATE, DELETE is a new feature targeted for a later release. fixes #3418
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index c9c7fd2a1..e9c3d0efa 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1613,7 +1613,7 @@ class SQLCompiler(Compiled):
if per_dialect:
text += " " + self.get_statement_hint_text(per_dialect)
- if self.ctes and toplevel:
+ if self.ctes and self._is_toplevel_select(select):
text = self._render_cte_clause() + text
if select._suffixes:
@@ -1627,6 +1627,20 @@ class SQLCompiler(Compiled):
else:
return text
+ def _is_toplevel_select(self, select):
+ """Return True if the stack is placed at the given select, and
+ is also the outermost SELECT, meaning there is either no stack
+ before this one, or the enclosing stack is a topmost INSERT.
+
+ """
+ return (
+ self.stack[-1]['selectable'] is select and
+ (
+ len(self.stack) == 1 or self.isinsert and len(self.stack) == 2
+ and self.statement is self.stack[0]['selectable']
+ )
+ )
+
def _setup_select_hints(self, select):
byfrom = dict([
(from_, hinttext % {