From 16915cec63acaae37f031fb8378fa2687cecce87 Mon Sep 17 00:00:00 2001 From: Gavin Date: Mon, 1 May 2023 19:02:40 +0300 Subject: [PATCH] HHH-16125 some small cleanups --- .../dialect/OracleArrayJdbcType.java | 55 +++++++++++----- .../dialect/PostgreSQLEnumJdbcType.java | 14 +++-- .../dialect/temptable/TemporaryTable.java | 12 ++-- .../type/descriptor/jdbc/ArrayJdbcType.java | 63 +++++++------------ .../type/descriptor/jdbc/JdbcType.java | 3 +- .../type/descriptor/sql/DdlType.java | 6 +- .../sql/internal/ArrayDdlTypeImpl.java | 3 +- .../descriptor/sql/spi/DdlTypeRegistry.java | 6 +- 8 files changed, 90 insertions(+), 72 deletions(-) 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 c99dfcd941..df5dce62af 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleArrayJdbcType.java @@ -27,12 +27,14 @@ import org.hibernate.type.descriptor.jdbc.ArrayJdbcType; import org.hibernate.type.descriptor.jdbc.BasicBinder; import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.internal.BasicTypeImpl; import org.hibernate.type.spi.TypeConfiguration; import oracle.jdbc.OracleConnection; import static java.sql.Types.ARRAY; import static java.util.Collections.emptySet; +import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_STRING_ARRAY; /** * Descriptor for {@link Types#ARRAY ARRAY} handling. @@ -59,7 +61,10 @@ public class OracleArrayJdbcType extends ArrayJdbcType { } @Override - public JdbcType resolveType(TypeConfiguration typeConfiguration, Dialect dialect, BasicType elementType, ColumnTypeInformation columnTypeInformation) { + public JdbcType resolveType( + TypeConfiguration typeConfiguration, + Dialect dialect, BasicType elementType, + ColumnTypeInformation columnTypeInformation) { String typeName = columnTypeInformation.getTypeName(); if ( typeName == null || typeName.isBlank() ) { typeName = getTypeName( elementType.getJavaTypeDescriptor(), dialect ); @@ -73,7 +78,11 @@ public class OracleArrayJdbcType extends ArrayJdbcType { } @Override - public JdbcType resolveType(TypeConfiguration typeConfiguration, Dialect dialect, JdbcType elementType, ColumnTypeInformation columnTypeInformation) { + public JdbcType resolveType( + TypeConfiguration typeConfiguration, + Dialect dialect, + JdbcType elementType, + ColumnTypeInformation columnTypeInformation) { // a bit wrong! return new OracleArrayJdbcType( elementType, columnTypeInformation.getTypeName() ); } @@ -114,10 +123,8 @@ public class OracleArrayJdbcType extends ArrayJdbcType { } } - private java.sql.Array getArray( - X value, - BasicPluralJavaType containerJavaType, - WrapperOptions options) throws SQLException { + private java.sql.Array getArray(X value, BasicPluralJavaType containerJavaType, WrapperOptions options) + throws SQLException { //noinspection unchecked final Class arrayClass = (Class) Array.newInstance( getElementJdbcType().getPreferredJavaTypeClass( options ), @@ -159,29 +166,45 @@ public class OracleArrayJdbcType extends ArrayJdbcType { TypeConfiguration typeConfiguration) { final Dialect dialect = database.getDialect(); final BasicPluralJavaType pluralJavaType = (BasicPluralJavaType) javaType; - final String elementTypeName = typeName==null - ? getTypeName( pluralJavaType.getElementJavaType(), dialect ) - : typeName; + final JavaType elementJavaType = pluralJavaType.getElementJavaType(); + final String elementTypeName = typeName==null ? getTypeName( elementJavaType, dialect ) : typeName; final String elementType = typeConfiguration.getDdlTypeRegistry().getTypeName( getElementJdbcType().getDdlTypeCode(), dialect.getSizeStrategy().resolveSize( getElementJdbcType(), - pluralJavaType.getElementJavaType(), + elementJavaType, columnSize.getPrecision(), columnSize.getScale(), columnSize.getLength() - ) + ), + new BasicTypeImpl<>( elementJavaType, getElementJdbcType() ) ); - final String[] create = new String[] { "create or replace type " + elementTypeName + " as varying array(255) of " + elementType }; - final String[] drop = new String[] { -// "drop type " + elementTypeName - }; database.addAuxiliaryDatabaseObject( - new NamedAuxiliaryDatabaseObject( elementTypeName, database.getDefaultNamespace(), create, drop, emptySet(), true ) + new NamedAuxiliaryDatabaseObject( + elementTypeName, + database.getDefaultNamespace(), + getCreateArrayTypeCommand( elementTypeName, elementType ), + getDropArrayTypeCommand( elementTypeName ), + emptySet(), + true + ) ); } + String[] getCreateArrayTypeCommand(String elementTypeName, String elementType) { + return new String[]{ + "create or replace type " + elementTypeName + + " as varying array(255) of " + elementType + }; + } + + String[] getDropArrayTypeCommand(String elementTypeName) { + // for some weird reason dropping the type declarations causes problem in the test suite +// return new String[] { "drop type " + elementTypeName }; + return EMPTY_STRING_ARRAY; + } + // @Override // public String getExtraCreateTableInfo(JavaType javaType, String columnName, String tableName, Database database) { // final Dialect dialect = database.getDialect(); 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 2f3eac2420..e579162134 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java @@ -122,12 +122,18 @@ public class PostgreSQLEnumJdbcType implements JdbcType { TypeConfiguration typeConfiguration) { final Dialect dialect = database.getDialect(); final Class> enumClass = (Class>) javaType.getJavaType(); - final String enumTypeName = enumClass.getSimpleName(); final String[] create = dialect.getCreateEnumTypeCommand( enumClass ); - if ( create != null ) { - final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); + final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); + if ( create != null && create.length>0 ) { database.addAuxiliaryDatabaseObject( - new NamedAuxiliaryDatabaseObject( enumTypeName, database.getDefaultNamespace(), create, drop, emptySet(), true ) + new NamedAuxiliaryDatabaseObject( + enumClass.getSimpleName(), + database.getDefaultNamespace(), + create, + drop, + emptySet(), + true + ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTable.java b/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTable.java index cb6f52f143..62389d2d1d 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTable.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTable.java @@ -139,7 +139,8 @@ public class TemporaryTable implements Exportable, Contributable { uuidType, typeConfiguration.getDdlTypeRegistry().getTypeName( uuidType.getJdbcType().getDdlTypeCode(), - size + size, + uuidType ), size ); @@ -442,7 +443,8 @@ public class TemporaryTable implements Exportable, Contributable { null, null, null - ) + ), + integerBasicType ); } else if ( dialect.getIdentityColumnSupport().supportsIdentityColumns() ) { @@ -454,7 +456,8 @@ public class TemporaryTable implements Exportable, Contributable { null, null, null - ) + ), + integerBasicType ) + " " + dialect.getIdentityColumnSupport() .getIdentityColumnString( integerBasicType.getJdbcType().getDdlTypeCode() ); } @@ -468,7 +471,8 @@ public class TemporaryTable implements Exportable, Contributable { null, null, null - ) + ), + integerBasicType ); } columns.add( 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 ca5772d3a9..6adff50613 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 @@ -27,6 +27,8 @@ import org.hibernate.type.descriptor.java.ByteArrayJavaType; import org.hibernate.type.descriptor.java.ByteJavaType; import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterArray; +import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry; +import org.hibernate.type.internal.BasicTypeImpl; import org.hibernate.type.spi.TypeConfiguration; /** @@ -114,8 +116,7 @@ public class ArrayJdbcType implements JdbcType { @Override protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { - final java.sql.Array arr = getArray( value, options ); - st.setArray( index, arr ); + st.setArray( index, getArray( value, options ) ); } @Override @@ -130,9 +131,7 @@ public class ArrayJdbcType implements JdbcType { } } - private java.sql.Array getArray( - X value, - WrapperOptions options) throws SQLException { + private java.sql.Array getArray(X value, WrapperOptions options) throws SQLException { final TypeConfiguration typeConfiguration = options.getSessionFactory().getTypeConfiguration(); final JdbcType elementJdbcType = ( (ArrayJdbcType) getJdbcType() ).getElementJdbcType(); final JdbcType underlyingJdbcType = typeConfiguration.getJdbcTypeRegistry() @@ -150,15 +149,19 @@ public class ArrayJdbcType implements JdbcType { elementJdbcJavaTypeClass = preferredJavaTypeClass; } //noinspection unchecked - final Class arrayClass = (Class) Array.newInstance( - elementJdbcJavaTypeClass, - 0 - ).getClass(); + final Class arrayClass = (Class) + Array.newInstance( elementJdbcJavaTypeClass, 0 ).getClass(); final Object[] objects = getJavaType().unwrap( value, arrayClass, options ); final SharedSessionContractImplementor session = options.getSession(); + final String typeName = getElementTypeName( elementJdbcType, session ); + return session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection() + .createArrayOf( typeName, objects ); + } + + private String getElementTypeName(JdbcType elementJdbcType, SharedSessionContractImplementor session) { // TODO: ideally, we would have the actual size or the actual type/column accessible - // this is something that we would need for supporting composite types anyway + // this is something that we would need for supporting composite types anyway final JavaType elementJavaType; if ( getJavaType() instanceof ByteArrayJavaType ) { // Special handling needed for Byte[], because that would conflict with the VARBINARY mapping @@ -172,24 +175,19 @@ public class ArrayJdbcType implements JdbcType { final Size size = session.getJdbcServices() .getDialect() .getSizeStrategy() - .resolveSize( - elementJdbcType, - elementJavaType, - null, - null, - null - ); - String typeName = session.getTypeConfiguration() - .getDdlTypeRegistry() - .getDescriptor( elementJdbcType.getDdlTypeCode() ) - .getTypeName( size ); + .resolveSize( elementJdbcType, elementJavaType, null, null, null ); + final DdlTypeRegistry ddlTypeRegistry = session.getTypeConfiguration().getDdlTypeRegistry(); + final String typeName = ddlTypeRegistry.getDescriptor( elementJdbcType.getDdlTypeCode() ) + .getTypeName( size, new BasicTypeImpl<>( elementJavaType, elementJdbcType), ddlTypeRegistry ); int cutIndex = typeName.indexOf( '(' ); if ( cutIndex > 0 ) { // getTypeName for this case required length, etc, parameters. // Cut them out and use database defaults. - typeName = typeName.substring( 0, cutIndex ); + return typeName.substring( 0, cutIndex ); + } + else { + return typeName; } - return session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection().createArrayOf( typeName, objects ); } }; } @@ -223,23 +221,4 @@ public class ArrayJdbcType implements JdbcType { public String toString() { return "ArrayTypeDescriptor(" + getFriendlyName() + ")"; } - - @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( !(o instanceof ArrayJdbcType) ) { - return false; - } - - ArrayJdbcType that = (ArrayJdbcType) o; - - return elementJdbcType.equals( that.elementJdbcType ); - } - - @Override - public int hashCode() { - return elementJdbcType.hashCode(); - } } 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 b3fd77e0f8..330746ba48 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 @@ -20,6 +20,7 @@ import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.sql.ast.spi.StringBuilderSqlAppender; import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.type.SqlTypes; +import org.hibernate.type.Type; import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.WrapperOptions; @@ -92,7 +93,7 @@ public interface JdbcType extends Serializable { * A {@linkplain SqlTypes JDBC type code} that identifies the SQL column type to * be used for schema generation. *

- * This value is passed to {@link DdlTypeRegistry#getTypeName(int, Size)} + * This value is passed to {@link DdlTypeRegistry#getTypeName(int, Size, Type)} * to obtain the SQL column type. * * @return a JDBC type code diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/DdlType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/DdlType.java index 01c6752646..611818296f 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/DdlType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/DdlType.java @@ -72,7 +72,8 @@ public interface DdlType extends Serializable { * Return a type with length, precision, and scale specified by the given * {@linkplain Size size object}. * - * @deprecated not appropriate for named enum or array types + * @deprecated not appropriate for named enum or array types, + * use {@link #getTypeName(Size, Type, DdlTypeRegistry)} instead */ @Deprecated(since = "6.3") default String getTypeName(Size size) { @@ -82,7 +83,8 @@ public interface DdlType extends Serializable { /** * Return a type with the given length, precision, and scale. * - * @deprecated not appropriate for named enum or array types + * @deprecated not appropriate for named enum or array types, + * use {@link #getTypeName(Size, Type, DdlTypeRegistry)} instead */ @Deprecated(since = "6.3") String getTypeName(Long size, Integer precision, Integer scale); 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 e76119b805..4a102a51ca 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 @@ -39,7 +39,8 @@ public class ArrayDdlTypeImpl extends DdlTypeImpl { columnSize.getPrecision(), columnSize.getScale(), columnSize.getLength() - ) + ), + elementType ); return dialect.getArrayTypeName( javaTypeDescriptor.getElementJavaType().getJavaTypeClass().getSimpleName(), diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java index f88a482bfd..b5e6d573da 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java @@ -187,7 +187,8 @@ public class DdlTypeRegistry implements Serializable { * @return the associated type name with the smallest capacity that accommodates * the given size, if available, and the default type name otherwise * - * @deprecated not appropriate for named enum or array types + * @deprecated not appropriate for named enum or array types, + * use {@link #getTypeName(int, Size, Type)} instead */ @Deprecated(since = "6.3") public String getTypeName(int typeCode, Size size) { @@ -238,7 +239,8 @@ public class DdlTypeRegistry implements Serializable { * @return the associated type name with the smallest capacity that accommodates * the given size, if available, and the default type name otherwise * - * @deprecated not appropriate for named enum or array types + * @deprecated not appropriate for named enum or array types, + * use {@link #getTypeName(int, Size, Type)} instead */ @Deprecated(since = "6.3") public String getTypeName(int typeCode, Long size, Integer precision, Integer scale) {