HHH-16125 some small cleanups

This commit is contained in:
Gavin 2023-05-01 19:02:40 +03:00 committed by Gavin King
parent aebd601845
commit 16915cec63
8 changed files with 90 additions and 72 deletions

View File

@ -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<X> containerJavaType,
WrapperOptions options) throws SQLException {
private java.sql.Array getArray(X value, BasicPluralJavaType<X> containerJavaType, WrapperOptions options)
throws SQLException {
//noinspection unchecked
final Class<Object[]> arrayClass = (Class<Object[]>) 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();

View File

@ -122,12 +122,18 @@ public class PostgreSQLEnumJdbcType implements JdbcType {
TypeConfiguration typeConfiguration) {
final Dialect dialect = database.getDialect();
final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) 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
)
);
}
}

View File

@ -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(

View File

@ -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<Object[]> arrayClass = (Class<Object[]>) Array.newInstance(
elementJdbcJavaTypeClass,
0
).getClass();
final Class<Object[]> arrayClass = (Class<Object[]>)
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<X> 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();
}
}

View File

@ -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.
* <p>
* 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

View File

@ -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);

View File

@ -39,7 +39,8 @@ public class ArrayDdlTypeImpl extends DdlTypeImpl {
columnSize.getPrecision(),
columnSize.getScale(),
columnSize.getLength()
)
),
elementType
);
return dialect.getArrayTypeName(
javaTypeDescriptor.getElementJavaType().getJavaTypeClass().getSimpleName(),

View File

@ -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) {