[jOOQ/jOOQ#11812] Fixed formatting of ROW(X, ROW(Y, Z)[]) types

This commit is contained in:
Lukas Eder 2021-05-03 09:48:19 +02:00
parent 2087434cfe
commit ea80188d20
2 changed files with 18 additions and 21 deletions

View File

@ -39,6 +39,7 @@ package org.jooq.impl;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.stream.Collectors.joining;
import static org.jooq.XMLFormat.RecordFormat.COLUMN_NAME_ELEMENTS;
import static org.jooq.XMLFormat.RecordFormat.VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE;
import static org.jooq.conf.SettingsTools.renderLocale;
@ -1474,20 +1475,20 @@ abstract class AbstractResult<R extends Record> extends AbstractFormattable impl
formatted += DatatypeConverter.printBase64Binary((byte[]) value);
}
else if (value.getClass().isArray()) {
// [#6545] Nested arrays
if (value.getClass().getComponentType().isArray())
formatted += Arrays.deepToString((Object[]) value);
else
formatted += Arrays.toString((Object[]) value);
// [#6545] Nested arrays are handled recursively
formatted += Arrays.stream((Object[]) value).map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
}
else if (value instanceof EnumType) {
formatted += ((EnumType) value).getLiteral();
}
else if (value instanceof List) {
formatted += ((List<?>) value).stream().map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
}
else if (value instanceof Record) {
formatted += Arrays
.stream(((Record) value).valuesRow().fields())
.map(f -> format0(f, false, visual))
.collect(Collectors.joining(", ", "(", ")"));
.collect(joining(", ", "(", ")"));
}
// [#6080] Support formatting of nested ROWs
else if (value instanceof Param) {

View File

@ -133,6 +133,7 @@ import static org.jooq.tools.jdbc.JDBCUtils.safeFree;
import static org.jooq.tools.jdbc.JDBCUtils.wasNull;
import static org.jooq.tools.reflect.Reflect.on;
import static org.jooq.tools.reflect.Reflect.onClass;
import static org.jooq.util.postgres.PostgresUtils.toPGArray;
import static org.jooq.util.postgres.PostgresUtils.toPGArrayString;
import static org.jooq.util.postgres.PostgresUtils.toPGInterval;
@ -3501,7 +3502,10 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
else if (EnumType.class.isAssignableFrom(type))
return (T) DefaultEnumTypeBinding.getEnumType((Class<EnumType>) type, string);
else if (Record.class.isAssignableFrom(type) && !InternalRecord.class.isAssignableFrom(type))
else if (Record.class.isAssignableFrom(type)
// [#11812] UDTRecords/TableRecords or InternalRecords that don't have an explicit converter
&& (!InternalRecord.class.isAssignableFrom(type) || type == converter.fromType()))
return (T) pgNewRecord(type, null, string);
else if (type == Object.class)
return (T) string;
@ -3571,25 +3575,17 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
* @return The converted array
*/
private static final Object[] pgNewArray(Class<?> type, String string) {
if (string == null) {
if (string == null)
return null;
}
try {
Class<?> component = type.getComponentType();
List<String> values = PostgresUtils.toPGArray(string);
if (values.isEmpty()) {
return (Object[]) java.lang.reflect.Array.newInstance(component, 0);
}
else {
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(component, values.size());
for (int i = 0; i < values.size(); i++)
result[i] = pgFromString(type.getComponentType(), values.get(i));
return result;
}
return Tools.map(
toPGArray(string),
v -> pgFromString(component, v),
size -> (Object[]) java.lang.reflect.Array.newInstance(component, size)
);
}
catch (Exception e) {
throw new DataTypeException("Error while creating array", e);