[jOOQ/jOOQ#11491] Add a Settings.forceIntegerTypesOnZeroScaleDecimals

This commit is contained in:
Lukas Eder 2021-04-14 17:51:46 +02:00
parent b0c4723326
commit fa2c4edb9e
5 changed files with 72 additions and 5 deletions

View File

@ -38,6 +38,8 @@ public class Settings
private final static long serialVersionUID = 31500L;
@XmlElement(defaultValue = "true")
protected Boolean forceIntegerTypesOnZeroScaleDecimals = true;
@XmlElement(defaultValue = "true")
protected Boolean renderCatalog = true;
@XmlElement(defaultValue = "true")
protected Boolean renderSchema = true;
@ -303,6 +305,30 @@ public class Settings
@XmlElement(name = "schema")
protected List<ParseSearchSchema> parseSearchPath;
/**
* Historically, zero-scale decimal types are generated as their most appropriate, corresponding integer type (e.g. NUMBER(2, 0) and less: Byte). The same behaviour is replicated in the {@link org.jooq.Meta} API. This flag allows for turning off this feature.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isForceIntegerTypesOnZeroScaleDecimals() {
return forceIntegerTypesOnZeroScaleDecimals;
}
/**
* Sets the value of the forceIntegerTypesOnZeroScaleDecimals property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setForceIntegerTypesOnZeroScaleDecimals(Boolean value) {
this.forceIntegerTypesOnZeroScaleDecimals = value;
}
/**
* Whether any catalog name should be rendered at all.
* <p>
@ -2679,6 +2705,11 @@ public class Settings
this.parseSearchPath = parseSearchPath;
}
public Settings withForceIntegerTypesOnZeroScaleDecimals(Boolean value) {
setForceIntegerTypesOnZeroScaleDecimals(value);
return this;
}
public Settings withRenderCatalog(Boolean value) {
setRenderCatalog(value);
return this;
@ -3581,6 +3612,7 @@ public class Settings
@Override
public final void appendTo(XMLBuilder builder) {
builder.append("forceIntegerTypesOnZeroScaleDecimals", forceIntegerTypesOnZeroScaleDecimals);
builder.append("renderCatalog", renderCatalog);
builder.append("renderSchema", renderSchema);
builder.append("renderMapping", renderMapping);
@ -3713,6 +3745,15 @@ public class Settings
return false;
}
Settings other = ((Settings) that);
if (forceIntegerTypesOnZeroScaleDecimals == null) {
if (other.forceIntegerTypesOnZeroScaleDecimals!= null) {
return false;
}
} else {
if (!forceIntegerTypesOnZeroScaleDecimals.equals(other.forceIntegerTypesOnZeroScaleDecimals)) {
return false;
}
}
if (renderCatalog == null) {
if (other.renderCatalog!= null) {
return false;
@ -4719,6 +4760,7 @@ public class Settings
public int hashCode() {
final int prime = 31;
int result = 1;
result = ((prime*result)+((forceIntegerTypesOnZeroScaleDecimals == null)? 0 :forceIntegerTypesOnZeroScaleDecimals.hashCode()));
result = ((prime*result)+((renderCatalog == null)? 0 :renderCatalog.hashCode()));
result = ((prime*result)+((renderSchema == null)? 0 :renderSchema.hashCode()));
result = ((prime*result)+((renderMapping == null)? 0 :renderMapping.hashCode()));

View File

@ -673,7 +673,7 @@ public class DefaultDataType<T> extends AbstractDataTypeX<T> {
public static final <T> DataType<T> getDataType(SQLDialect dialect, Class<T> type, DataType<T> fallbackDataType) {
// Treat primitive types the same way as their respective wrapper types
type = (Class<T>) wrapper(type);
type = wrapper(type);
// Recurse for arrays
if (byte[].class != type && type.isArray()) {
@ -743,6 +743,13 @@ public class DefaultDataType<T> extends AbstractDataTypeX<T> {
* Convert a type name (using precision and scale) into a Java class
*/
public static final DataType<?> getDataType(SQLDialect dialect, String t, int p, int s) throws SQLDialectNotSupportedException {
return getDataType(dialect, t, p, s, true);
}
/**
* Convert a type name (using precision and scale) into a Java class
*/
public static final DataType<?> getDataType(SQLDialect dialect, String t, int p, int s, boolean forceIntegerTypesOnZeroScaleDecimals) throws SQLDialectNotSupportedException {
DataType<?> result = DefaultDataType.getDataType(dialect, t);
boolean array = result.isArray();
@ -750,7 +757,7 @@ public class DefaultDataType<T> extends AbstractDataTypeX<T> {
if (array)
result = result.getArrayComponentDataType();
if (result.getType() == BigDecimal.class)
if (forceIntegerTypesOnZeroScaleDecimals && result.getType() == BigDecimal.class)
result = DefaultDataType.getDataType(dialect, getNumericClass(p, s));
// [#10809] Use dialect only for lookup, don't report the dialect-specific type

View File

@ -37,6 +37,7 @@
*/
package org.jooq.impl;
import static java.lang.Boolean.FALSE;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
@ -134,7 +135,13 @@ final class MetaDataFieldProvider implements Serializable {
String type = meta.getColumnTypeName(i);
try {
dataType = DefaultDataType.getDataType(configuration.family(), type, precision, scale);
dataType = DefaultDataType.getDataType(
configuration.family(),
type,
precision,
scale,
!FALSE.equals(configuration.settings().isForceIntegerTypesOnZeroScaleDecimals())
);
if (dataType.hasPrecision())
dataType = dataType.precision(precision);

View File

@ -585,7 +585,8 @@ final class MetaImpl extends AbstractMeta {
family(),
record.get(3, String.class),
record.get(4, int.class),
record.get(5, int.class)
record.get(5, int.class),
!FALSE.equals(settings().isForceIntegerTypesOnZeroScaleDecimals())
),
record.get(6, Long.class),
record.get(7, Long.class),
@ -1047,7 +1048,13 @@ final class MetaImpl extends AbstractMeta {
// TODO: Exception handling should be moved inside SQLDataType
DataType type = null;
try {
type = DefaultDataType.getDataType(family(), typeName, precision, scale);
type = DefaultDataType.getDataType(
family(),
typeName,
precision,
scale,
!FALSE.equals(settings().isForceIntegerTypesOnZeroScaleDecimals())
);
// [#10207] Ignore secondary identity columns, as allowed e.g. in PostgreSQL
if (isAutoIncrement)

View File

@ -14,6 +14,10 @@
<complexType name="Settings">
<annotation><appinfo><jxb:class><jxb:javadoc><![CDATA[Settings that influence the way jOOQ renders SQL code.]]></jxb:javadoc></jxb:class></appinfo></annotation>
<all>
<element name="forceIntegerTypesOnZeroScaleDecimals" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Historically, zero-scale decimal types are generated as their most appropriate, corresponding integer type (e.g. NUMBER(2, 0) and less: Byte). The same behaviour is replicated in the {@link org.jooq.Meta} API. This flag allows for turning off this feature.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderCatalog" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether any catalog name should be rendered at all.
<p>