summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorSamuel Chou <sam.chou@windystudios.com>2018-09-19 13:30:24 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2018-09-20 19:04:46 -0400
commit33fccc486111fc6b41eab651cc7325c83099ad45 (patch)
treeeb26f09702a4ce9ab2e2da9eb67de5903d23d4e7 /lib/sqlalchemy/sql/compiler.py
parent95094d3789a558b310782c27a33b95dddb092dfc (diff)
downloadsqlalchemy-33fccc486111fc6b41eab651cc7325c83099ad45.tar.gz
Allow dialects to customize group by clause compilation
Refactored :class:`.SQLCompiler` to expose a :meth:`.SQLCompiler.group_by_clause` method similar to the :meth:`.SQLCompiler.order_by_clause` and :meth:`.SQLCompiler.limit_clause` methods, which can be overridden by dialects to customize how GROUP BY renders. Pull request courtesy Samuel Chou. Change-Id: I0a7238e55032558c27a0c56a72907c7b883456f1 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/474
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index e45db428a..2f68b7e2e 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -968,11 +968,7 @@ class SQLCompiler(Compiled):
for i, c in enumerate(cs.selects))
)
- group_by = cs._group_by_clause._compiler_dispatch(
- self, asfrom=asfrom, **kwargs)
- if group_by:
- text += " GROUP BY " + group_by
-
+ text += self.group_by_clause(cs, **dict(asfrom=asfrom, **kwargs))
text += self.order_by_clause(cs, **kwargs)
text += (cs._limit_clause is not None
or cs._offset_clause is not None) and \
@@ -1929,10 +1925,7 @@ class SQLCompiler(Compiled):
text += " \nWHERE " + t
if select._group_by_clause.clauses:
- group_by = select._group_by_clause._compiler_dispatch(
- self, **kwargs)
- if group_by:
- text += " GROUP BY " + group_by
+ text += self.group_by_clause(select, **kwargs)
if select._having is not None:
t = select._having._compiler_dispatch(self, **kwargs)
@@ -1988,7 +1981,18 @@ class SQLCompiler(Compiled):
"""
return select._distinct and "DISTINCT " or ""
+ def group_by_clause(self, select, **kw):
+ """allow dialects to customize how GROUP BY is rendered."""
+
+ group_by = select._group_by_clause._compiler_dispatch(self, **kw)
+ if group_by:
+ return " GROUP BY " + group_by
+ else:
+ return ""
+
def order_by_clause(self, select, **kw):
+ """allow dialects to customize how ORDER BY is rendered."""
+
order_by = select._order_by_clause._compiler_dispatch(self, **kw)
if order_by:
return " ORDER BY " + order_by