/**
* Return a new select with expressions initialized.
*/
private Select newSelect(ExpContext ctx, Select parent,
String alias, QueryExpressions exps, QueryExpressionsState state) {
Select subselect = JDBCStoreQuery.getThreadLocalSelect(_subselect);
Select sel = parent != null ? subselect
: ctx.store.getSQLFactory().newSelect();
sel.setAutoDistinct((exps.distinct & exps.DISTINCT_AUTO) != 0);
sel.setJoinSyntax(ctx.fetch.getJoinSyntax());
sel.setParent(parent, alias);
Context[] qryCtx = JDBCStoreQuery.getThreadLocalContext();
Context lctx = null;
for (int i = 0; i < qryCtx.length; i++) {
if (qryCtx[i].cloneFrom == exps.ctx()) {
lctx = qryCtx[i];
break;
}
}
if (sel.ctx() == null)
sel.setContext(lctx);
if (parent == null && lctx.getSubselContexts() != null) {
// this is the case subselect was created before parent got created
List<Context> subselCtxs = lctx.getSubselContexts();
for (Context subselCtx : subselCtxs) {
Select subsel = (Select) subselCtx.getSelect();
Subquery subquery = subselCtx.getSubquery();
subsel.setParent(sel, subquery.getCandidateAlias());
}
}
if (HasContainsExpressionVisitor.hasContains(exps.filter)) {
sel.setHasSubselect(true);
}
initialize(sel, ctx, exps, state);
if (!sel.getAutoDistinct()) {
if ((exps.distinct & exps.DISTINCT_TRUE) != 0)
sel.setDistinct(true);
else if ((exps.distinct & exps.DISTINCT_FALSE) != 0)
sel.setDistinct(false);
} else if (exps.projections.length > 0) {
if (!sel.isDistinct() && (exps.distinct & exps.DISTINCT_TRUE) != 0){
// if the select is not distinct but the query is, force
// the select to be distinct
sel.setDistinct(true);
} else if (sel.isDistinct()) {
// when aggregating data or making a non-distinct projection
// from a distinct select, we have to select from a tmp
// table formed by a distinct subselect in the from clause;
// this subselect selects the pks of the candidate (to
// get unique candidate values) and needed field values and
// applies the where conditions; the outer select applies
// ordering, grouping, etc
boolean agg = exps.isAggregate();
boolean candidate = ProjectionExpressionVisitor.
hasCandidateProjections(exps.projections);
if (agg || (candidate
&& (exps.distinct & exps.DISTINCT_TRUE) == 0)) {
DBDictionary dict = ctx.store.getDBDictionary();
dict.assertSupport(dict.supportsSubselect,
"SupportsSubselect");
Select inner = sel;
sel = ctx.store.getSQLFactory().newSelect();
sel.setParent(parent, alias);
sel.setDistinct(agg
&& (exps.distinct & exps.DISTINCT_TRUE) != 0);
sel.setFromSelect(inner);