diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-02-27 12:48:54 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-02-27 12:49:31 -0500 |
commit | 0c31059a1d6303d2d16b59eb6a5c2c751acdf14a (patch) | |
tree | 2c2f51d9b72714da076521932f57735dca02caa5 /lib/sqlalchemy/sql/compiler.py | |
parent | dc615763d39916e9c037c7c376db1817cdf02764 (diff) | |
download | sqlalchemy-0c31059a1d6303d2d16b59eb6a5c2c751acdf14a.tar.gz |
support stringify for plain CTE
Altered the compilation for the :class:`.CTE` construct so that a string is
returned representing the inner SELECT statement if the :class:`.CTE` is
stringified directly, outside of the context of an enclosing SELECT; This
is the same behavior of :meth:`_FromClause.alias` and
:meth:`_SelectStatement.subquery`. Previously, a blank string would be
returned as the CTE is normally placed above a SELECT after that SELECT has
been generated, which is generally misleading when debugging.
Change-Id: Id3007c28e4a7a56d867e850bb890752946bd8f6f
References: #5988
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 763b4cabb..ff1eb686b 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2550,12 +2550,23 @@ class SQLCompiler(Compiled): kwargs["positional_names"] = self.cte_positional[cte] = [] assert kwargs.get("subquery", False) is False - text += " AS %s\n(%s)" % ( - self._generate_prefixes(cte, cte._prefixes, **kwargs), - cte.element._compiler_dispatch( + + if not self.stack: + # toplevel, this is a stringify of the + # cte directly. just compile the inner + # the way alias() does. + return cte.element._compiler_dispatch( + self, asfrom=asfrom, **kwargs + ) + else: + prefixes = self._generate_prefixes( + cte, cte._prefixes, **kwargs + ) + inner = cte.element._compiler_dispatch( self, asfrom=True, **kwargs - ), - ) + ) + + text += " AS %s\n(%s)" % (prefixes, inner) if cte._suffixes: text += " " + self._generate_prefixes( |