[jOOQ/jOOQ#10179] Boolean field emulation should be NOT NULL aware
Fix this also for AND, OR, NOT, TRUE, FALSE
This commit is contained in:
parent
c30c06f828
commit
6bfda07a84
@ -63,6 +63,15 @@ abstract class AbstractCondition extends AbstractQueryPart implements Condition
|
||||
|
||||
AbstractCondition() {}
|
||||
|
||||
/**
|
||||
* [#10179] Subclasses may override this method to indicate that the
|
||||
* condition may produce <code>TRUE</code>, <code>FALSE</code>, or
|
||||
* <code>NULL</code>.
|
||||
*/
|
||||
boolean isNullable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clause[] clauses(Context<?> ctx) {
|
||||
return CLAUSES;
|
||||
|
||||
@ -110,6 +110,15 @@ final class CombinedCondition extends AbstractCondition {
|
||||
return falseCondition();
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
for (Condition condition : conditions)
|
||||
if (!(condition instanceof AbstractCondition) || ((AbstractCondition) condition).isNullable())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private CombinedCondition(Operator operator, int size) {
|
||||
if (operator == null)
|
||||
throw new IllegalArgumentException("The argument 'operator' must not be null");
|
||||
|
||||
@ -81,15 +81,14 @@ final class ConditionAsField extends AbstractField<Boolean> {
|
||||
|
||||
|
||||
// [#10179] Avoid 3VL when not necessary
|
||||
if (condition instanceof NotNullCondition)
|
||||
if (condition instanceof AbstractCondition && !((AbstractCondition) condition).isNullable())
|
||||
ctx.visit(DSL.when(condition, inline(true))
|
||||
.else_(inline(false)));
|
||||
|
||||
// [#3206] Implement 3VL if necessary or unknown
|
||||
else
|
||||
ctx.visit(DSL.when(condition, inline(true))
|
||||
.when(not(condition), inline(false))
|
||||
.else_(inline((Boolean) null)));
|
||||
.when(not(condition), inline(false)));
|
||||
break;
|
||||
|
||||
// These databases can inline predicates in column expression contexts
|
||||
|
||||
@ -51,7 +51,7 @@ import org.jooq.Select;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class ExistsCondition extends AbstractCondition implements NotNullCondition {
|
||||
final class ExistsCondition extends AbstractCondition {
|
||||
|
||||
private static final long serialVersionUID = 5678338161136603292L;
|
||||
private static final Clause[] CLAUSES_EXISTS = { CONDITION, CONDITION_EXISTS };
|
||||
@ -65,6 +65,11 @@ final class ExistsCondition extends AbstractCondition implements NotNullConditio
|
||||
this.exists = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(exists ? K_EXISTS : K_NOT_EXISTS)
|
||||
|
||||
@ -53,6 +53,11 @@ final class FalseCondition extends AbstractCondition implements False {
|
||||
private static final Clause[] CLAUSES = { CONDITION, CONDITION_COMPARISON };
|
||||
static final FalseCondition INSTANCE = new FalseCondition();
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.sql("1 = 0");
|
||||
|
||||
@ -81,7 +81,7 @@ import org.jooq.SQLDialect;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class IsDistinctFrom<T> extends AbstractCondition implements NotNullCondition {
|
||||
final class IsDistinctFrom<T> extends AbstractCondition {
|
||||
|
||||
|
||||
/**
|
||||
@ -105,6 +105,11 @@ final class IsDistinctFrom<T> extends AbstractCondition implements NotNullCondit
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
if (isEmbeddable(lhs) && isEmbeddable(rhs))
|
||||
|
||||
@ -47,7 +47,7 @@ import org.jooq.Field;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class IsDocument extends AbstractCondition implements NotNullCondition {
|
||||
final class IsDocument extends AbstractCondition {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -61,6 +61,11 @@ final class IsDocument extends AbstractCondition implements NotNullCondition {
|
||||
this.isDocument = isDocument;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(field).sql(' ').visit(isDocument ? K_IS_DOCUMENT : K_IS_NOT_DOCUMENT);
|
||||
|
||||
@ -51,7 +51,7 @@ import org.jooq.Field;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class IsJSON extends AbstractCondition implements NotNullCondition {
|
||||
final class IsJSON extends AbstractCondition {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -65,6 +65,11 @@ final class IsJSON extends AbstractCondition implements NotNullCondition {
|
||||
this.isJSON = isJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
switch (ctx.family()) {
|
||||
|
||||
@ -54,7 +54,7 @@ import org.jooq.Field;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class IsNull extends AbstractCondition implements NotNullCondition {
|
||||
final class IsNull extends AbstractCondition {
|
||||
|
||||
private static final long serialVersionUID = -747240442279619486L;
|
||||
private static final Clause[] CLAUSES_NULL = { CONDITION, CONDITION_IS_NULL };
|
||||
@ -68,6 +68,11 @@ final class IsNull extends AbstractCondition implements NotNullCondition {
|
||||
this.isNull = isNull;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
if (isEmbeddable(field))
|
||||
|
||||
@ -53,6 +53,11 @@ final class NoCondition extends AbstractCondition {
|
||||
private static final Clause[] CLAUSES = { CONDITION, CONDITION_COMPARISON };
|
||||
static final NoCondition INSTANCE = new NoCondition();
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.sql("1 = 1");
|
||||
|
||||
@ -56,9 +56,14 @@ final class NotCondition extends AbstractCondition {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isNullable() {
|
||||
return !(condition instanceof AbstractCondition) || ((AbstractCondition) condition).isNullable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(K_NOT).sql('(').visit(condition).sql(')');
|
||||
ctx.visit(K_NOT).sql(" (").visit(condition).sql(')');
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -1,46 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* A marker interface for conditions that can only be <code>TRUE</code> or
|
||||
* <code>FALSE</code>, never <code>NULL</code>.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
interface NotNullCondition {}
|
||||
@ -75,7 +75,7 @@ import org.jooq.SQLDialect;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class RowIsDistinctFrom extends AbstractCondition implements NotNullCondition {
|
||||
final class RowIsDistinctFrom extends AbstractCondition {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -98,6 +98,11 @@ final class RowIsDistinctFrom extends AbstractCondition implements NotNullCondit
|
||||
this.not = not;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(delegate(ctx.configuration()));
|
||||
|
||||
@ -82,7 +82,7 @@ import org.jooq.Table;
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class RowIsNull extends AbstractCondition implements NotNullCondition {
|
||||
final class RowIsNull extends AbstractCondition {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -110,6 +110,11 @@ final class RowIsNull extends AbstractCondition implements NotNullCondition {
|
||||
this.isNull = isNull;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
|
||||
|
||||
@ -54,6 +54,11 @@ final class TrueCondition extends AbstractCondition implements True {
|
||||
private static final Clause[] CLAUSES = { CONDITION, CONDITION_COMPARISON };
|
||||
static final TrueCondition INSTANCE = new TrueCondition();
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.sql("1 = 1");
|
||||
|
||||
@ -54,7 +54,7 @@ import org.jooq.Table;
|
||||
/**
|
||||
* @author Knut Wannheden
|
||||
*/
|
||||
final class UniqueCondition extends AbstractCondition implements NotNullCondition {
|
||||
final class UniqueCondition extends AbstractCondition {
|
||||
|
||||
private static final long serialVersionUID = -5560973283201522844L;
|
||||
|
||||
@ -66,6 +66,11 @@ final class UniqueCondition extends AbstractCondition implements NotNullConditio
|
||||
this.unique = unique;
|
||||
}
|
||||
|
||||
@Override
|
||||
final boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(delegate(ctx));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user