Search in sources :

Example 1 with Row

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

the class RowInCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    if (asList(DERBY, FIREBIRD, SQLITE).contains(configuration.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        for (Row row : right) {
            conditions.add(new RowCondition(left, row, EQUALS));
        }
        Condition result = DSL.or(conditions);
        if (comparator == NOT_IN) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) QueryPartInternal(org.jooq.QueryPartInternal) ArrayList(java.util.ArrayList) Row(org.jooq.Row)

Example 2 with Row

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

the class TableAlias method init.

/**
     * Register fields for this table alias
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Fields<R> init(String[] fieldAliases) {
    List<Field<?>> result = new ArrayList<Field<?>>();
    Row row = this.alias.wrapped().fieldsRow();
    int size = row.size();
    for (int i = 0; i < size; i++) {
        Field<?> field = row.field(i);
        String name = field.getName();
        if (fieldAliases != null && fieldAliases.length > i) {
            name = fieldAliases[i];
        }
        result.add(new TableFieldImpl(name, field.getDataType(), this, field.getComment(), field.getBinding()));
    }
    return new Fields<R>(result);
}
Also used : TableField(org.jooq.TableField) Field(org.jooq.Field) ArrayList(java.util.ArrayList) Row(org.jooq.Row)

Example 3 with Row

use of org.jooq.Row 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)

Aggregations

ArrayList (java.util.ArrayList)3 Row (org.jooq.Row)3 Condition (org.jooq.Condition)2 Field (org.jooq.Field)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 QueryPartInternal (org.jooq.QueryPartInternal)1 TableField (org.jooq.TableField)1 DSL.falseCondition (org.jooq.impl.DSL.falseCondition)1 DSL.trueCondition (org.jooq.impl.DSL.trueCondition)1