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 \
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)
"""
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
from sqlalchemy.engine import default
from sqlalchemy.dialects import mysql, mssql, postgresql, oracle, \
sqlite, sybase
+from sqlalchemy.dialects.postgresql.base import PGCompiler, PGDialect
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import compiler
"GROUP BY myothertable.othername ORDER BY myothertable.othername"
)
+ def test_custom_order_by_clause(self):
+ class CustomCompiler(PGCompiler):
+ def order_by_clause(self, select, **kw):
+ return super(CustomCompiler, self).\
+ order_by_clause(select, **kw) + " CUSTOMIZED"
+
+ class CustomDialect(PGDialect):
+ name = 'custom'
+ statement_compiler = CustomCompiler
+
+ stmt = select([table1.c.myid]).order_by(table1.c.myid)
+ self.assert_compile(
+ stmt,
+ "SELECT mytable.myid FROM mytable ORDER BY "
+ "mytable.myid CUSTOMIZED",
+ dialect=CustomDialect()
+ )
+
+ def test_custom_group_by_clause(self):
+ class CustomCompiler(PGCompiler):
+ def group_by_clause(self, select, **kw):
+ return super(CustomCompiler, self).\
+ group_by_clause(select, **kw) + " CUSTOMIZED"
+
+ class CustomDialect(PGDialect):
+ name = 'custom'
+ statement_compiler = CustomCompiler
+
+ stmt = select([table1.c.myid]).group_by(table1.c.myid)
+ self.assert_compile(
+ stmt,
+ "SELECT mytable.myid FROM mytable GROUP BY "
+ "mytable.myid CUSTOMIZED",
+ dialect=CustomDialect()
+ )
+
def test_for_update(self):
self.assert_compile(
table1.select(table1.c.myid == 7).with_for_update(),