Exemplo n.º 1
0
    protected final Field<?> getField(Table<?> table, String name) {
        Field<?> result = table.field(name);

        if (result == null) {
            result = table.field(name.toUpperCase());
        }

        if (result == null) {
            result = table.field(name.toLowerCase());
        }

        return result;
    }
Exemplo n.º 2
0
 /** Extracted method for type-safety */
 private <Z> Condition condition(Table<?> pivot, Field<Z> field) {
   return field.equal(pivot.field(field));
 }
Exemplo n.º 3
0
    private Table<Record> select(Configuration configuration) {
      List<Field<?>> groupingFields = new ArrayList<Field<?>>();
      List<Field<?>> aliasedGroupingFields = new ArrayList<Field<?>>();
      List<Field<?>> aggregatedFields = new ArrayList<Field<?>>();

      Table<?> pivot = table.as("pivot_outer");

      // Clearly, the API should be improved to make this more object-
      // oriented...

      // This loop finds all fields that are used in aggregate
      // functions. They're excluded from the GROUP BY clause
      for (Field<?> field : aggregateFunctions) {
        if (field instanceof Function) {
          for (QueryPart argument : ((Function<?>) field).getArguments()) {
            if (argument instanceof Field) {
              aggregatedFields.add((Field<?>) argument);
            }
          }
        }
      }

      // This loop finds all fields qualify for GROUP BY clauses
      for (Field<?> field : table.fields()) {
        if (!aggregatedFields.contains(field)) {
          if (!on.equals(field)) {
            aliasedGroupingFields.add(pivot.field(field));
            groupingFields.add(field);
          }
        }
      }

      // The product {aggregateFunctions} x {in}
      List<Field<?>> aggregationSelects = new ArrayList<Field<?>>();
      for (Field<?> inField : in) {
        for (Field<?> aggregateFunction : aggregateFunctions) {
          Condition join = trueCondition();

          for (Field<?> field : groupingFields) {
            join = join.and(condition(pivot, field));
          }

          @SuppressWarnings("unchecked")
          Select<?> aggregateSelect =
              using(configuration)
                  .select(aggregateFunction)
                  .from(table)
                  .where(on.equal((Field<T>) inField))
                  .and(join);

          aggregationSelects.add(
              aggregateSelect.asField(inField.getName() + "_" + aggregateFunction.getName()));
        }
      }

      // This is the complete select
      Table<Record> select =
          using(configuration)
              .select(aliasedGroupingFields)
              .select(aggregationSelects)
              .from(pivot)
              .where(pivot.field(on).in(in.toArray(new Field[0])))
              .groupBy(aliasedGroupingFields)
              .asTable();

      return select;
    }