Search in sources :

Example 1 with GroupField

use of org.jooq.GroupField in project jOOQ by jOOQ.

the class ParserImpl method parseQueryPrimary.

private static final SelectQueryImpl<Record> parseQueryPrimary(ParserContext ctx) {
    if (parseIf(ctx, '(')) {
        SelectQueryImpl<Record> result = parseSelect(ctx);
        parse(ctx, ')');
        return result;
    }
    parseKeyword(ctx, "SELECT");
    boolean distinct = parseKeywordIf(ctx, "DISTINCT") || parseKeywordIf(ctx, "UNIQUE");
    Long limit = null;
    Long offset = null;
    if (!distinct)
        parseKeywordIf(ctx, "ALL");
    if (parseKeywordIf(ctx, "TOP")) {
        limit = parseUnsignedInteger(ctx);
        if (parseKeywordIf(ctx, "START AT"))
            offset = parseUnsignedInteger(ctx);
    }
    List<Field<?>> select = parseSelectList(ctx);
    List<Table<?>> from = null;
    Condition startWith = null;
    Condition connectBy = null;
    boolean connectByNoCycle = false;
    Condition where = null;
    List<GroupField> groupBy = null;
    Condition having = null;
    if (parseKeywordIf(ctx, "FROM"))
        from = parseTables(ctx);
    // TODO is there a better way?
    if (from != null && from.size() == 1 && from.get(0).getName().equalsIgnoreCase("dual"))
        from = null;
    if (parseKeywordIf(ctx, "START WITH")) {
        startWith = parseCondition(ctx);
        parseKeyword(ctx, "CONNECT BY");
        connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
        connectBy = parseCondition(ctx);
    } else if (parseKeywordIf(ctx, "CONNECT BY")) {
        connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
        connectBy = parseCondition(ctx);
        if (parseKeywordIf(ctx, "START WITH"))
            startWith = parseCondition(ctx);
    }
    if (parseKeywordIf(ctx, "WHERE"))
        where = parseCondition(ctx);
    if (parseKeywordIf(ctx, "GROUP BY")) {
        if (parseIf(ctx, '(')) {
            parse(ctx, ')');
            groupBy = emptyList();
        } else if (parseKeywordIf(ctx, "ROLLUP")) {
            parse(ctx, '(');
            groupBy = singletonList(rollup(parseFields(ctx).toArray(EMPTY_FIELD)));
            parse(ctx, ')');
        } else if (parseKeywordIf(ctx, "CUBE")) {
            parse(ctx, '(');
            groupBy = singletonList(cube(parseFields(ctx).toArray(EMPTY_FIELD)));
            parse(ctx, ')');
        } else if (parseKeywordIf(ctx, "GROUPING SETS")) {
            List<List<Field<?>>> fieldSets = new ArrayList<List<Field<?>>>();
            parse(ctx, '(');
            do {
                parse(ctx, '(');
                if (parseIf(ctx, ')')) {
                    fieldSets.add(Collections.<Field<?>>emptyList());
                } else {
                    fieldSets.add(parseFields(ctx));
                    parse(ctx, ')');
                }
            } while (parseIf(ctx, ','));
            parse(ctx, ')');
            groupBy = singletonList(groupingSets(fieldSets.toArray((Collection[]) EMPTY_COLLECTION)));
        } else {
            groupBy = (List) parseFields(ctx);
            if (parseKeywordIf(ctx, "WITH ROLLUP"))
                groupBy = singletonList(rollup(groupBy.toArray(EMPTY_FIELD)));
        }
    }
    if (parseKeywordIf(ctx, "HAVING"))
        having = parseCondition(ctx);
    // TODO support WINDOW
    SelectQueryImpl<Record> result = (SelectQueryImpl<Record>) ctx.dsl.selectQuery();
    if (distinct)
        result.setDistinct(distinct);
    if (select.size() > 0)
        result.addSelect(select);
    if (from != null)
        result.addFrom(from);
    if (connectBy != null)
        if (connectByNoCycle)
            result.addConnectByNoCycle(connectBy);
        else
            result.addConnectBy(connectBy);
    if (startWith != null)
        result.setConnectByStartWith(startWith);
    if (where != null)
        result.addConditions(where);
    if (groupBy != null)
        result.addGroupBy(groupBy);
    if (having != null)
        result.addHaving(having);
    if (limit != null)
        if (offset != null)
            result.addLimit((int) (long) offset, (int) (long) limit);
        else
            result.addLimit((int) (long) limit);
    return result;
}
Also used : Condition(org.jooq.Condition) Table(org.jooq.Table) GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Collection(java.util.Collection) Record(org.jooq.Record) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Condition (org.jooq.Condition)1 Field (org.jooq.Field)1 GroupField (org.jooq.GroupField)1 Record (org.jooq.Record)1 SortField (org.jooq.SortField)1 Table (org.jooq.Table)1 TableField (org.jooq.TableField)1