HHH-16125 remove DDL generation stuff from converters

it never belonged there!
This commit is contained in:
Gavin 2023-04-28 12:54:05 +02:00 committed by Gavin King
parent d075093ebf
commit db4a1bb6ef
9 changed files with 83 additions and 86 deletions

View File

@ -811,6 +811,23 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
return columnName + " between " + min + " and " + max;
}
/**
* Render a SQL check condition for a column that represents an enumerated value
* by its {@linkplain jakarta.persistence.EnumType#ORDINAL ordinal representation}.
*
* @return a SQL expression that will occur in a {@code check} constraint
*/
public String getCheckCondition(String columnName, long[] values) {
StringBuilder check = new StringBuilder();
check.append( columnName ).append( " in (" );
String separator = "";
for ( long value : values ) {
check.append( separator ).append( value );
separator = ",";
}
return check.append( ')' ).toString();
}
@Override
public void contributeFunctions(FunctionContributions functionContributions) {
initializeFunctionRegistry( functionContributions );

View File

@ -338,11 +338,9 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
column.setSqlTypeCode( resolution.getJdbcType().getDdlTypeCode() );
}
if ( resolution.getValueConverter() != null ) {
final String declaration = resolution.getLegacyResolvedBasicType().getSpecializedTypeDeclaration( dialect );
if ( declaration != null ) {
column.setSpecializedTypeDeclaration( declaration );
}
final String declaration = resolution.getLegacyResolvedBasicType().getSpecializedTypeDeclaration( dialect );
if ( declaration != null ) {
column.setSpecializedTypeDeclaration( declaration );
}
if ( dialect.supportsColumnCheck() ) {

View File

@ -146,28 +146,17 @@ public interface BasicType<T> extends Type, BasicDomainType<T>, MappingType, Bas
*/
@Incubating
default String getCheckCondition(String columnName, Dialect dialect) {
final BasicValueConverter<T, ?> valueConverter = getValueConverter();
String checkCondition = null;
if ( valueConverter != null ) {
checkCondition = valueConverter.getCheckCondition(
columnName,
getJdbcType(),
dialect
);
}
if ( checkCondition == null ) {
checkCondition = getJdbcType().getCheckCondition(
columnName,
getMappedJavaType(),
valueConverter,
dialect
);
}
String checkCondition = getJdbcType().getCheckCondition(
columnName,
getMappedJavaType(),
getValueConverter(),
dialect
);
if ( checkCondition == null ) {
checkCondition = getMappedJavaType().getCheckCondition(
columnName,
getJdbcType(),
valueConverter,
getValueConverter(),
dialect
);
}
@ -176,8 +165,7 @@ public interface BasicType<T> extends Type, BasicDomainType<T>, MappingType, Bas
@Incubating
default String getSpecializedTypeDeclaration(Dialect dialect) {
final BasicValueConverter<T, ?> valueConverter = getValueConverter();
return valueConverter == null ? null : valueConverter.getSpecializedTypeDeclaration( getJdbcType(), dialect );
return getMappedJavaType().getSpecializedTypeDeclaration( getJdbcType(), getValueConverter(), dialect );
}
@Override

View File

@ -7,12 +7,10 @@
package org.hibernate.type;
import jakarta.persistence.AttributeConverter;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.BooleanJavaType;
import org.hibernate.type.descriptor.java.CharacterJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
/**
* Abstract supertype of converters which map {@link Boolean} to {@link Character}.
@ -45,15 +43,5 @@ public abstract class CharBooleanConverter
return CharacterJavaType.INSTANCE;
}
@Override
public String getCheckCondition(String columnName, JdbcType jdbcType, Dialect dialect) {
return dialect.getCheckCondition( columnName, getValues() );
}
@Override
public String getSpecializedTypeDeclaration(JdbcType jdbcType, Dialect dialect) {
return dialect.getEnumTypeDeclaration( null, getValues() );
}
protected abstract String[] getValues();
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.type;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.BooleanJavaType;
import org.hibernate.type.descriptor.java.IntegerJavaType;
@ -14,7 +13,6 @@ import org.hibernate.type.descriptor.java.JavaType;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import org.hibernate.type.descriptor.jdbc.JdbcType;
/**
* Handles conversion to/from {@code Boolean} as {@code 0} (false) or {@code 1} (true)
@ -74,9 +72,4 @@ public class NumericBooleanConverter implements AttributeConverter<Boolean, Inte
public JavaType<Integer> getRelationalJavaType() {
return IntegerJavaType.INSTANCE;
}
@Override
public String getCheckCondition(String columnName, JdbcType jdbcType, Dialect dialect) {
return columnName + " in (0,1)";
}
}

View File

@ -110,28 +110,6 @@ public class JpaAttributeConverterImpl<O,R> implements JpaAttributeConverter<O,R
}
}
@Override
public String getCheckCondition(String columnName, JdbcType sqlType, Dialect dialect) {
if ( BasicValueConverter.class.isAssignableFrom( attributeConverterBean.getBeanClass() ) ) {
return ((BasicValueConverter<?, ?>) attributeConverterBean.getBeanInstance())
.getCheckCondition( columnName, sqlType, dialect );
}
else {
return null;
}
}
@Override
public String getSpecializedTypeDeclaration(JdbcType jdbcType, Dialect dialect) {
if ( BasicValueConverter.class.isAssignableFrom( attributeConverterBean.getBeanClass() ) ) {
return ((BasicValueConverter<?, ?>) attributeConverterBean.getBeanInstance())
.getSpecializedTypeDeclaration( jdbcType, dialect );
}
else {
return null;
}
}
@Override
public JavaType<? extends AttributeConverter<O, R>> getConverterJavaType() {
return converterJtd;

View File

@ -47,23 +47,4 @@ public interface BasicValueConverter<D,R> {
* Descriptor for the Java type for the relational portion of this converter
*/
JavaType<R> getRelationalJavaType();
/**
* The check constraint that should be added to the column
* definition in generated DDL.
*
* @param columnName the name of the column
* @param sqlType the {@link JdbcType} of the mapped column
* @param dialect the SQL {@link Dialect}
* @return a check constraint condition or null
*/
@Incubating
default String getCheckCondition(String columnName, JdbcType sqlType, Dialect dialect) {
return null;
}
@Incubating
default String getSpecializedTypeDeclaration(JdbcType jdbcType, Dialect dialect) {
return null;
}
}

View File

@ -9,6 +9,7 @@ package org.hibernate.type.descriptor.java;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.CharSequenceHelper;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.spi.PrimitiveJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -147,7 +148,7 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
}
@Override
public Class getPrimitiveClass() {
public Class<?> getPrimitiveClass() {
return boolean.class;
}
@ -180,4 +181,48 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
public int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) {
return 0;
}
@Override @Deprecated
public String getSpecializedTypeDeclaration(JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
if ( converter != null && dialect.hasNativeEnums() ) {
if ( jdbcType.isString() ) {
@SuppressWarnings("unchecked")
BasicValueConverter<Boolean, ?> stringConverter = (BasicValueConverter<Boolean, ?>) converter;
String[] values = new String[] {
stringConverter.toRelationalValue(false).toString(),
stringConverter.toRelationalValue(true).toString()
};
return dialect.getEnumTypeDeclaration( null, values );
}
}
return null;
}
@Override
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
if ( converter != null ) {
if ( jdbcType.isString() ) {
@SuppressWarnings("unchecked")
BasicValueConverter<Boolean, ?> stringConverter =
(BasicValueConverter<Boolean, ?>) converter;
String[] values = new String[] {
stringConverter.toRelationalValue(false).toString(),
stringConverter.toRelationalValue(true).toString()
};
return dialect.getCheckCondition( columnName, values );
}
else if ( jdbcType.isInteger() ) {
@SuppressWarnings("unchecked")
BasicValueConverter<Boolean, ? extends Number> numericConverter =
(BasicValueConverter<Boolean, ? extends Number>) converter;
long[] values = new long[] {
numericConverter.toRelationalValue(false).longValue(),
numericConverter.toRelationalValue(true).longValue()
};
return dialect.getCheckCondition( columnName, values );
}
}
return null;
}
}

View File

@ -344,7 +344,16 @@ public interface JavaType<T> extends Serializable {
* @return a check constraint condition or null
* @since 6.2
*/
@Incubating
default String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
return null;
}
/**
* @deprecated this was an experimental approach that we have moved away from
*/
@Incubating @Deprecated
default String getSpecializedTypeDeclaration(JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
return null;
}
}