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 ? ")" : "");
}
}
}
Aggregations