Search in sources :

Example 1 with CommonTableExpression

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

the class DefaultParseContext method parseWith.

private final Query parseWith(boolean parseSelect, Integer degree) {
    int parens = 0;
    while (parseIf('(')) parens++;
    parseKeyword("WITH");
    boolean recursive = parseKeywordIf("RECURSIVE");
    List<CommonTableExpression<?>> cte = new ArrayList<>();
    do {
        Name name = parseIdentifier();
        DerivedColumnList dcl = null;
        if (parseIf('(')) {
            List<Name> columnNames = parseIdentifiers();
            parse(')');
            dcl = name.fields(columnNames.toArray(EMPTY_NAME));
        }
        parseKeyword("AS");
        boolean materialized = parseKeywordIf("MATERIALIZED");
        boolean notMaterialized = !materialized && parseKeywordIf("NOT MATERIALIZED");
        parse('(');
        ResultQuery<?> resultQuery = (ResultQuery<?>) parseQuery(true, false);
        parse(')');
        cte.add(dcl != null ? materialized ? dcl.asMaterialized(resultQuery) : notMaterialized ? dcl.asNotMaterialized(resultQuery) : dcl.as(resultQuery) : materialized ? name.asMaterialized(resultQuery) : notMaterialized ? name.asNotMaterialized(resultQuery) : name.as(resultQuery));
    } while (parseIf(','));
    // TODO Better model API for WITH clause
    WithImpl with = (WithImpl) new WithImpl(dsl.configuration(), recursive).with(cte.toArray(EMPTY_COMMON_TABLE_EXPRESSION));
    Query result;
    if (!parseSelect && (peekKeyword("DELETE") || peekKeyword("DEL")))
        result = parseDelete(with, false);
    else if (!parseSelect && (peekKeyword("INSERT") || peekKeyword("INS")))
        result = parseInsert(with, false);
    else if (!parseSelect && peekKeyword("MERGE"))
        result = parseMerge(with);
    else if (peekSelect(true))
        result = parseSelect(degree, with);
    else if (!parseSelect && (peekKeyword("UPDATE") || peekKeyword("UPD")))
        result = parseUpdate(with, false);
    else if ((parseWhitespaceIf() || true) && done())
        throw exception("Missing statement after WITH");
    else
        throw exception("Unsupported statement after WITH");
    while (parens-- > 0) parse(')');
    return result;
}
Also used : ResultQuery(org.jooq.ResultQuery) Query(org.jooq.Query) DDLQuery(org.jooq.DDLQuery) CommonTableExpression(org.jooq.CommonTableExpression) ArrayList(java.util.ArrayList) DerivedColumnList(org.jooq.DerivedColumnList) ResultQuery(org.jooq.ResultQuery) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint) Name(org.jooq.Name) DSL.systemName(org.jooq.impl.DSL.systemName)

Example 2 with CommonTableExpression

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

the class WithImpl method as0.

// -------------------------------------------------------------------------
// XXX With API
// -------------------------------------------------------------------------
private final WithStep as0(ResultQuery query, Boolean materialized) {
    DerivedColumnList dcl;
    if (fieldNameFunction != null)
        dcl = name(alias).fields(fieldNameFunction);
    else
        dcl = name(alias).fields(fieldAliases);
    CommonTableExpression cte;
    if (materialized == null)
        cte = dcl.as(query);
    else if (materialized)
        cte = dcl.asMaterialized(query);
    else
        cte = dcl.asNotMaterialized(query);
    this.ctes.add(cte);
    this.alias = null;
    this.fieldAliases = null;
    this.fieldNameFunction = null;
    return this;
}
Also used : CommonTableExpression(org.jooq.CommonTableExpression) DerivedColumnList(org.jooq.DerivedColumnList)

Aggregations

CommonTableExpression (org.jooq.CommonTableExpression)2 DerivedColumnList (org.jooq.DerivedColumnList)2 ArrayList (java.util.ArrayList)1 Constraint (org.jooq.Constraint)1 DDLQuery (org.jooq.DDLQuery)1 Name (org.jooq.Name)1 Query (org.jooq.Query)1 ResultQuery (org.jooq.ResultQuery)1 DSL.constraint (org.jooq.impl.DSL.constraint)1 DSL.systemName (org.jooq.impl.DSL.systemName)1