// create where clause; if there are no where conditions and
// no ordering or projections, we return null to signify that this
// query should be treated like an extent
Select inner = sel.getFromSelect();
SQLBuffer where = buildWhere((inner != null) ? inner : sel, ctx,
state.filter, exps.filter);
if (where == null && exps.projections.length == 0
&& exps.ordering.length == 0
&& (sel.getJoins() == null || sel.getJoins().isEmpty())) {
_extent = true;
sel.setAutoDistinct((exps.distinct & exps.DISTINCT_AUTO) != 0);
return sel;
}
// now set sql criteria; it goes on the inner select if present
if (inner != null)
inner.where(where);
else
sel.where(where);
// apply grouping and having. this does not select the grouping
// columns, just builds the GROUP BY clauses. we don't build the
// ORDER BY clauses yet because if we decide to add this select
// to a union, the ORDER BY values get aliased differently
if (exps.having != null) {
Exp havingExp = (Exp) exps.having;
SQLBuffer buf = new SQLBuffer(ctx.store.getDBDictionary());
havingExp.appendTo(sel, ctx, state.having, buf);
sel.having(buf);
}
for (int i = 0; i < exps.grouping.length; i++)
((Val) exps.grouping[i]).groupBy(sel, ctx, state.grouping[i]);