Search in sources :

Example 31 with Condition

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

the class MergeImpl method getStandardMerge.

// -------------------------------------------------------------------------
// QueryPart API
// -------------------------------------------------------------------------
/**
     * Return a standard MERGE statement emulating the H2-specific syntax
     */
private final QueryPart getStandardMerge() {
    // The SRC for the USING() clause:
    // ------------------------------
    Table<?> src;
    if (upsertSelect != null) {
        List<Field<?>> v = new ArrayList<Field<?>>();
        Row row = upsertSelect.fieldsRow();
        for (int i = 0; i < row.size(); i++) {
            v.add(row.field(i).as("s" + (i + 1)));
        }
        // [#579] TODO: Currently, this syntax may require aliasing
        // on the call-site
        src = DSL.select(v).from(upsertSelect).asTable("src");
    } else {
        List<Field<?>> v = new ArrayList<Field<?>>();
        for (int i = 0; i < getUpsertValues().size(); i++) {
            v.add(getUpsertValues().get(i).as("s" + (i + 1)));
        }
        src = DSL.select(v).asTable("src");
    }
    // The condition for the ON clause:
    // --------------------------------
    Set<Field<?>> onFields = new HashSet<Field<?>>();
    Condition condition = null;
    if (getUpsertKeys().isEmpty()) {
        UniqueKey<?> key = table.getPrimaryKey();
        if (key != null) {
            onFields.addAll(key.getFields());
            for (int i = 0; i < key.getFields().size(); i++) {
                Condition rhs = key.getFields().get(i).equal((Field) src.field(i));
                if (condition == null) {
                    condition = rhs;
                } else {
                    condition = condition.and(rhs);
                }
            }
        } else // This should probably execute an INSERT statement
        {
            throw new IllegalStateException("Cannot omit KEY() clause on a non-Updatable Table");
        }
    } else {
        for (int i = 0; i < getUpsertKeys().size(); i++) {
            int matchIndex = getUpsertFields().indexOf(getUpsertKeys().get(i));
            if (matchIndex == -1) {
                throw new IllegalStateException("Fields in KEY() clause must be part of the fields specified in MERGE INTO table (...)");
            }
            onFields.addAll(getUpsertKeys());
            Condition rhs = getUpsertKeys().get(i).equal((Field) src.field(matchIndex));
            if (condition == null) {
                condition = rhs;
            } else {
                condition = condition.and(rhs);
            }
        }
    }
    // INSERT and UPDATE clauses
    // -------------------------
    Map<Field<?>, Field<?>> update = new LinkedHashMap<Field<?>, Field<?>>();
    Map<Field<?>, Field<?>> insert = new LinkedHashMap<Field<?>, Field<?>>();
    for (int i = 0; i < src.fieldsRow().size(); i++) {
        // Oracle does not allow to update fields from the ON clause
        if (!onFields.contains(getUpsertFields().get(i))) {
            update.put(getUpsertFields().get(i), src.field(i));
        }
        insert.put(getUpsertFields().get(i), src.field(i));
    }
    return DSL.mergeInto(table).using(src).on(condition).whenMatchedThenUpdate().set(update).whenNotMatchedThenInsert().set(insert);
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.jooq.Field) Row(org.jooq.Row) HashSet(java.util.HashSet)

Example 32 with Condition

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

the class ParserImpl method parseDelete.

private static final Delete<?> parseDelete(ParserContext ctx) {
    parseKeyword(ctx, "DELETE");
    parseKeywordIf(ctx, "FROM");
    Table<?> tableName = parseTableName(ctx);
    boolean where = parseKeywordIf(ctx, "WHERE");
    Condition condition = where ? parseCondition(ctx) : null;
    // TODO Implement returning
    // boolean returning = parseKeywordIf(ctx, "RETURNING");
    DeleteWhereStep<?> s1;
    DeleteFinalStep<?> s2;
    s1 = ctx.dsl.delete(tableName);
    s2 = where ? s1.where(condition) : s1;
    return s2;
}
Also used : Condition(org.jooq.Condition)

Example 33 with Condition

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

the class ParserImpl method parseUpdate.

private static final Update<?> parseUpdate(ParserContext ctx) {
    parseKeyword(ctx, "UPDATE");
    Table<?> tableName = parseTableName(ctx);
    parseKeyword(ctx, "SET");
    // TODO Row value expression updates
    Map<Field<?>, Object> map = parseSetClauseList(ctx);
    // TODO support FROM
    Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
    // TODO support RETURNING
    return condition == null ? ctx.dsl.update(tableName).set(map) : ctx.dsl.update(tableName).set(map).where(condition);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 34 with Condition

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

the class ParserImpl method parseAggregateFunctionIf.

private static final Field<?> parseAggregateFunctionIf(ParserContext ctx) {
    AggregateFunction<?> agg;
    WindowBeforeOverStep<?> over;
    Field<?> result;
    Condition filter;
    over = agg = parseCountIf(ctx);
    if (agg == null)
        over = agg = parseGeneralSetFunctionIf(ctx);
    if (agg == null)
        over = agg = parseBinarySetFunctionIf(ctx);
    if (agg == null)
        over = parseOrderedSetFunctionIf(ctx);
    if (agg == null && over == null)
        return null;
    if (agg != null && parseKeywordIf(ctx, "FILTER")) {
        parse(ctx, '(');
        parseKeyword(ctx, "WHERE");
        filter = parseCondition(ctx);
        parse(ctx, ')');
        result = over = agg.filterWhere(filter);
    } else if (agg != null)
        result = agg;
    else
        result = over;
    // TODO parse WITHIN GROUP (ORDER BY) where applicable
    if (parseKeywordIf(ctx, "OVER")) {
        Object nameOrSpecification = parseWindowNameOrSpecification(ctx, agg != null);
        if (nameOrSpecification instanceof Name)
            result = over.over((Name) nameOrSpecification);
        else if (nameOrSpecification instanceof WindowSpecification)
            result = over.over((WindowSpecification) nameOrSpecification);
        else
            result = over.over();
    }
    return result;
}
Also used : Condition(org.jooq.Condition) WindowSpecification(org.jooq.WindowSpecification) Name(org.jooq.Name)

Example 35 with Condition

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

the class RowIsNull method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    // Informix doesn't implement the RVE IS NULL predicate.
    if (asList(CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, SQLITE).contains(configuration.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        for (Field<?> field : row.fields()) {
            conditions.add(isNull ? field.isNull() : field.isNotNull());
        }
        Condition result = DSL.and(conditions);
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) QueryPartInternal(org.jooq.QueryPartInternal) ArrayList(java.util.ArrayList)

Aggregations

Condition (org.jooq.Condition)47 Field (org.jooq.Field)17 ArrayList (java.util.ArrayList)14 List (java.util.List)11 GroupField (org.jooq.GroupField)11 SortField (org.jooq.SortField)11 TableField (org.jooq.TableField)11 DSLContext (org.jooq.DSLContext)7 Record1 (org.jooq.Record1)7 SearchUtilities.mkTerms (com.khartec.waltz.data.SearchUtilities.mkTerms)6 Collectors (java.util.stream.Collectors)6 DSL (org.jooq.impl.DSL)6 EntitySearchOptions (com.khartec.waltz.model.entity_search.EntitySearchOptions)5 SetUtilities.orderedUnion (com.khartec.waltz.common.SetUtilities.orderedUnion)4 DatabaseVendorSpecific (com.khartec.waltz.data.DatabaseVendorSpecific)4 FullTextSearch (com.khartec.waltz.data.FullTextSearch)4 JooqUtilities (com.khartec.waltz.data.JooqUtilities)4 Collections.emptyList (java.util.Collections.emptyList)4 Record (org.jooq.Record)4 EntityKind (com.khartec.waltz.model.EntityKind)3