[jOOQ/jOOQ#9263] Support Field<Boolean> with non-standard Converter

When a Boolean-typed field's `Converter` is not the default, then
`DSL#condition(Field<Boolean>)` will now render the SQL by comparing the
field to `true` (as converted by the field's converter). The same
applies to `DSL#not(Field<Boolean>)`.
This commit is contained in:
Knut Wannheden 2019-09-24 16:09:51 +02:00
parent 3085035a25
commit 292605a12f
6 changed files with 101 additions and 40 deletions

View File

@ -37,6 +37,7 @@
*/
package org.jooq;
import org.jooq.impl.IdentityConverter;
import org.jooq.impl.SQLDataType;
/**
@ -61,38 +62,7 @@ public class Converters<T, U> implements Converter<T, U> {
* Create an identity converter.
*/
public static <T> Converter<T, T> identity(final Class<T> type) {
return new Converter<T, T>() {
/**
* Generated UID
*/
private static final long serialVersionUID = -8331976721627671263L;
@Override
public final T from(T t) {
return t;
}
@Override
public final T to(T t) {
return t;
}
@Override
public final Class<T> fromType() {
return type;
}
@Override
public final Class<T> toType() {
return type;
}
@Override
public String toString() {
return "IdentityConverter [ " + fromType().getName() + " ]";
}
};
return new IdentityConverter<T>(type);
}
/**

View File

@ -39,9 +39,11 @@ package org.jooq.impl;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.not;
import static org.jooq.impl.SQLDataType.BOOLEAN;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.DataType;
/**
* @author Lukas Eder
@ -55,7 +57,11 @@ final class ConditionAsField extends AbstractField<Boolean> {
final Condition condition;
ConditionAsField(Condition condition) {
super(DSL.name(condition.toString()), SQLDataType.BOOLEAN);
this(condition, BOOLEAN);
}
ConditionAsField(Condition condition, DataType<Boolean> dataType) {
super(DSL.name(condition.toString()), dataType);
this.condition = condition;
}
@ -80,9 +86,9 @@ final class ConditionAsField extends AbstractField<Boolean> {
case FIREBIRD:
// [#3206] Correct implementation of three-valued logic is important here
ctx.visit(DSL.when(condition, inline(true))
.when(not(condition), inline(false))
.otherwise(inline((Boolean) null)));
ctx.visit(DSL.when(condition, inline(true, getDataType()))
.when(not(condition), inline(false, getDataType()))
.otherwise(inline((Boolean) null, getDataType())));
break;
// These databases can inline predicates in column expression contexts

View File

@ -78,7 +78,7 @@ final class FieldCondition extends AbstractCondition {
ctx.sql('(').visit(field).sql(" = ").visit(inline(true)).sql(')');
ctx.visit(field.eq(inline(true, field.getDataType())));
break;
@ -97,7 +97,7 @@ final class FieldCondition extends AbstractCondition {
case POSTGRES:
case SQLITE:
default:
ctx.visit(field);
ctx.visit(Tools.hasDefaultConverter(field) ? field : field.eq(inline(true, field.getDataType())));
break;
}
}

View File

@ -0,0 +1,75 @@
/*
* 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 org.jooq.Converter;
public final class IdentityConverter<T> implements Converter<T, T> {
private static final long serialVersionUID = -8331976721627671263L;
private final Class<T> type;
public IdentityConverter(Class<T> type) {
this.type = type;
}
@Override
public final T from(T t) {
return t;
}
@Override
public final T to(T t) {
return t;
}
@Override
public final Class<T> fromType() {
return type;
}
@Override
public final Class<T> toType() {
return type;
}
@Override
public String toString() {
return "IdentityConverter [ " + fromType().getName() + " ]";
}
}

View File

@ -78,7 +78,7 @@ final class NotField extends AbstractField<Boolean> {
ctx.visit(DSL.field(not(condition(field))));
ctx.visit(new ConditionAsField(not(condition(field)), field.getDataType()));
break;
@ -96,7 +96,10 @@ final class NotField extends AbstractField<Boolean> {
case POSTGRES:
case SQLITE:
default:
ctx.visit(K_NOT).sql('(').visit(field).sql(')');
ctx.visit(K_NOT)
.sql('(')
.visit(Tools.hasDefaultConverter(field) ? field : condition(field))
.sql(')');
break;
}
}

View File

@ -2976,6 +2976,13 @@ final class Tools {
return field instanceof Param;
}
/**
* Utility method to check whether a field uses a default {@link Converter}
*/
static final boolean hasDefaultConverter(Field<?> field) {
return field.getConverter() instanceof IdentityConverter;
}
/**
* Utility method to extract a value from a field
*/