Search in sources :

Example 11 with Condition

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

the class AbstractMetaDataReadInterface method readKv.

@Override
public Optional<String> readKv(DSLContext dsl, MetaInfoKey key) {
    KvTable<KvRecord> kvTable = getKvTable();
    Condition c = kvTable.KEY.eq(key.getKeyName());
    return dsl.select(kvTable.VALUE).from(kvTable).where(c).fetchOptional().map(Record1::value1);
}
Also used : Condition(org.jooq.Condition) KvRecord(com.torodb.backend.tables.records.KvRecord) Record1(org.jooq.Record1)

Example 12 with Condition

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

the class InsertQueryImpl method matchByPrimaryKey.

/**
     * Produce a {@link Condition} that matches existing rows by the inserted or
     * updated primary key values.
     */
@SuppressWarnings("unchecked")
private final Condition matchByPrimaryKey(FieldMapForInsert map) {
    Condition condition = null;
    for (Field<?> f : table.getPrimaryKey().getFields()) {
        Field<Object> field = (Field<Object>) f;
        Field<Object> value = (Field<Object>) map.get(field);
        Condition other = field.equal(value);
        condition = (condition == null) ? other : condition.and(other);
    }
    return condition;
}
Also used : Condition(org.jooq.Condition) Field(org.jooq.Field)

Example 13 with Condition

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

the class JoinTable method accept.

@Override
public final void accept(Context<?> ctx) {
    JoinType translatedType = translateType(ctx);
    Clause translatedClause = translateClause(translatedType);
    String keyword = translatedType.toSQL();
    if (translatedType == CROSS_APPLY && ctx.family() == POSTGRES) {
        keyword = "cross join lateral";
    } else if (translatedType == OUTER_APPLY && ctx.family() == POSTGRES) {
        keyword = "left outer join lateral";
    }
    toSQLTable(ctx, lhs);
    switch(translatedType) {
        case LEFT_SEMI_JOIN:
        case LEFT_ANTI_JOIN:
            if (ctx.data(DATA_COLLECT_SEMI_ANTI_JOIN) != null) {
                @SuppressWarnings("unchecked") List<Condition> semiAntiJoinPredicates = (List<Condition>) ctx.data(DATA_COLLECTED_SEMI_ANTI_JOIN);
                if (semiAntiJoinPredicates == null) {
                    semiAntiJoinPredicates = new ArrayList<Condition>();
                    ctx.data(DATA_COLLECTED_SEMI_ANTI_JOIN, semiAntiJoinPredicates);
                }
                switch(translatedType) {
                    case LEFT_SEMI_JOIN:
                        semiAntiJoinPredicates.add(exists(selectOne().from(rhs).where(condition)));
                        break;
                    case LEFT_ANTI_JOIN:
                        semiAntiJoinPredicates.add(notExists(selectOne().from(rhs).where(condition)));
                        break;
                }
                return;
            }
    }
    ctx.formatIndentStart().formatSeparator().start(translatedClause).keyword(keyword).sql(' ');
    toSQLTable(ctx, rhs);
    // OUTER JOINed table
    if (!rhsPartitionBy.isEmpty()) {
        ctx.formatSeparator().start(TABLE_JOIN_PARTITION_BY).keyword("partition by").sql(" (").visit(rhsPartitionBy).sql(')').end(TABLE_JOIN_PARTITION_BY);
    }
    // CROSS JOIN and NATURAL JOIN do not have any condition clauses
    if (!asList(CROSS_JOIN, NATURAL_JOIN, NATURAL_LEFT_OUTER_JOIN, NATURAL_RIGHT_OUTER_JOIN, CROSS_APPLY, OUTER_APPLY).contains(translatedType)) {
        toSQLJoinCondition(ctx);
    } else if (OUTER_APPLY == translatedType && ctx.family() == POSTGRES) {
        ctx.formatSeparator().start(TABLE_JOIN_ON).keyword("on").sql(" true").end(TABLE_JOIN_ON);
    }
    ctx.end(translatedClause).formatIndentEnd();
}
Also used : Condition(org.jooq.Condition) JoinType(org.jooq.JoinType) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Clause(org.jooq.Clause)

Example 14 with Condition

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

the class RowBetweenCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    // These casts are safe for RowImpl
    RowN r = (RowN) row;
    RowN min = (RowN) minValue;
    RowN max = (RowN) maxValue;
    // These dialects don't support the SYMMETRIC keyword at all
    if (symmetric && asList(CUBRID, DERBY, FIREBIRD, H2, MARIADB, MYSQL, SQLITE).contains(configuration.family())) {
        return not ? (QueryPartInternal) r.notBetween(min, max).and(r.notBetween(max, min)) : (QueryPartInternal) r.between(min, max).or(r.between(max, min));
    } else // Can't handle row value expressions with the BETWEEN predicate
    if (row.size() > 1 && asList(CUBRID, DERBY, FIREBIRD, MARIADB, MYSQL, SQLITE).contains(configuration.family())) {
        Condition result = r.ge(min).and(r.le(max));
        if (not) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) RowN(org.jooq.RowN)

Example 15 with Condition

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

the class RowCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    SQLDialect dialect = configuration.dialect();
    // Regular comparison predicate emulation
    if (asList(EQUALS, NOT_EQUALS).contains(comparator) && asList(DERBY, FIREBIRD, SQLITE).contains(dialect.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            conditions.add(leftFields[i].equal((Field) rightFields[i]));
        }
        Condition result = DSL.and(conditions);
        if (comparator == NOT_EQUALS) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else // Ordering comparison predicate emulation
    if (asList(GREATER, GREATER_OR_EQUAL, LESS, LESS_OR_EQUAL).contains(comparator) && asList(DERBY, CUBRID, FIREBIRD, SQLITE).contains(dialect.family())) {
        // The order component of the comparator (stripping the equal component)
        Comparator order = (comparator == GREATER) ? GREATER : (comparator == GREATER_OR_EQUAL) ? GREATER : (comparator == LESS) ? LESS : (comparator == LESS_OR_EQUAL) ? LESS : null;
        // [#2658] The factored order component of the comparator (enforcing the equal component)
        Comparator factoredOrder = (comparator == GREATER) ? GREATER_OR_EQUAL : (comparator == GREATER_OR_EQUAL) ? GREATER_OR_EQUAL : (comparator == LESS) ? LESS_OR_EQUAL : (comparator == LESS_OR_EQUAL) ? LESS_OR_EQUAL : null;
        // Whether the comparator has an equal component
        boolean equal = (comparator == GREATER_OR_EQUAL) || (comparator == LESS_OR_EQUAL);
        // The following algorithm emulates the equivalency of these expressions:
        // (A, B, C) > (X, Y, Z)
        // (A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z)
        List<Condition> outer = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            List<Condition> inner = new ArrayList<Condition>();
            for (int j = 0; j < i; j++) {
                inner.add(leftFields[j].equal((Field) rightFields[j]));
            }
            inner.add(leftFields[i].compare(order, (Field) rightFields[i]));
            outer.add(DSL.and(inner));
        }
        if (equal) {
            outer.add(new RowCondition(left, right, Comparator.EQUALS));
        }
        Condition result = DSL.or(outer);
        // (A >= X) AND ((A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z))
        if (leftFields.length > 1) {
            result = leftFields[0].compare(factoredOrder, (Field) rightFields[0]).and(result);
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) Comparator(org.jooq.Comparator) QueryPartInternal(org.jooq.QueryPartInternal) Field(org.jooq.Field) SQLDialect(org.jooq.SQLDialect) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList)

Aggregations

Condition (org.jooq.Condition)29 Field (org.jooq.Field)14 GroupField (org.jooq.GroupField)11 SortField (org.jooq.SortField)11 TableField (org.jooq.TableField)11 ArrayList (java.util.ArrayList)9 List (java.util.List)5 Arrays.asList (java.util.Arrays.asList)3 Comparator (org.jooq.Comparator)3 QueryPartInternal (org.jooq.QueryPartInternal)3 Record (org.jooq.Record)3 SQLDialect (org.jooq.SQLDialect)3 Collections.emptyList (java.util.Collections.emptyList)2 LinkedHashMap (java.util.LinkedHashMap)2 ConstraintTypeStep (org.jooq.ConstraintTypeStep)2 Name (org.jooq.Name)2 Record1 (org.jooq.Record1)2 Row (org.jooq.Row)2 RowN (org.jooq.RowN)2 Table (org.jooq.Table)2