From 1f9add32c27781ce753c19ba0a85e94f5c3f2356 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 23 Aug 2024 13:14:23 +0200 Subject: [PATCH] HHH-18512 cleanup code surrounding custom PostgreSQL and Oracle types Signed-off-by: Gavin King --- .../java/org/hibernate/dialect/Dialect.java | 7 + .../dialect/OracleArrayJdbcType.java | 147 ++++++++++-------- .../org/hibernate/dialect/OracleDialect.java | 21 +++ .../hibernate/dialect/OracleEnumJdbcType.java | 56 ++----- .../dialect/OracleOrdinalEnumJdbcType.java | 59 +------ .../dialect/PostgreSQLArrayJdbcType.java | 5 +- .../dialect/PostgreSQLEnumJdbcType.java | 33 +--- .../PostgreSQLOrdinalEnumJdbcType.java | 25 --- .../type/descriptor/jdbc/ArrayJdbcType.java | 56 +++---- .../type/descriptor/jdbc/JdbcType.java | 13 -- .../sql/internal/ArrayDdlTypeImpl.java | 8 +- .../descriptor/sql/internal/DdlTypeImpl.java | 3 +- 12 files changed, 162 insertions(+), 271 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index 42744df085..8de47394a7 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -5207,6 +5207,13 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun Integer precision, Integer scale, Long length); + + default Size resolveSize( + JdbcType jdbcType, + JavaType javaType, + Size size) { + return resolveSize( jdbcType, javaType, size.getPrecision(), size.getScale(), size.getLength() ); + } } public class SizeStrategyImpl implements SizeStrategy { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java index 87537cc7db..fbfef65f31 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java @@ -14,8 +14,8 @@ import java.sql.Types; import java.util.Locale; import org.hibernate.HibernateException; -import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.relational.Database; +import org.hibernate.boot.model.relational.Namespace; import org.hibernate.engine.jdbc.Size; import org.hibernate.mapping.UserDefinedArrayType; import org.hibernate.type.BasicType; @@ -31,14 +31,16 @@ import org.hibernate.type.descriptor.jdbc.BasicBinder; import org.hibernate.type.descriptor.jdbc.BasicExtractor; import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; import org.hibernate.type.descriptor.jdbc.SqlTypedJdbcType; import org.hibernate.type.descriptor.jdbc.StructJdbcType; import org.hibernate.type.internal.BasicTypeImpl; -import org.hibernate.type.spi.TypeConfiguration; import oracle.jdbc.OracleConnection; +import org.hibernate.type.spi.TypeConfiguration; import static java.sql.Types.ARRAY; +import static org.hibernate.boot.model.naming.Identifier.toIdentifier; /** * Descriptor for {@link Types#ARRAY ARRAY} handling. @@ -69,14 +71,16 @@ public class OracleArrayJdbcType extends ArrayJdbcType implements SqlTypedJdbcTy @Override public ValueBinder getBinder(final JavaType javaTypeDescriptor) { - //noinspection unchecked - final ValueBinder elementBinder = getElementJdbcType().getBinder( ( (BasicPluralJavaType) javaTypeDescriptor ).getElementJavaType() ); + @SuppressWarnings("unchecked") + final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaTypeDescriptor; + final ValueBinder elementBinder = getElementJdbcType().getBinder( pluralJavaType.getElementJavaType() ); return new BasicBinder<>( javaTypeDescriptor, this ) { private String typeName(WrapperOptions options) { - return ( upperTypeName == null - ? getTypeName( options, (BasicPluralJavaType) getJavaType(), (ArrayJdbcType) getJdbcType() ).toUpperCase( Locale.ROOT ) - : upperTypeName - ); + final BasicPluralJavaType javaType = (BasicPluralJavaType) getJavaType(); + final ArrayJdbcType jdbcType = (ArrayJdbcType) getJdbcType(); + return upperTypeName == null + ? getTypeName( options, javaType, jdbcType ).toUpperCase( Locale.ROOT ) + : upperTypeName; } @Override protected void doBindNull(PreparedStatement st, int index, WrapperOptions options) throws SQLException { @@ -107,12 +111,12 @@ public class OracleArrayJdbcType extends ArrayJdbcType implements SqlTypedJdbcTy @Override public java.sql.Array getBindValue(X value, WrapperOptions options) throws SQLException { - final Object[] objects = ( (OracleArrayJdbcType) getJdbcType() ).getArray( this, elementBinder, value, options ); + final OracleArrayJdbcType oracleArrayJdbcType = (OracleArrayJdbcType) getJdbcType(); + final Object[] objects = oracleArrayJdbcType.getArray( this, elementBinder, value, options ); final String arrayTypeName = typeName( options ); - - final OracleConnection oracleConnection = options.getSession() - .getJdbcCoordinator().getLogicalConnection().getPhysicalConnection() - .unwrap( OracleConnection.class ); + final OracleConnection oracleConnection = + options.getSession().getJdbcCoordinator().getLogicalConnection().getPhysicalConnection() + .unwrap( OracleConnection.class ); try { return oracleConnection.createOracleArray( arrayTypeName, objects ); } @@ -145,74 +149,81 @@ public class OracleArrayJdbcType extends ArrayJdbcType implements SqlTypedJdbcTy } static String getTypeName(WrapperOptions options, BasicPluralJavaType containerJavaType, ArrayJdbcType arrayJdbcType) { - Dialect dialect = options.getSessionFactory().getJdbcServices().getDialect(); + final Dialect dialect = options.getSessionFactory().getJdbcServices().getDialect(); return getTypeName( containerJavaType.getElementJavaType(), arrayJdbcType.getElementJdbcType(), dialect ); } static String getTypeName(BasicType elementType, Dialect dialect) { final BasicValueConverter converter = elementType.getValueConverter(); if ( converter != null ) { - final String simpleName; - if ( converter instanceof JpaAttributeConverter ) { - simpleName = ( (JpaAttributeConverter) converter ).getConverterJavaType() - .getJavaTypeClass() - .getSimpleName(); - } - else { - simpleName = converter.getClass().getSimpleName(); - } return dialect.getArrayTypeName( - simpleName, + converterClassName( converter ), null, // not needed by OracleDialect.getArrayTypeName() null // not needed by OracleDialect.getArrayTypeName() ); } - return getTypeName( elementType.getJavaTypeDescriptor(), elementType.getJdbcType(), dialect ); + else { + return getTypeName( elementType.getJavaTypeDescriptor(), elementType.getJdbcType(), dialect ); + } + } + + private static String converterClassName(BasicValueConverter converter) { + if ( converter instanceof JpaAttributeConverter ) { + final JpaAttributeConverter jpaConverter = (JpaAttributeConverter) converter; + return jpaConverter.getConverterJavaType().getJavaTypeClass().getSimpleName(); + } + else { + return converter.getClass().getSimpleName(); + } } static String getTypeName(JavaType elementJavaType, JdbcType elementJdbcType, Dialect dialect) { - final String simpleName; - if ( elementJavaType.getJavaTypeClass().isArray() ) { - simpleName = dialect.getArrayTypeName( - elementJavaType.getJavaTypeClass().getComponentType().getSimpleName(), + return dialect.getArrayTypeName( + arrayClassName( elementJavaType, elementJdbcType, dialect ), + null, // not needed by OracleDialect.getArrayTypeName() + null // not needed by OracleDialect.getArrayTypeName() + ); + } + + private static String arrayClassName(JavaType elementJavaType, JdbcType elementJdbcType, Dialect dialect) { + final Class javaClass = elementJavaType.getJavaTypeClass(); + if ( javaClass.isArray() ) { + return dialect.getArrayTypeName( + javaClass.getComponentType().getSimpleName(), null, // not needed by OracleDialect.getArrayTypeName() null // not needed by OracleDialect.getArrayTypeName() ); } else if ( elementJdbcType instanceof StructJdbcType ) { - simpleName = ( (StructJdbcType) elementJdbcType ).getStructTypeName(); + return ( (StructJdbcType) elementJdbcType).getStructTypeName(); } else { final Class preferredJavaTypeClass = elementJdbcType.getPreferredJavaTypeClass( null ); - if ( preferredJavaTypeClass == elementJavaType.getJavaTypeClass() ) { - simpleName = elementJavaType.getJavaTypeClass().getSimpleName(); + if ( preferredJavaTypeClass == javaClass) { + return javaClass.getSimpleName(); } else { if ( preferredJavaTypeClass.isArray() ) { - simpleName = elementJavaType.getJavaTypeClass().getSimpleName() + dialect.getArrayTypeName( + return javaClass.getSimpleName() + dialect.getArrayTypeName( preferredJavaTypeClass.getComponentType().getSimpleName(), null, null ); } else { - simpleName = elementJavaType.getJavaTypeClass().getSimpleName() + preferredJavaTypeClass.getSimpleName(); + return javaClass.getSimpleName() + preferredJavaTypeClass.getSimpleName(); } } } - return dialect.getArrayTypeName( - simpleName, - null, // not needed by OracleDialect.getArrayTypeName() - null // not needed by OracleDialect.getArrayTypeName() - ); } @Override public void addAuxiliaryDatabaseObjects( JavaType javaType, + BasicValueConverter valueConverter, Size columnSize, Database database, - TypeConfiguration typeConfiguration) { + JdbcTypeIndicators context) { final JdbcType elementJdbcType = getElementJdbcType(); if ( elementJdbcType instanceof StructJdbcType ) { // OracleAggregateSupport will take care of contributing the auxiliary database object @@ -221,28 +232,41 @@ public class OracleArrayJdbcType extends ArrayJdbcType implements SqlTypedJdbcTy final Dialect dialect = database.getDialect(); final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaType; final JavaType elementJavaType = pluralJavaType.getElementJavaType(); - final String arrayTypeName = typeName == null ? getTypeName( elementJavaType, elementJdbcType, dialect ) : typeName; - final String elementType = typeConfiguration.getDdlTypeRegistry().getTypeName( - elementJdbcType.getDdlTypeCode(), - dialect.getSizeStrategy().resolveSize( - elementJdbcType, - elementJavaType, - columnSize.getPrecision(), - columnSize.getScale(), - columnSize.getLength() - ), - new BasicTypeImpl<>( elementJavaType, elementJdbcType ) - ); - final UserDefinedArrayType userDefinedArrayType = database.getDefaultNamespace().createUserDefinedArrayType( - Identifier.toIdentifier( arrayTypeName ), - name -> new UserDefinedArrayType( "orm", database.getDefaultNamespace(), name ) - ); + final String elementTypeName = + elementType( elementJavaType, elementJdbcType, columnSize, context.getTypeConfiguration(), dialect ); + final String arrayTypeName = arrayTypeName( elementJavaType, elementJdbcType, dialect ); + createUserDefinedArrayType( arrayTypeName, elementTypeName, columnSize, elementJdbcType, database ); + } + + private String arrayTypeName(JavaType elementJavaType, JdbcType elementJdbcType, Dialect dialect) { + return typeName == null + ? getTypeName( elementJavaType, elementJdbcType, dialect ) + : typeName; + } + + private void createUserDefinedArrayType( + String arrayTypeName, String elementTypeName, Size columnSize, JdbcType elementJdbcType, Database database) { + final Namespace defaultNamespace = database.getDefaultNamespace(); + final UserDefinedArrayType userDefinedArrayType = + defaultNamespace.createUserDefinedArrayType( + toIdentifier( arrayTypeName ), + name -> new UserDefinedArrayType( "orm", defaultNamespace, name ) + ); userDefinedArrayType.setArraySqlTypeCode( getDdlTypeCode() ); - userDefinedArrayType.setElementTypeName( elementType ); + userDefinedArrayType.setElementTypeName( elementTypeName ); userDefinedArrayType.setElementSqlTypeCode( elementJdbcType.getDefaultSqlTypeCode() ); userDefinedArrayType.setArrayLength( columnSize.getArrayLength() == null ? 127 : columnSize.getArrayLength() ); } + private static String elementType( + JavaType elementJavaType, JdbcType elementJdbcType, Size columnSize, + TypeConfiguration typeConfiguration, Dialect dialect) { + return typeConfiguration.getDdlTypeRegistry() + .getTypeName( elementJdbcType.getDdlTypeCode(), + dialect.getSizeStrategy().resolveSize( elementJdbcType, elementJavaType, columnSize ), + new BasicTypeImpl<>(elementJavaType, elementJdbcType) ); + } + @Override public void registerOutParameter(CallableStatement callableStatement, String name) throws SQLException { callableStatement.registerOutParameter( name, ARRAY, upperTypeName ); @@ -255,12 +279,9 @@ public class OracleArrayJdbcType extends ArrayJdbcType implements SqlTypedJdbcTy @Override public String getExtraCreateTableInfo(JavaType javaType, String columnName, String tableName, Database database) { - return getElementJdbcType().getExtraCreateTableInfo( - ( (BasicPluralJavaType) javaType ).getElementJavaType(), - columnName, - tableName, - database - ); + final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaType; + return getElementJdbcType() + .getExtraCreateTableInfo( pluralJavaType.getElementJavaType(), columnName, tableName, database ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java index 9dc3f79952..3ab6edfea8 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java @@ -1683,6 +1683,27 @@ public class OracleDialect extends Dialect { return new String[] { domain.toString() }; } + /** + * Used to generate the {@code CREATE} DDL command for + * Data Use Case Domain based on {@code VARCHAR2} values. + * + * @return the DDL command to create that enum + */ + public static String[] getCreateVarcharEnumTypeCommand(String name, String[] values) { + final StringBuilder domain = new StringBuilder(); + domain.append( "create domain " ) + .append( name ) + .append( " as enum (" ); + String separator = ""; + for ( String value : values ) { + domain.append( separator ).append( value ).append("='").append(value).append("'"); + separator = ", "; + } + domain.append( ')' ); + return new String[] { domain.toString() }; + + } + @Override public String[] getDropEnumTypeCommand(String name) { return new String[] { "drop domain if exists " + name + " force" }; diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java index 1592994407..276002bf6b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java @@ -19,7 +19,6 @@ import org.hibernate.type.descriptor.jdbc.BasicExtractor; import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import org.hibernate.type.spi.TypeConfiguration; import java.sql.CallableStatement; import java.sql.PreparedStatement; @@ -64,7 +63,11 @@ public class OracleEnumJdbcType implements JdbcType { @Override public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { - return (appender, value, dialect, wrapperOptions) -> appender.appendSql( dialect.getEnumTypeDeclaration( (Class>) javaType.getJavaType() )+"." + ((Enum) value).name() ); + @SuppressWarnings("unchecked") + final Class> enumClass = (Class>) javaType.getJavaType(); + return (appender, value, dialect, wrapperOptions) + -> appender.appendSql( dialect.getEnumTypeDeclaration( enumClass ) + + "." + ((Enum) value).name() ); } @Override @@ -131,44 +134,24 @@ public class OracleEnumJdbcType implements JdbcType { Size columnSize, Database database, JdbcTypeIndicators context) { - addAuxiliaryDatabaseObjects( javaType, valueConverter, database, true ); - } - - @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - Size columnSize, - Database database, - TypeConfiguration typeConfiguration) { - addAuxiliaryDatabaseObjects( javaType, null, database, true ); - } - - private void addAuxiliaryDatabaseObjects( - JavaType javaType, - BasicValueConverter valueConverter, - Database database, - boolean sortEnumValues) { @SuppressWarnings("unchecked") final Class> enumClass = (Class>) javaType.getJavaType(); - final String enumTypeName = enumClass.getSimpleName(); @SuppressWarnings("unchecked") final String[] enumeratedValues = valueConverter == null ? getEnumeratedValues( enumClass ) : getEnumeratedValues( enumClass, (BasicValueConverter,?>) valueConverter ) ; - if ( sortEnumValues ) { + if ( getDefaultSqlTypeCode() == NAMED_ENUM ) { Arrays.sort( enumeratedValues ); } final Dialect dialect = database.getDialect(); - final String[] create = getCreateEnumTypeCommand( - javaType.getJavaTypeClass().getSimpleName(), - enumeratedValues - ); + final String[] create = + getCreateEnumTypeCommand( javaType.getJavaTypeClass().getSimpleName(), enumeratedValues, dialect ); final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); if ( create != null && create.length > 0 ) { database.addAuxiliaryDatabaseObject( new NamedAuxiliaryDatabaseObject( - enumTypeName, + enumClass.getSimpleName(), database.getDefaultNamespace(), create, drop, @@ -179,24 +162,7 @@ public class OracleEnumJdbcType implements JdbcType { } } - /** - * Used to generate the CREATE DDL command for Data Use Case Domain based on VARCHAR2 values. - * - * @param name - * @param values - * @return the DDL command to create that enum - */ - public String[] getCreateEnumTypeCommand(String name, String[] values) { - final StringBuilder domain = new StringBuilder(); - domain.append( "create domain " ) - .append( name ) - .append( " as enum (" ); - String separator = ""; - for ( String value : values ) { - domain.append( separator ).append( value ).append("='").append(value).append("'"); - separator = ", "; - } - domain.append( ')' ); - return new String[] { domain.toString() }; + String[] getCreateEnumTypeCommand(String name, String[] values, Dialect dialect) { + return OracleDialect.getCreateVarcharEnumTypeCommand( name, values ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleOrdinalEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleOrdinalEnumJdbcType.java index b552e7c69a..1b036768a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleOrdinalEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleOrdinalEnumJdbcType.java @@ -13,13 +13,11 @@ import org.hibernate.engine.jdbc.Size; import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.WrapperOptions; -import org.hibernate.type.descriptor.converter.internal.EnumHelper; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.jdbc.BasicBinder; import org.hibernate.type.descriptor.jdbc.BasicExtractor; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import org.hibernate.type.spi.TypeConfiguration; import java.sql.CallableStatement; import java.sql.PreparedStatement; @@ -30,6 +28,7 @@ import java.util.Arrays; import static java.util.Collections.emptySet; import static org.hibernate.type.SqlTypes.NAMED_ORDINAL_ENUM; +import static org.hibernate.type.descriptor.converter.internal.EnumHelper.getEnumeratedValues; /** * Represents a named {@code enum} type on Oracle 23ai+. @@ -94,12 +93,9 @@ public class OracleOrdinalEnumJdbcType extends OracleEnumJdbcType { @Override protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { final int value = rs.getInt( paramIndex ); - if(rs.wasNull()) { - return getJavaType().wrap(null, options); - } - else { - return getJavaType().wrap(value - 1, options); - } + return rs.wasNull() + ? getJavaType().wrap(null, options) + : getJavaType().wrap(value - 1, options); } @Override @@ -115,51 +111,8 @@ public class OracleOrdinalEnumJdbcType extends OracleEnumJdbcType { } @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - BasicValueConverter valueConverter, - Size columnSize, - Database database, - JdbcTypeIndicators context) { - addAuxiliaryDatabaseObjects( javaType, database, false ); + String[] getCreateEnumTypeCommand(String name, String[] enumeratedValues, Dialect dialect) { + return dialect.getCreateEnumTypeCommand( name, enumeratedValues ); } - @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - Size columnSize, - Database database, - TypeConfiguration typeConfiguration) { - addAuxiliaryDatabaseObjects( javaType, database, false ); - } - - private void addAuxiliaryDatabaseObjects( - JavaType javaType, - Database database, - boolean sortEnumValues) { - final Dialect dialect = database.getDialect(); - final Class> enumClass = (Class>) javaType.getJavaType(); - final String enumTypeName = enumClass.getSimpleName(); - final String[] enumeratedValues = EnumHelper.getEnumeratedValues( enumClass ); - if ( sortEnumValues ) { - Arrays.sort( enumeratedValues ); - } - final String[] create = dialect.getCreateEnumTypeCommand( - javaType.getJavaTypeClass().getSimpleName(), - enumeratedValues - ); - final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); - if ( create != null && create.length > 0 ) { - database.addAuxiliaryDatabaseObject( - new NamedAuxiliaryDatabaseObject( - enumTypeName, - database.getDefaultNamespace(), - create, - drop, - emptySet(), - true - ) - ); - } - } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLArrayJdbcType.java index f16fe6a041..bb88147949 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLArrayJdbcType.java @@ -33,8 +33,9 @@ public class PostgreSQLArrayJdbcType extends ArrayJdbcType { @Override public ValueBinder getBinder(final JavaType javaTypeDescriptor) { - //noinspection unchecked - final ValueBinder elementBinder = getElementJdbcType().getBinder( ( (BasicPluralJavaType) javaTypeDescriptor ).getElementJavaType() ); + @SuppressWarnings("unchecked") + final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaTypeDescriptor; + final ValueBinder elementBinder = getElementJdbcType().getBinder( pluralJavaType.getElementJavaType() ); return new BasicBinder<>( javaTypeDescriptor, this ) { @Override diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java index b7b84c2d53..7b66e13edf 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java @@ -19,7 +19,6 @@ import org.hibernate.type.descriptor.jdbc.BasicExtractor; import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import org.hibernate.type.spi.TypeConfiguration; import java.sql.CallableStatement; import java.sql.PreparedStatement; @@ -65,9 +64,11 @@ public class PostgreSQLEnumJdbcType implements JdbcType { @Override public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { + @SuppressWarnings("unchecked") + final Class> enumClass = (Class>) javaType.getJavaType(); return (appender, value, dialect, wrapperOptions) -> appender.appendSql( "'" + ((Enum) value).name() + "'::" - + dialect.getEnumTypeDeclaration( (Class>) javaType.getJavaType() ) ); + + dialect.getEnumTypeDeclaration( enumClass ) ); } @Override @@ -134,44 +135,24 @@ public class PostgreSQLEnumJdbcType implements JdbcType { Size columnSize, Database database, JdbcTypeIndicators context) { - addAuxiliaryDatabaseObjects( javaType, valueConverter, database, true ); - } - - @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - Size columnSize, - Database database, - TypeConfiguration typeConfiguration) { - addAuxiliaryDatabaseObjects( javaType, null, database, true ); - } - - protected void addAuxiliaryDatabaseObjects( - JavaType javaType, - BasicValueConverter valueConverter, - Database database, - boolean sortEnumValues) { @SuppressWarnings("unchecked") final Class> enumClass = (Class>) javaType.getJavaType(); - final String enumTypeName = enumClass.getSimpleName(); @SuppressWarnings("unchecked") final String[] enumeratedValues = valueConverter == null ? getEnumeratedValues( enumClass ) : getEnumeratedValues( enumClass, (BasicValueConverter,?>) valueConverter ) ; - if ( sortEnumValues ) { + if ( getDefaultSqlTypeCode() == NAMED_ENUM ) { Arrays.sort( enumeratedValues ); } final Dialect dialect = database.getDialect(); - final String[] create = dialect.getCreateEnumTypeCommand( - javaType.getJavaTypeClass().getSimpleName(), - enumeratedValues - ); + final String[] create = + dialect.getCreateEnumTypeCommand( javaType.getJavaTypeClass().getSimpleName(), enumeratedValues ); final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); if ( create != null && create.length > 0 ) { database.addAuxiliaryDatabaseObject( new NamedAuxiliaryDatabaseObject( - enumTypeName, + enumClass.getSimpleName(), database.getDefaultNamespace(), create, drop, diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLOrdinalEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLOrdinalEnumJdbcType.java index cd190e3d4e..c7348cedf2 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLOrdinalEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLOrdinalEnumJdbcType.java @@ -6,13 +6,6 @@ */ package org.hibernate.dialect; -import org.hibernate.boot.model.relational.Database; -import org.hibernate.engine.jdbc.Size; -import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; -import org.hibernate.type.descriptor.java.JavaType; -import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; -import org.hibernate.type.spi.TypeConfiguration; - import jakarta.persistence.EnumType; import static org.hibernate.type.SqlTypes.NAMED_ORDINAL_ENUM; @@ -39,22 +32,4 @@ public class PostgreSQLOrdinalEnumJdbcType extends PostgreSQLEnumJdbcType { public int getDefaultSqlTypeCode() { return NAMED_ORDINAL_ENUM; } - - @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - BasicValueConverter valueConverter, Size columnSize, - Database database, - JdbcTypeIndicators context) { - addAuxiliaryDatabaseObjects( javaType, valueConverter, database, false ); - } - - @Override - public void addAuxiliaryDatabaseObjects( - JavaType javaType, - Size columnSize, - Database database, - TypeConfiguration typeConfiguration) { - addAuxiliaryDatabaseObjects( javaType, null, database, false ); - } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java index d4c005df98..82375fde86 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java @@ -141,20 +141,13 @@ public class ArrayJdbcType implements JdbcType { } } - protected Object[] getArray( - BasicBinder binder, - ValueBinder elementBinder, - Object value, - WrapperOptions options) throws SQLException { + protected Object[] getArray(BasicBinder binder, ValueBinder elementBinder, T value, WrapperOptions options) + throws SQLException { final JdbcType elementJdbcType = ( (ArrayJdbcType) binder.getJdbcType() ).getElementJdbcType(); //noinspection unchecked - final JavaType javaType = (JavaType) binder.getJavaType(); + final JavaType javaType = (JavaType) binder.getJavaType(); if ( elementJdbcType instanceof AggregateJdbcType ) { - final Object[] domainObjects = javaType.unwrap( - value, - Object[].class, - options - ); + final T[] domainObjects = (T[]) javaType.unwrap( value, Object[].class, options ); final Object[] objects = new Object[domainObjects.length]; for ( int i = 0; i < domainObjects.length; i++ ) { objects[i] = elementBinder.getBindValue( domainObjects[i], options ); @@ -163,22 +156,15 @@ public class ArrayJdbcType implements JdbcType { } else { final TypeConfiguration typeConfiguration = options.getSessionFactory().getTypeConfiguration(); - final JdbcType underlyingJdbcType = typeConfiguration.getJdbcTypeRegistry() - .getDescriptor( elementJdbcType.getDefaultSqlTypeCode() ); + final JdbcType underlyingJdbcType = + typeConfiguration.getJdbcTypeRegistry().getDescriptor( elementJdbcType.getDefaultSqlTypeCode() ); final Class preferredJavaTypeClass = elementJdbcType.getPreferredJavaTypeClass( options ); - final Class elementJdbcJavaTypeClass; - if ( preferredJavaTypeClass == null ) { - elementJdbcJavaTypeClass = underlyingJdbcType.getJdbcRecommendedJavaTypeMapping( - null, - null, - typeConfiguration - ).getJavaTypeClass(); - } - else { - elementJdbcJavaTypeClass = preferredJavaTypeClass; - } - //noinspection unchecked - final Class arrayClass = (Class) + final Class elementJdbcJavaTypeClass = + preferredJavaTypeClass == null + ? underlyingJdbcType.getJdbcRecommendedJavaTypeMapping(null, null, typeConfiguration ) + .getJavaTypeClass() + : preferredJavaTypeClass; + final Class arrayClass = (Class) Array.newInstance( elementJdbcJavaTypeClass, 0 ).getClass(); return javaType.unwrap( value, arrayClass, options ); } @@ -192,11 +178,8 @@ public class ArrayJdbcType implements JdbcType { final Object[] domainObjects = new Object[Array.getLength( rawArray )]; for ( int i = 0; i < domainObjects.length; i++ ) { final Object[] aggregateRawValues = aggregateJdbcType.extractJdbcValues( Array.get( rawArray, i ), options ); - final StructAttributeValues attributeValues = StructHelper.getAttributeValues( - embeddableMappingType, - aggregateRawValues, - options - ); + final StructAttributeValues attributeValues = + StructHelper.getAttributeValues( embeddableMappingType, aggregateRawValues, options ); domainObjects[i] = instantiate( embeddableMappingType, attributeValues, options.getSessionFactory() ); } return extractor.getJavaType().wrap( domainObjects, options ); @@ -208,8 +191,9 @@ public class ArrayJdbcType implements JdbcType { @Override public ValueBinder getBinder(final JavaType javaTypeDescriptor) { - //noinspection unchecked - final ValueBinder elementBinder = elementJdbcType.getBinder( ( (BasicPluralJavaType) javaTypeDescriptor ).getElementJavaType() ); + @SuppressWarnings("unchecked") + final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaTypeDescriptor; + final ValueBinder elementBinder = elementJdbcType.getBinder( pluralJavaType.getElementJavaType() ); return new BasicBinder<>( javaTypeDescriptor, this ) { @Override @@ -284,9 +268,9 @@ public class ArrayJdbcType implements JdbcType { */ @Override public boolean equals(Object o) { - return o != null && - getClass() == o.getClass() && - getElementJdbcType().equals( ( (ArrayJdbcType) o ).getElementJdbcType() ); + return o != null + && getClass() == o.getClass() + && getElementJdbcType().equals( ( (ArrayJdbcType) o ).getElementJdbcType() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java index 3f7b57272f..4123d1ea3c 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java @@ -372,18 +372,6 @@ public interface JdbcType extends Serializable { callableStatement.registerOutParameter( index, getJdbcTypeCode() ); } - /** - * @deprecated Use {@link #addAuxiliaryDatabaseObjects(JavaType, BasicValueConverter, Size, Database, JdbcTypeIndicators)} instead - */ - @Incubating - @Deprecated(forRemoval = true) - default void addAuxiliaryDatabaseObjects( - JavaType javaType, - Size columnSize, - Database database, - TypeConfiguration typeConfiguration) { - } - /** * Add auxiliary database objects for this {@linkplain JdbcType} to the {@link Database} object. * @@ -396,7 +384,6 @@ public interface JdbcType extends Serializable { Size columnSize, Database database, JdbcTypeIndicators context) { - addAuxiliaryDatabaseObjects( javaType, columnSize, database, context.getTypeConfiguration() ); } @Incubating diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/ArrayDdlTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/ArrayDdlTypeImpl.java index e2d5b21e20..69844484b8 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/ArrayDdlTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/ArrayDdlTypeImpl.java @@ -46,9 +46,7 @@ public class ArrayDdlTypeImpl extends DdlTypeImpl { dialect.getSizeStrategy().resolveSize( elementType.getJdbcMapping().getJdbcType(), elementType.getJavaTypeDescriptor(), - columnSize.getPrecision(), - columnSize.getScale(), - columnSize.getLength() + columnSize ), elementType, ddlTypeRegistry @@ -79,9 +77,7 @@ public class ArrayDdlTypeImpl extends DdlTypeImpl { dialect.getSizeStrategy().resolveSize( elementType.getJdbcMapping().getJdbcType(), elementType.getJavaTypeDescriptor(), - columnSize.getPrecision(), - columnSize.getScale(), - columnSize.getLength() + columnSize ), elementType ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/DdlTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/DdlTypeImpl.java index e90b444e7d..e588a54f99 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/DdlTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/internal/DdlTypeImpl.java @@ -114,8 +114,7 @@ public class DdlTypeImpl implements DdlType { } else { //use the given length/precision/scale - final Size size = dialect.getSizeStrategy() - .resolveSize( jdbcType, javaType, precision, scale, length ); + final Size size = dialect.getSizeStrategy().resolveSize( jdbcType, javaType, precision, scale, length ); if ( size.getPrecision() != null && size.getScale() == null ) { //needed for cast(x as BigInteger(p)) size.setScale( javaType.getDefaultSqlScale( dialect, jdbcType ) );