[jOOQ/jOOQ#17570] Replace usage of internal castIfNeeded(Field<?>, Class<T>) utility by castIfNeeded(Field<?>, DataType<T>)

This commit is contained in:
Lukas Eder 2024-11-07 10:39:06 +01:00
parent 2aa5a282e5
commit c9ca7aa207
11 changed files with 22 additions and 41 deletions

View File

@ -41,6 +41,7 @@ import static org.jooq.Clause.FIELD;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.ExpressionOperator.ADD;
import static org.jooq.impl.ExpressionOperator.SUBTRACT;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.castIfNeeded;
import static org.jooq.impl.Tools.map;
@ -1232,7 +1233,7 @@ implements
else if (t.isBoolean())
return eq(inline(true).coerce(this));
else
return castIfNeeded(this, String.class).in(BooleanValues.TRUE_VALUES);
return castIfNeeded(this, VARCHAR).in(BooleanValues.TRUE_VALUES);
}
@Override
@ -1246,7 +1247,7 @@ implements
else if (t.isBoolean())
return eq(inline(false).coerce(this));
else
return castIfNeeded(this, String.class).in(BooleanValues.FALSE_VALUES);
return castIfNeeded(this, VARCHAR).in(BooleanValues.FALSE_VALUES);
}
@Override
@ -1504,7 +1505,7 @@ implements
@Override
public final Condition equalIgnoreCase(Field<String> value) {
return DSL.lower(castIfNeeded(this, String.class)).equal(DSL.lower(value));
return DSL.lower(castIfNeeded(this, VARCHAR)).equal(DSL.lower(value));
}
@Override
@ -1514,7 +1515,7 @@ implements
@Override
public final Condition notEqualIgnoreCase(Field<String> value) {
return DSL.lower(castIfNeeded(this, String.class)).notEqual(DSL.lower(value));
return DSL.lower(castIfNeeded(this, VARCHAR)).notEqual(DSL.lower(value));
}
@Override

View File

@ -42,6 +42,7 @@ import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.ExpressionOperator.ADD;
import static org.jooq.impl.ExpressionOperator.CONCAT;
import static org.jooq.impl.Names.N_CONCAT;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.castAllIfNeeded;
@ -137,7 +138,7 @@ final class Concat extends AbstractField<String> implements QOM.Concat {
private final List<Field<String>> cast(Context<?> ctx, List<Field<String>> result) {
// [#461] Type cast the concat expression, if this isn't a VARCHAR field
for (Field<String> f : castAllIfNeeded(arguments, String.class)) {
for (Field<String> f : castAllIfNeeded(arguments, VARCHAR)) {
if (f instanceof Concat c)
c.cast(ctx, result);
else

View File

@ -65,6 +65,7 @@ import static org.jooq.impl.Names.N_SQL_TSI_SECOND;
import static org.jooq.impl.Names.N_STRFTIME;
import static org.jooq.impl.Names.N_TIMESTAMPDIFF;
import static org.jooq.impl.Names.N_TIMESTAMP_DIFF;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.SQLDataType.TIMESTAMP;
import static org.jooq.impl.Tools.castIfNeeded;
@ -308,7 +309,7 @@ final class DateDiff<T> extends AbstractField<Integer> implements UNotYetImpleme
return;
}
ctx.visit(castIfNeeded(endDate.minus(startDate), Integer.class));
ctx.visit(castIfNeeded(endDate.minus(startDate), INTEGER));
}
private final Field<Integer> partDiff(DatePart p) {

View File

@ -144,7 +144,7 @@ implements
case FIREBIRD:
ctx.visit(idiv(
imul(
castIfNeeded(radians, BigDecimal.class),
castIfNeeded(radians, NUMERIC),
inline(180)
),
pi()

View File

@ -67,7 +67,9 @@ import static org.jooq.impl.Names.N_TO_CHAR;
import static org.jooq.impl.Names.N_TO_NUMBER;
import static org.jooq.impl.Names.N_TRUNC;
import static org.jooq.impl.Names.N_WEEKDAY;
import static org.jooq.impl.SQLDataType.DATE;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.SQLDataType.TIMESTAMP;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.castIfNeeded;

View File

@ -179,18 +179,18 @@ implements
case NOT_LIKE:
case NOT_SIMILAR_TO:
if (!arg1.getDataType().isString() && REQUIRES_CAST_ON_LIKE.contains(ctx.dialect()))
arg1 = castIfNeeded(arg1, String.class);
arg1 = castIfNeeded(arg1, VARCHAR);
if (!arg2.getDataType().isString() && REQUIRES_CAST_ON_LIKE.contains(ctx.dialect()))
arg2 = castIfNeeded(arg2, String.class);
arg2 = castIfNeeded(arg2, VARCHAR);
break;
case LIKE_IGNORE_CASE:
case NOT_LIKE_IGNORE_CASE:
if (!arg1.getDataType().isString())
arg1 = castIfNeeded(arg1, String.class);
arg1 = castIfNeeded(arg1, VARCHAR);
if (!arg2.getDataType().isString())
arg2 = castIfNeeded(arg2, String.class);
arg2 = castIfNeeded(arg2, VARCHAR);
break;
}

View File

@ -244,7 +244,7 @@ implements
ctx.sql('(');
// The explicit cast is needed in Postgres
QueryPartListView<Field<?>> args = wrap(castIfNeeded((Field<?>) arguments.get(0), String.class));
QueryPartListView<Field<?>> args = wrap(castIfNeeded((Field<?>) arguments.get(0), VARCHAR));
acceptArguments1(ctx, args);
if (arguments.size() > 1)

View File

@ -129,7 +129,7 @@ implements
case FIREBIRD:
ctx.visit(idiv(
imul(
castIfNeeded(degrees, BigDecimal.class),
castIfNeeded(degrees, NUMERIC),
pi()
),
inline(180)

View File

@ -168,7 +168,7 @@ final class TimestampDiff<T> extends AbstractField<DayToSecond> implements QOM.T
default:
// Default implementation for equals() and hashCode()
ctx.visit(castIfNeeded(timestamp1.sub(timestamp2), DayToSecond.class));
ctx.visit(castIfNeeded(timestamp1.sub(timestamp2), INTERVALDAYTOSECOND));
break;
}
}

View File

@ -1921,19 +1921,6 @@ final class Tools {
return new IllegalArgumentException("Cannot interpret argument of type " + value.getClass() + " as a Field: " + value);
}
/**
* [#461] [#473] [#2597] [#8234] Some internals need a cast only if necessary.
*/
@SuppressWarnings("unchecked")
static final <T> Field<T>[] castAllIfNeeded(Field<?>[] fields, Class<T> type) {
Field<T>[] castFields = new Field[fields.length];
for (int i = 0; i < fields.length; i++)
castFields[i] = castIfNeeded(fields[i], type);
return castFields;
}
/**
* [#461] [#473] [#2597] [#8234] Some internals need a cast only if necessary.
*/
@ -1947,17 +1934,6 @@ final class Tools {
return castFields;
}
/**
* [#461] [#473] [#2597] [#8234] Some internals need a cast only if necessary.
*/
@SuppressWarnings("unchecked")
static final <T> Field<T> castIfNeeded(Field<?> field, Class<T> type) {
if (field.getType().equals(type))
return (Field<T>) field;
else
return field.cast(type);
}
/**
* [#461] [#473] [#2597] [#8234] Some internals need a cast only if necessary.
*/
@ -4072,9 +4048,8 @@ final class Tools {
return escape((Field<String>) field, ESCAPE);
}
}
else {
return castIfNeeded(field, String.class);
}
else
return castIfNeeded(field, VARCHAR);
}
static final boolean isParam(Field<?> field) {

View File

@ -45,6 +45,7 @@ import static org.jooq.impl.Keywords.K_CAST;
import static org.jooq.impl.Names.N_DATETIME_TRUNC;
import static org.jooq.impl.Names.N_DATE_TRUNC;
import static org.jooq.impl.Names.N_TRUNC;
import static org.jooq.impl.SQLDataType.DATE;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.castIfNeeded;