[jOOQ/jOOQ#12425] Move IS [ NOT ] NULL predicate to API generator

This commit is contained in:
Lukas Eder 2021-09-16 16:48:42 +02:00
parent 62d8102e90
commit 37c6e8033a
4 changed files with 228 additions and 60 deletions

View File

@ -710,6 +710,13 @@ extends
@Support
Condition isDistinctFrom(Field<T> arg2);
/**
* The <code>IS_NULL</code> operator.
*/
@NotNull
@Support
Condition isNull();
/**
* The <code>IS_NOT_DISTINCT_FROM</code> operator.
* <p>
@ -740,6 +747,13 @@ extends
@Support
Condition isNotDistinctFrom(Field<T> arg2);
/**
* The <code>IS_NOT_NULL</code> operator.
*/
@NotNull
@Support
Condition isNotNull();
// -------------------------------------------------------------------------
// XML predicates
// -------------------------------------------------------------------------
@ -1347,28 +1361,6 @@ extends
@Support
Field<T> divide(Field<? extends Number> value);
// ------------------------------------------------------------------------
// NULL predicates
// ------------------------------------------------------------------------
/**
* Create a condition to check this field against <code>null</code>.
* <p>
* SQL: <code>this is null</code>
*/
@NotNull
@Support
Condition isNull();
/**
* Create a condition to check this field against <code>null</code>.
* <p>
* SQL: <code>this is not null</code>
*/
@NotNull
@Support
Condition isNotNull();
// ------------------------------------------------------------------------
// LIKE_REGEX predicates
// ------------------------------------------------------------------------

View File

@ -357,6 +357,12 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
return new IsDistinctFrom(this, nullSafe(arg2, getDataType()));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNull() {
return new IsNull(this);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNotDistinctFrom(T arg2) {
@ -375,6 +381,12 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
return new IsNotDistinctFrom(this, nullSafe(arg2, getDataType()));
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition isNotNull() {
return new IsNotNull(this);
}
// -------------------------------------------------------------------------
// XML predicates
// -------------------------------------------------------------------------
@ -711,16 +723,6 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
// XXX: Conditions created from this field
// ------------------------------------------------------------------------
@Override
public final Condition isNull() {
return new IsNull(this, true);
}
@Override
public final Condition isNotNull() {
return new IsNull(this, false);
}
/**
* [#11200] Nest these constants to prevent initialisation deadlocks.
*/

View File

@ -0,0 +1,134 @@
/*
* 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 NULL</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unused" })
final class IsNotNull
extends
AbstractCondition
{
final Field<?> arg1;
IsNotNull(
Field<?> arg1
) {
this.arg1 = nullSafeNotNull(arg1, OTHER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
private static final Clause[] CLAUSES = { Clause.CONDITION, Clause.CONDITION_IS_NOT_NULL };
@Override
final boolean isNullable() {
return false;
}
@Override
public final void accept(Context<?> ctx) {
if (arg1.getDataType().isEmbeddable())
ctx.visit(row(embeddedFields(arg1)).isNotNull());
else
ctx.visit(arg1).sql(' ').visit(K_IS_NOT_NULL);
}
@Override
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof IsNotNull) {
return
StringUtils.equals(arg1, ((IsNotNull) that).arg1)
;
}
else
return super.equals(that);
}
}

View File

@ -35,38 +35,54 @@
*
*
*/
package org.jooq.impl;
import static org.jooq.Clause.CONDITION;
import static org.jooq.Clause.CONDITION_IS_NOT_NULL;
import static org.jooq.Clause.CONDITION_IS_NULL;
// ...
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.Keywords.K_IS_NOT_NULL;
import static org.jooq.impl.Keywords.K_IS_NULL;
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 org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Field;
/**
* @author Lukas Eder
* The <code>IS NULL</code> statement.
*/
final class IsNull extends AbstractCondition {
@SuppressWarnings({ "rawtypes", "unused" })
final class IsNull
extends
AbstractCondition
{
private static final Clause[] CLAUSES_NULL = { CONDITION, CONDITION_IS_NULL };
private static final Clause[] CLAUSES_NULL_NOT = { CONDITION, CONDITION_IS_NOT_NULL };
final Field<?> arg1;
private final Field<?> field;
private final boolean isNull;
IsNull(
Field<?> arg1
) {
IsNull(Field<?> field, boolean isNull) {
this.field = field;
this.isNull = isNull;
this.arg1 = nullSafeNotNull(arg1, OTHER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
private static final Clause[] CLAUSES = { Clause.CONDITION, Clause.CONDITION_IS_NULL };
@Override
final boolean isNullable() {
return false;
@ -74,21 +90,45 @@ final class IsNull extends AbstractCondition {
@Override
public final void accept(Context<?> ctx) {
if (field.getDataType().isEmbeddable())
if (isNull)
ctx.visit(row(embeddedFields(field)).isNull());
else
ctx.visit(row(embeddedFields(field)).isNotNull());
if (arg1.getDataType().isEmbeddable())
ctx.visit(row(embeddedFields(arg1)).isNull());
else
ctx.visit(field).sql(' ').visit(isNull ? K_IS_NULL : K_IS_NOT_NULL);
ctx.visit(arg1).sql(' ').visit(K_IS_NULL);
}
@Override
public final Clause[] clauses(Context<?> ctx) {
return isNull ? CLAUSES_NULL : CLAUSES_NULL_NOT;
return CLAUSES;
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof IsNull) {
return
StringUtils.equals(arg1, ((IsNull) that).arg1)
;
}
else
return super.equals(that);
}
}