Skip to content

Commit

Permalink
HHH-16125 some small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed May 1, 2023
1 parent aebd601 commit 16915ce
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
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.
Expand All @@ -59,7 +61,10 @@ public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaTypeD
}

@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 );
Expand All @@ -73,7 +78,11 @@ public JdbcType resolveType(TypeConfiguration typeConfiguration, Dialect dialect
}

@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() );
}
Expand Down Expand Up @@ -114,10 +123,8 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
}
}

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 ),
Expand Down Expand Up @@ -159,29 +166,45 @@ public void addAuxiliaryDatabaseObjects(
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ public void addAuxiliaryDatabaseObjects(
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
)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private TemporaryTable(
uuidType,
typeConfiguration.getDdlTypeRegistry().getTypeName(
uuidType.getJdbcType().getDdlTypeCode(),
size
size,
uuidType
),
size
);
Expand Down Expand Up @@ -442,7 +443,8 @@ public static TemporaryTable createEntityTable(
null,
null,
null
)
),
integerBasicType
);
}
else if ( dialect.getIdentityColumnSupport().supportsIdentityColumns() ) {
Expand All @@ -454,7 +456,8 @@ else if ( dialect.getIdentityColumnSupport().supportsIdentityColumns() ) {
null,
null,
null
)
),
integerBasicType
) + " " + dialect.getIdentityColumnSupport()
.getIdentityColumnString( integerBasicType.getJdbcType().getDdlTypeCode() );
}
Expand All @@ -468,7 +471,8 @@ else if ( dialect.getIdentityColumnSupport().supportsIdentityColumns() ) {
null,
null,
null
)
),
integerBasicType
);
}
columns.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
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;

/**
Expand Down Expand Up @@ -114,8 +116,7 @@ public <X> ValueBinder<X> getBinder(final JavaType<X> javaTypeDescriptor) {

@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
Expand All @@ -130,9 +131,7 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
}
}

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()
Expand All @@ -150,15 +149,19 @@ private java.sql.Array getArray(
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
Expand All @@ -172,24 +175,19 @@ private java.sql.Array getArray(
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 );
}
};
}
Expand Down Expand Up @@ -223,23 +221,4 @@ public String getFriendlyName() {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -92,7 +93,7 @@ default int getDefaultSqlTypeCode() {
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ default String[] getRawTypeNames() {
* 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) {
Expand All @@ -82,7 +83,8 @@ default String getTypeName(Size size) {
/**
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public String getTypeName(Size columnSize, Type type, DdlTypeRegistry ddlTypeReg
columnSize.getPrecision(),
columnSize.getScale(),
columnSize.getLength()
)
),
elementType
);
return dialect.getArrayTypeName(
javaTypeDescriptor.getElementJavaType().getJavaTypeClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public String getTypeName(int typeCode, Dialect dialect) {
* @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) {
Expand Down Expand Up @@ -238,7 +239,8 @@ public String getTypeName(int typeCode, Size columnSize, Type type) {
* @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) {
Expand Down

0 comments on commit 16915ce

Please sign in to comment.