[jOOQ/jOOQ#11061] Parser should look up data types for bind variables from context

This commit is contained in:
Lukas Eder 2020-12-01 17:25:40 +01:00
parent b5a6ef98c6
commit b63b6e2710
2 changed files with 129 additions and 1 deletions

View File

@ -0,0 +1,121 @@
/*
* 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.Context;
import org.jooq.DataType;
import org.jooq.Param;
import org.jooq.ParamMode;
import org.jooq.conf.ParamType;
/**
* A {@link Param} wrapper object that allows for lazily initialising the value
* and its potentially required calls to custom converters.
* <p>
* See [#7742] for details.
*
* @author Lukas Eder
* @deprecated - [#11061] - 3.15.0 - This type has been introduced only because
* we have mutable {@link Param} types. Avoid reusing this type when
* not needed.
*/
@Deprecated
final class ConvertedVal<T> extends AbstractField<T> implements Param<T> {
private static final long serialVersionUID = 1258437916133900173L;
private final Param<?> delegate;
ConvertedVal(Param<?> delegate, DataType<T> type) {
super(delegate.getUnqualifiedName(), type);
this.delegate = delegate instanceof ConvertedVal ? ((ConvertedVal<?>) delegate).delegate : delegate;
}
// ------------------------------------------------------------------------
// XXX: Field API
// ------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(delegate);
}
// ------------------------------------------------------------------------
// XXX: Param API
// ------------------------------------------------------------------------
@Override
public final String getParamName() {
return delegate.getParamName();
}
@Override
public final T getValue() {
return getDataType().convert(delegate.getValue());
}
@Override
public final void setValue(T value) {
delegate.setConverted(value);
}
@Override
public final void setConverted(Object value) {
delegate.setConverted(value);
}
@Override
public final void setInline(boolean inline) {
delegate.setInline(inline);
}
@Override
public final boolean isInline() {
return delegate.isInline();
}
@Override
public final ParamType getParamType() {
return delegate.getParamType();
}
@Override
public final ParamMode getParamMode() {
return delegate.getParamMode();
}
}

View File

@ -40,6 +40,7 @@ package org.jooq.impl;
import static org.jooq.conf.ParamType.NAMED;
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
import static org.jooq.impl.QueryPartListView.wrap;
import static org.jooq.impl.SQLDataType.OTHER;
import static org.jooq.impl.Tools.embeddedFields;
import static org.jooq.impl.Tools.BooleanDataKey.DATA_LIST_ALREADY_INDENTED;
@ -49,6 +50,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.RenderContext;
import org.jooq.conf.ParamType;
import org.jooq.exception.DataAccessException;
@ -80,7 +82,7 @@ final class Val<T> extends AbstractParam<T> {
* [#10438] Convert this bind value to a new type.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
final <U> Val<U> convertTo(DataType<U> type) {
final <U> Field<U> convertTo(DataType<U> type) {
// [#10438] A user defined data type could was not provided explicitly,
// when wrapping a bind value in DSL::val or DSL::inline
@ -100,6 +102,11 @@ final class Val<T> extends AbstractParam<T> {
// [#10438] A data type conversion between built in data types was made
else if (type instanceof ConvertedDataType)
return convertTo0(type);
// [#11061] Infer bind value data types if they could not be defined eagerly, mostly from the parser.
// Cannot use convertTo0() here as long as Param.setValue() is possible (mutable Params)
else if (OTHER.equals(getDataType()))
return new ConvertedVal<>(this, type);
else
return (Val) this;
}