Search in sources :

Example 1 with LESS_OR_EQUAL

use of org.jooq.Comparator.LESS_OR_EQUAL in project jOOQ by jOOQ.

the class RowCondition method accept.

@Override
public final void accept(Context<?> ctx) {
    // Regular comparison predicate emulation
    if ((comparator == EQUALS || comparator == NOT_EQUALS) && (forceEmulation || EMULATE_EQ_AND_NE.contains(ctx.dialect()))) {
        Field<?>[] rightFields = right.fields();
        Condition result = DSL.and(map(left.fields(), (f, i) -> f.equal((Field) rightFields[i])));
        if (comparator == NOT_EQUALS)
            result = result.not();
        ctx.visit(result);
    } else // Ordering comparison predicate emulation
    if ((comparator == GREATER || comparator == GREATER_OR_EQUAL || comparator == LESS || comparator == LESS_OR_EQUAL) && (forceEmulation || EMULATE_RANGES.contains(ctx.dialect()))) {
        // 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);
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        // 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<>(1 + leftFields.length);
        for (int i = 0; i < leftFields.length; i++) {
            List<Condition> inner = new ArrayList<>(1 + i);
            for (int j = 0; j < i; j++) inner.add(leftFields[j].equal((Field) rightFields[j]));
            inner.add(leftFields[i].compare(equal && i == leftFields.length - 1 ? comparator : order, (Field) rightFields[i]));
            outer.add(DSL.and(inner));
        }
        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);
        ctx.visit(result);
    } else {
        // Some dialects do not support != comparison with rows
        {
            // Some databases need extra parentheses around the RHS
            boolean extraParentheses = false;
            ctx.visit(left).sql(' ').sql(comparator.toSQL()).sql(' ').sql(extraParentheses ? "(" : "").visit(right).sql(extraParentheses ? ")" : "");
        }
    }
}
Also used : Condition(org.jooq.Condition) DERBY(org.jooq.SQLDialect.DERBY) LESS(org.jooq.Comparator.LESS) Row(org.jooq.Row) Tools.map(org.jooq.impl.Tools.map) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) GREATER_OR_EQUAL(org.jooq.Comparator.GREATER_OR_EQUAL) Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) CONDITION_COMPARISON(org.jooq.Clause.CONDITION_COMPARISON) Clause(org.jooq.Clause) SQLDialect(org.jooq.SQLDialect) NOT_EQUALS(org.jooq.Comparator.NOT_EQUALS) LESS_OR_EQUAL(org.jooq.Comparator.LESS_OR_EQUAL) K_NOT(org.jooq.impl.Keywords.K_NOT) QueryPartInternal(org.jooq.QueryPartInternal) Comparator(org.jooq.Comparator) CONDITION(org.jooq.Clause.CONDITION) Set(java.util.Set) Field(org.jooq.Field) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) DSL.select(org.jooq.impl.DSL.select) CUBRID(org.jooq.SQLDialect.CUBRID) EQUALS(org.jooq.Comparator.EQUALS) Configuration(org.jooq.Configuration) List(java.util.List) Context(org.jooq.Context) GREATER(org.jooq.Comparator.GREATER) Field(org.jooq.Field) ArrayList(java.util.ArrayList) List(java.util.List) Comparator(org.jooq.Comparator)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Clause (org.jooq.Clause)1 CONDITION (org.jooq.Clause.CONDITION)1 CONDITION_COMPARISON (org.jooq.Clause.CONDITION_COMPARISON)1 Comparator (org.jooq.Comparator)1 EQUALS (org.jooq.Comparator.EQUALS)1 GREATER (org.jooq.Comparator.GREATER)1 GREATER_OR_EQUAL (org.jooq.Comparator.GREATER_OR_EQUAL)1 LESS (org.jooq.Comparator.LESS)1 LESS_OR_EQUAL (org.jooq.Comparator.LESS_OR_EQUAL)1 NOT_EQUALS (org.jooq.Comparator.NOT_EQUALS)1 Condition (org.jooq.Condition)1 Configuration (org.jooq.Configuration)1 Context (org.jooq.Context)1 Field (org.jooq.Field)1 QueryPartInternal (org.jooq.QueryPartInternal)1 Row (org.jooq.Row)1 SQLDialect (org.jooq.SQLDialect)1