Search in sources :

Example 1 with EQUALS

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

the class RowInCondition method accept.

@Override
public final void accept(Context<?> ctx) {
    if (EMULATE_IN.contains(ctx.dialect())) {
        Condition result = DSL.or(map(right, r -> new RowCondition(left, r, EQUALS)));
        if (not)
            result = result.not();
        ctx.visit(result);
    } else {
        if (right.size() == 0) {
            if (not)
                ctx.visit(trueCondition());
            else
                ctx.visit(falseCondition());
        } else {
            ctx.visit(left).sql(' ').visit((not ? NOT_IN : IN).toKeyword()).sql(" (").visit(new QueryPartListView<>(AbstractInList.padded(ctx, right))).sql(')');
        }
    }
}
Also used : Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) DERBY(org.jooq.SQLDialect.DERBY) Row(org.jooq.Row) Tools.map(org.jooq.impl.Tools.map) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) MAX_ROW_DEGREE(org.jooq.Constants.MAX_ROW_DEGREE) Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) Clause(org.jooq.Clause) SQLITE(org.jooq.SQLDialect.SQLITE) SQLDialect(org.jooq.SQLDialect) QueryPartInternal(org.jooq.QueryPartInternal) CONDITION_IN(org.jooq.Clause.CONDITION_IN) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) CONDITION(org.jooq.Clause.CONDITION) Set(java.util.Set) Constants(org.jooq.Constants) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) EQUALS(org.jooq.Comparator.EQUALS) Configuration(org.jooq.Configuration) List(java.util.List) Context(org.jooq.Context) NOT_IN(org.jooq.Comparator.NOT_IN) CONDITION_NOT_IN(org.jooq.Clause.CONDITION_NOT_IN) IN(org.jooq.Comparator.IN)

Example 2 with EQUALS

use of org.jooq.Comparator.EQUALS 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)2 List (java.util.List)2 Set (java.util.Set)2 Clause (org.jooq.Clause)2 CONDITION (org.jooq.Clause.CONDITION)2 EQUALS (org.jooq.Comparator.EQUALS)2 Condition (org.jooq.Condition)2 Configuration (org.jooq.Configuration)2 Context (org.jooq.Context)2 QueryPartInternal (org.jooq.QueryPartInternal)2 Row (org.jooq.Row)2 SQLDialect (org.jooq.SQLDialect)2 DERBY (org.jooq.SQLDialect.DERBY)2 FIREBIRD (org.jooq.SQLDialect.FIREBIRD)2 UNotYetImplemented (org.jooq.impl.QOM.UNotYetImplemented)2 Tools.map (org.jooq.impl.Tools.map)2 CONDITION_COMPARISON (org.jooq.Clause.CONDITION_COMPARISON)1 CONDITION_IN (org.jooq.Clause.CONDITION_IN)1 CONDITION_NOT_IN (org.jooq.Clause.CONDITION_NOT_IN)1 Comparator (org.jooq.Comparator)1