[jOOQ/jOOQ#12425] Move IS [ NOT ] DISTINCT FROM to API generator

This includes:
- [jOOQ/jOOQ#12432] Extract CompareCondition and CombinedCondition into their own classes
This commit is contained in:
Lukas Eder 2021-09-15 16:11:58 +02:00
parent 76ade05727
commit 04f00df329
5 changed files with 332 additions and 295 deletions

View File

@ -676,6 +676,70 @@ extends
// -------------------------------------------------------------------------
// Boolean functions
// -------------------------------------------------------------------------
/**
* The <code>IS_DISTINCT_FROM</code> operator.
* <p>
* The DISTINCT predicate allows for creating NULL safe comparisons where the two operands
* are tested for non-equality
*/
@NotNull
@Support
Condition isDistinctFrom(T arg2);
/**
* The <code>IS_DISTINCT_FROM</code> operator.
* <p>
* The DISTINCT predicate allows for creating NULL safe comparisons where the two operands
* are tested for non-equality
*/
@NotNull
@Support
Condition isDistinctFrom(Select<? extends Record1<T>> arg2);
/**
* The <code>IS_DISTINCT_FROM</code> operator.
* <p>
* The DISTINCT predicate allows for creating NULL safe comparisons where the two operands
* are tested for non-equality
*/
@NotNull
@Support
Condition isDistinctFrom(Field<T> arg2);
/**
* The <code>IS_NOT_DISTINCT_FROM</code> operator.
* <p>
* The NOT DISTINCT predicate allows for creating NULL safe comparisons where the two
* operands are tested for equality
*/
@NotNull
@Support
Condition isNotDistinctFrom(T arg2);
/**
* The <code>IS_NOT_DISTINCT_FROM</code> operator.
* <p>
* The NOT DISTINCT predicate allows for creating NULL safe comparisons where the two
* operands are tested for equality
*/
@NotNull
@Support
Condition isNotDistinctFrom(Select<? extends Record1<T>> arg2);
/**
* The <code>IS_NOT_DISTINCT_FROM</code> operator.
* <p>
* The NOT DISTINCT predicate allows for creating NULL safe comparisons where the two
* operands are tested for equality
*/
@NotNull
@Support
Condition isNotDistinctFrom(Field<T> arg2);
/**
* The <code>BIT_AND</code> operator.
*
@ -1259,190 +1323,6 @@ extends
@Support
Condition isNotNull();
// ------------------------------------------------------------------------
// DISTINCT predicates
// ------------------------------------------------------------------------
/**
* Create a condition to check if this field is <code>DISTINCT</code> from
* another value.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>not([this] &lt;=&gt; [value])</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS NOT [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* NOT EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [value] IS NULL THEN FALSE
* WHEN [this] IS NULL AND [value] IS NOT NULL THEN TRUE
* WHEN [this] IS NOT NULL AND [value] IS NULL THEN TRUE
* WHEN [this] = [value] THEN FALSE
* ELSE TRUE
* END
* </pre></code> SQL: <code>this is distinct from value</code>
*/
@NotNull
@Support
Condition isDistinctFrom(T value);
/**
* Create a condition to check if this field is <code>DISTINCT</code> from
* another field.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>not([this] &lt;=&gt; [value])</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS NOT [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* NOT EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [field] IS NULL THEN FALSE
* WHEN [this] IS NULL AND [field] IS NOT NULL THEN TRUE
* WHEN [this] IS NOT NULL AND [field] IS NULL THEN TRUE
* WHEN [this] = [field] THEN FALSE
* ELSE TRUE
* END
* </pre></code> SQL: <code>this is distinct from field</code>
*/
@NotNull
@Support
Condition isDistinctFrom(Field<T> field);
/**
* Create a condition to check if this field is <code>DISTINCT</code> from
* another field.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>not([this] &lt;=&gt; [value])</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS NOT [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* NOT EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [field] IS NULL THEN FALSE
* WHEN [this] IS NULL AND [field] IS NOT NULL THEN TRUE
* WHEN [this] IS NOT NULL AND [field] IS NULL THEN TRUE
* WHEN [this] = [field] THEN FALSE
* ELSE TRUE
* END
* </pre></code> SQL: <code>this is distinct from field</code>
*/
@NotNull
@Support
Condition isDistinctFrom(Select<? extends Record1<T>> select);
/**
* Create a condition to check if this field is <code>NOT DISTINCT</code>
* from another value.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>[this] &lt;=&gt; [value]</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [value] IS NULL THEN TRUE
* WHEN [this] IS NULL AND [value] IS NOT NULL THEN FALSE
* WHEN [this] IS NOT NULL AND [value] IS NULL THEN FALSE
* WHEN [this] = [value] THEN TRUE
* ELSE FALSE
* END
* </pre></code> SQL: <code>this is not distinct from value</code>
*/
@NotNull
@Support
Condition isNotDistinctFrom(T value);
/**
* Create a condition to check if this field is <code>NOT DISTINCT</code>
* from another field.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>[this] &lt;=&gt; [value]</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [field] IS NULL THEN TRUE
* WHEN [this] IS NULL AND [field] IS NOT NULL THEN FALSE
* WHEN [this] IS NOT NULL AND [field] IS NULL THEN FALSE
* WHEN [this] = [value] THEN TRUE
* ELSE FALSE
* END
* </pre></code> SQL: <code>this is not distinct from field</code>
*/
@NotNull
@Support
Condition isNotDistinctFrom(Field<T> field);
/**
* Create a condition to check if this field is <code>NOT DISTINCT</code>
* from another field.
* <p>
* In {@link SQLDialect#MYSQL} and {@link SQLDialect#MARIADB}, this can be
* emulated through <code><pre>[this] &lt;=&gt; [value]</pre></code>
* <p>
* In {@link SQLDialect#SQLITE}, this can be emulated through
* <code><pre>[this] IS [value]</pre></code>
* <p>
* In databases that support <code>INTERSECT</code> (see
* {@link Select#intersect(Select)}, this predicate can be emulated as
* follows: <code><pre>
* EXISTS (SELECT [this] INTERSECT SELECT [value])
* </pre></code>
* <p>
* If this is not supported by the underlying database, jOOQ will render
* this instead: <code><pre>
* CASE WHEN [this] IS NULL AND [field] IS NULL THEN TRUE
* WHEN [this] IS NULL AND [field] IS NOT NULL THEN FALSE
* WHEN [this] IS NOT NULL AND [field] IS NULL THEN FALSE
* WHEN [this] = [value] THEN TRUE
* ELSE FALSE
* END
* </pre></code> SQL: <code>this is not distinct from field</code>
*/
@NotNull
@Support
Condition isNotDistinctFrom(Select<? extends Record1<T>> select);
// ------------------------------------------------------------------------
// LIKE_REGEX predicates
// ------------------------------------------------------------------------

View File

@ -42,8 +42,6 @@ import static org.jooq.Comparator.EQUALS;
import static org.jooq.Comparator.GREATER;
import static org.jooq.Comparator.GREATER_OR_EQUAL;
import static org.jooq.Comparator.IN;
import static org.jooq.Comparator.IS_DISTINCT_FROM;
import static org.jooq.Comparator.IS_NOT_DISTINCT_FROM;
import static org.jooq.Comparator.LESS;
import static org.jooq.Comparator.LESS_OR_EQUAL;
import static org.jooq.Comparator.LIKE;
@ -337,6 +335,46 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
// -------------------------------------------------------------------------
// Boolean functions
// -------------------------------------------------------------------------
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isDistinctFrom(T arg2) {
return new IsDistinctFrom(this, Tools.field(arg2, this));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isDistinctFrom(Select<? extends Record1<T>> arg2) {
return new IsDistinctFrom(this, DSL.field(arg2));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isDistinctFrom(Field<T> arg2) {
return new IsDistinctFrom(this, arg2);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNotDistinctFrom(T arg2) {
return new IsNotDistinctFrom(this, Tools.field(arg2, this));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNotDistinctFrom(Select<? extends Record1<T>> arg2) {
return new IsNotDistinctFrom(this, DSL.field(arg2));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNotDistinctFrom(Field<T> arg2) {
return new IsNotDistinctFrom(this, arg2);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Field<T> bitAnd(T arg2) {
@ -652,36 +690,6 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
return new IsNull(this, false);
}
@Override
public final Condition isDistinctFrom(T value) {
return isDistinctFrom(Tools.field(value, this));
}
@Override
public final Condition isDistinctFrom(Field<T> field) {
return compare(IS_DISTINCT_FROM, field);
}
@Override
public final Condition isDistinctFrom(Select<? extends Record1<T>> select) {
return isDistinctFrom(DSL.field(select));
}
@Override
public final Condition isNotDistinctFrom(T value) {
return isNotDistinctFrom(Tools.field(value, this));
}
@Override
public final Condition isNotDistinctFrom(Field<T> field) {
return compare(IS_NOT_DISTINCT_FROM, field);
}
@Override
public final Condition isNotDistinctFrom(Select<? extends Record1<T>> select) {
return isNotDistinctFrom(DSL.field(select));
}
/**
* [#11200] Nest these constants to prevent initialisation deadlocks.
*/
@ -1381,9 +1389,9 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
public final Condition compare(Comparator comparator, Field<T> field) {
switch (comparator) {
case IS_DISTINCT_FROM:
return new IsDistinctFrom<>(this, nullSafe(field, getDataType()));
case IS_NOT_DISTINCT_FROM:
return new IsDistinctFrom<>(this, nullSafe(field, getDataType()), comparator);
return new IsNotDistinctFrom<>(this, nullSafe(field, getDataType()));
default:
return new CompareCondition(this, nullSafe(field, getDataType()), comparator);
}

View File

@ -37,70 +37,60 @@
*/
package org.jooq.impl;
import static org.jooq.Comparator.IS_DISTINCT_FROM;
import static org.jooq.Comparator.IS_NOT_DISTINCT_FROM;
// ...
// ...
// ...
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.SQLDialect.DERBY;
// ...
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.MARIADB;
// ...
import static org.jooq.SQLDialect.MYSQL;
// ...
// ...
// ...
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.exists;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.notExists;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.Tools.embeddedFields;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.Internal.*;
import static org.jooq.impl.Keywords.*;
import static org.jooq.impl.Names.*;
import static org.jooq.impl.SQLDataType.*;
import static org.jooq.impl.Tools.*;
import static org.jooq.impl.Tools.BooleanDataKey.*;
import static org.jooq.impl.Tools.DataExtendedKey.*;
import static org.jooq.impl.Tools.DataKey.*;
import static org.jooq.SQLDialect.*;
import java.util.Set;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
import org.jooq.Comparator;
import org.jooq.Context;
import org.jooq.Field;
// ...
import org.jooq.QueryPartInternal;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
* The <code>IS DISTINCT FROM</code> statement.
*/
final class IsDistinctFrom<T> extends AbstractCondition {
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class IsDistinctFrom<T>
extends
AbstractCondition
{
private static final Set<SQLDialect> EMULATE_DISTINCT_PREDICATE = SQLDialect.supportedUntil(CUBRID, DERBY);
private static final Set<SQLDialect> SUPPORT_DISTINCT_WITH_ARROW = SQLDialect.supportedBy(MARIADB, MYSQL);
private final Field<T> arg1;
private final Field<T> arg2;
IsDistinctFrom(
Field<T> arg1,
Field<T> arg2
) {
final Field<T> lhs;
final Field<T> rhs;
final Comparator comparator;
IsDistinctFrom(Field<T> lhs, Field<T> rhs, Comparator comparator) {
this.lhs = lhs;
this.rhs = rhs;
this.comparator = comparator;
this.arg1 = nullSafeNotNull(arg1, (DataType) OTHER);
this.arg2 = nullSafeNotNull(arg2, (DataType) OTHER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
static final Set<SQLDialect> EMULATE_DISTINCT_PREDICATE = SQLDialect.supportedUntil(CUBRID, DERBY);
static final Set<SQLDialect> SUPPORT_DISTINCT_WITH_ARROW = SQLDialect.supportedBy(MARIADB, MYSQL);
@Override
final boolean isNullable() {
return false;
@ -108,10 +98,8 @@ final class IsDistinctFrom<T> extends AbstractCondition {
@Override
public final void accept(Context<?> ctx) {
if (lhs.getDataType().isEmbeddable() && rhs.getDataType().isEmbeddable())
ctx.visit(row(embeddedFields(lhs)).compare(comparator, row(embeddedFields(rhs))));
if (arg1.getDataType().isEmbeddable() && arg2.getDataType().isEmbeddable())
ctx.visit(row(embeddedFields(arg1)).isDistinctFrom(row(embeddedFields(arg2))));
@ -122,32 +110,43 @@ final class IsDistinctFrom<T> extends AbstractCondition {
// optimally using INTERSECT...
// [#7222] [#7224] Make sure the columns are aliased
else if (EMULATE_DISTINCT_PREDICATE.contains(ctx.dialect()))
ctx.visit(comparator == IS_DISTINCT_FROM
? notExists(select(lhs.as("x")).intersect(select(rhs.as("x"))))
: exists(select(lhs.as("x")).intersect(select(rhs.as("x")))));
ctx.visit(notExists(select(arg1.as("x")).intersect(select(arg2.as("x")))));
// MySQL knows the <=> operator
else if (SUPPORT_DISTINCT_WITH_ARROW.contains(ctx.dialect()))
ctx.visit(comparator == IS_DISTINCT_FROM
? condition("{not}({0} <=> {1})", lhs, rhs)
: condition("{0} <=> {1}", lhs, rhs));
ctx.visit(condition("{not}({0} <=> {1})", arg1, arg2));
// SQLite knows the IS / IS NOT predicate
else if (SQLITE == ctx.family())
ctx.visit(comparator == IS_DISTINCT_FROM
? condition("{0} {is not} {1}", lhs, rhs)
: condition("{0} {is} {1}", lhs, rhs));
ctx.visit(condition("{0} {is not} {1}", arg1, arg2));
else
ctx.visit(new CompareCondition(lhs, rhs, comparator));
ctx.visit(arg1).sql(' ').visit(K_IS).sql(' ').visit(K_DISTINCT).sql(' ').visit(K_FROM).sql(' ').visit(arg2);
}
final Field<T> $arg1() {
return arg1;
}
final Field<T> $arg2() {
return arg2;
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof IsDistinctFrom) {
return
StringUtils.equals(arg1, ((IsDistinctFrom) that).arg1) &&
StringUtils.equals(arg2, ((IsDistinctFrom) that).arg2)
;
}
else
return super.equals(that);
}
}

View File

@ -0,0 +1,146 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.Internal.*;
import static org.jooq.impl.Keywords.*;
import static org.jooq.impl.Names.*;
import static org.jooq.impl.SQLDataType.*;
import static org.jooq.impl.Tools.*;
import static org.jooq.impl.Tools.BooleanDataKey.*;
import static org.jooq.impl.Tools.DataExtendedKey.*;
import static org.jooq.impl.Tools.DataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
/**
* The <code>IS NOT DISTINCT FROM</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class IsNotDistinctFrom<T>
extends
AbstractCondition
{
private final Field<T> arg1;
private final Field<T> arg2;
IsNotDistinctFrom(
Field<T> arg1,
Field<T> arg2
) {
this.arg1 = nullSafeNotNull(arg1, (DataType) OTHER);
this.arg2 = nullSafeNotNull(arg2, (DataType) OTHER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
if (arg1.getDataType().isEmbeddable() && arg2.getDataType().isEmbeddable())
ctx.visit(row(embeddedFields(arg1)).isNotDistinctFrom(row(embeddedFields(arg2))));
// [#3511] These dialects need to emulate the IS DISTINCT FROM predicate,
// optimally using INTERSECT...
// [#7222] [#7224] Make sure the columns are aliased
else if (IsDistinctFrom.EMULATE_DISTINCT_PREDICATE.contains(ctx.dialect()))
ctx.visit(exists(select(arg1.as("x")).intersect(select(arg2.as("x")))));
// MySQL knows the <=> operator
else if (IsDistinctFrom.SUPPORT_DISTINCT_WITH_ARROW.contains(ctx.dialect()))
ctx.visit(condition("{0} <=> {1}", arg1, arg2));
// SQLite knows the IS / IS NOT predicate
else if (SQLITE == ctx.family())
ctx.visit(condition("{0} {is} {1}", arg1, arg2));
else
ctx.visit(arg1).sql(' ').visit(K_IS).sql(' ').visit(K_NOT).sql(' ').visit(K_DISTINCT).sql(' ').visit(K_FROM).sql(' ').visit(arg2);
}
final Field<T> $arg1() {
return arg1;
}
final Field<T> $arg2() {
return arg2;
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof IsNotDistinctFrom) {
return
StringUtils.equals(arg1, ((IsNotDistinctFrom) that).arg1) &&
StringUtils.equals(arg2, ((IsNotDistinctFrom) that).arg2)
;
}
else
return super.equals(that);
}
}

View File

@ -6438,6 +6438,10 @@ final class Tools {