diff options
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 05cc70aba..fd7e7d773 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -572,6 +572,10 @@ class SQLCompiler(engine.Compiled): text += (cs._limit is not None or cs._offset is not None) and \ self.limit_clause(cs) or "" + if self.ctes and \ + compound_index==1 and not entry: + text = self._render_cte_clause() + text + self.stack.pop(-1) if asfrom and parens: return "(" + text + ")" @@ -968,12 +972,7 @@ class SQLCompiler(engine.Compiled): if self.ctes and \ compound_index==1 and not entry: - cte_text = self.get_cte_preamble(self.ctes_recursive) + " " - cte_text += ", \n".join( - [txt for txt in self.ctes.values()] - ) - cte_text += "\n " - text = cte_text + text + text = self._render_cte_clause() + text self.stack.pop(-1) @@ -982,6 +981,14 @@ class SQLCompiler(engine.Compiled): else: return text + def _render_cte_clause(self): + cte_text = self.get_cte_preamble(self.ctes_recursive) + " " + cte_text += ", \n".join( + [txt for txt in self.ctes.values()] + ) + cte_text += "\n " + return cte_text + def get_cte_preamble(self, recursive): if recursive: return "WITH RECURSIVE" |