private List<Field> getAggregatedFieldsFromRoot() { List<Field> result = new ArrayList<Field>(); result.addAll(aggregateBy); for (Field field : action.getSelectFields()) { if (field.owner() == action.getRootQuery() || field.owner() == action.getQuery()) { if (field.getAggregation() != Aggregation.None) result.add(field); } } return result; }
@SuppressWarnings({"unchecked", "rawtypes"}) private Select create() { Select subselect = null; groupedSelectFields = null; boolean aggregatedRoot = !aggregateBy.isEmpty(); if (aggregatedRoot) { groupedSelectFields = getAggregatedFieldsFromRoot(); Collection<Query> from = new LinkedHashSet<Query>(); from.addAll(action.getQueries(groupedSelectFields)); from.addAll(action.getFilterQueries()); Collection<ILink> links = getLinks(from, true); SqlToken filter = action.getFilter(); subselect = new Select(); subselect.setFields(groupedSelectFields); subselect.setRootQuery(action.getRootQuery()); subselect.setLinks(links); subselect.setWhere(filter); subselect.setGroupBy((Collection) aggregateBy); // if(isCounter && !isGrouped()) // { // return new CountingSelect(subselect); // } } selectFields = getSelectFields(); Collection<Query> usedQueries = new LinkedHashSet<Query>(); usedQueries.addAll(action.getQueries(selectFields)); usedQueries.addAll(action.getQueries(getGroupFields())); usedQueries.addAll(action.getQueries(getSortFields())); usedQueries.addAll(action.getGroupFilterQueries()); if (aggregateBy.isEmpty()) usedQueries.addAll(action.getFilterQueries()); else usedQueries.remove(action.getRootQuery()); usedQueries = filterUsedQueries(usedQueries); Collection<ILink> links = getLinks(usedQueries, false); SqlToken filter = aggregatedRoot ? (!isGrouped() ? action.getGroupFilter() : null) : action.getFilter(); SqlToken groupFilter = isGrouped() ? action.getGroupFilter() : null; Select result = new Select(); result.setFields(selectFields); result.setRootQuery(action.getRootQuery()); result.setLinks(links); result.setSubselect(subselect); result.setWhere(filter); result.setGroupBy(getGroupFields()); result.setHaving(groupFilter); if (isCounter) return new CountingSelect(result); result.setOrderBy(getSortFields()); return isFramed ? new FramedSelect(result, getStart(), getLimit()) : isAggregated ? new AggregatingSelect(result) : result; }