Beispiel #1
0
  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;
  }
Beispiel #2
0
  private List<Field> getSelectFields() {
    List<Field> fields = new ArrayList<Field>();

    Collection<Field> groupFields = getGroupFields();
    boolean grouped = isGrouped();

    for (Field field : action.getSelectFields()) {
      if (checkField(field)) {
        if (grouped) {
          if (groupFields.contains(field) || field.isAggregated()) fields.add(field);
        } else if (isAggregated) {
          if (field.isAggregated()) fields.add(field);
        } else fields.add(field);
      }
    }

    return fields;
  }
Beispiel #3
0
  public Collection<ILink> getLinks(Collection<Query> queries, boolean innerSelect) {
    if (innerSelect || aggregateBy.isEmpty()) return action.getLinks(queries);

    Collection<ILink> links = new HashSet<ILink>();

    for (ILink link : aggregateBy) {
      Query linkQuery = link.getQuery();

      for (Query query : queries) {
        Collection<ILink> path = linkQuery.getPath(query);
        if (linkQuery == query || !path.isEmpty()) {
          links.addAll(path);
          links.add(link);
        }
      }
    }

    return links;
  }
Beispiel #4
0
  @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;
  }
Beispiel #5
0
 private Collection<Field> getGroupFields() {
   return action.getGroupByFields();
 }
Beispiel #6
0
 private Collection<Field> getSortFields() {
   return action.getSortFields();
 }
Beispiel #7
0
 private int getLimit() {
   return action.getLimit();
 }
Beispiel #8
0
 private int getStart() {
   int total = action.getTotalCount();
   int start = Math.max(action.getStart() + 1, 1);
   return start > total ? 1 : start;
 }
Beispiel #9
0
 public SelectFactory(ReadAction action) {
   this.action = action;
   aggregateBy = action.getAggregateByFields();
 }