[jOOQ/jOOQ#13094] Throw an exception when calling ConvertedDataType.getArrayDataType() when the type has a custom Binding

This commit is contained in:
Lukas Eder 2022-02-18 10:25:55 +01:00
parent 5a1d813383
commit 72a0010df5
2 changed files with 28 additions and 2 deletions

View File

@ -77,6 +77,7 @@ import java.util.List;
import java.util.function.Function;
import org.jooq.Converters.UnknownType;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.DataTypeException;
import org.jooq.impl.DSL;
import org.jooq.impl.QOM.GenerationOption;
@ -176,9 +177,29 @@ public interface DataType<T> extends Named {
/**
* Retrieve the data type for an ARRAY of this data type.
* <p>
* Built-in data types, as well as custom data types that have a custom
* {@link #getConverter()} can be translated to array data types using
* {@link Converter#forArrays()}. Data types with custom
* {@link #getBinding()} cannot be translated to an array data type. Use
* this idiom, instead:
* <p>
* <code><pre>
* // Doesn't work
* DataType<UserType[]> t1 =
* SQLDataType.INTEGER.asConvertedDataType(binding).getArrayDataType();
*
* // Works
* DataType<UserType[]> t2 =
* SQLDataType.INTEGER.getArrayDataType().asConvertedDataType(arrayBinding);
* </pre></code>
*
* @throws DataTypeException When this data type has a custom
* {@link #getBinding()}, which cannot be automatically
* translated to an array {@link Binding}.
*/
@NotNull
DataType<T[]> getArrayDataType();
DataType<T[]> getArrayDataType() throws DataTypeException;
/**
* Retrieve the Java component type if this is an ARRAY type, or

View File

@ -53,6 +53,8 @@ import org.jooq.Record;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.SQLDialect;
import org.jooq.exception.DataTypeException;
import org.jooq.impl.DefaultBinding.InternalBinding;
import org.jooq.impl.QOM.GenerationOption;
import org.jetbrains.annotations.NotNull;
@ -136,7 +138,10 @@ final class ConvertedDataType<T, U> extends AbstractDataTypeX<U> {
@Override
public final DataType<U[]> getArrayDataType() {
return delegate.getArrayDataType().asConvertedDataType(Converters.forArrays(binding.converter()));
if (getBinding() instanceof InternalBinding)
return delegate.getArrayDataType().asConvertedDataType(Converters.forArrays(binding.converter()));
else
throw new DataTypeException("Cannot create array data types from custom data types with custom bindings.");
}
@Override