Skip to content

Commit

Permalink
HHH-18020 realign behavior of ClobJdbcType with BlobJdbcType
Browse files Browse the repository at this point in the history
there was some organic divergence here
  • Loading branch information
gavinking committed May 2, 2024
1 parent c955150 commit 72261fd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
// Therefore here we overwrite the sql type descriptors to
// use the non-N variants which are supported.
jdbcTypeRegistry.addDescriptor( Types.NCHAR, CharJdbcType.INSTANCE );
jdbcTypeRegistry.addDescriptor(
Types.NCLOB,
useInputStreamToInsertBlob()
? ClobJdbcType.STREAM_BINDING
: ClobJdbcType.CLOB_BINDING
);
jdbcTypeRegistry.addDescriptor( Types.NCLOB, ClobJdbcType.STREAM_BINDING );
jdbcTypeRegistry.addDescriptor( Types.NVARCHAR, VarcharJdbcType.INSTANCE );
jdbcTypeRegistry.addDescriptor( Types.NUMERIC, DecimalJdbcType.INSTANCE );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
// Therefore here we overwrite the sql type descriptors to
// use the non-N variants which are supported.
jdbcTypeRegistry.addDescriptor( Types.NCHAR, CharJdbcType.INSTANCE );
jdbcTypeRegistry.addDescriptor(
Types.NCLOB,
useInputStreamToInsertBlob()
? ClobJdbcType.STREAM_BINDING
: ClobJdbcType.CLOB_BINDING
);
jdbcTypeRegistry.addDescriptor( Types.NCLOB, ClobJdbcType.STREAM_BINDING );
jdbcTypeRegistry.addDescriptor( Types.NVARCHAR, VarcharJdbcType.INSTANCE );
jdbcTypeRegistry.addDescriptor( Types.NUMERIC, DecimalJdbcType.INSTANCE );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1688,13 +1688,6 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
jdbcTypeRegistry.addDescriptor( NClobJdbcType.DEFAULT );
}

if ( useInputStreamToInsertBlob() ) {
jdbcTypeRegistry.addDescriptor(
Types.CLOB,
ClobJdbcType.STREAM_BINDING
);
}

if ( getTimeZoneSupport() == TimeZoneSupport.NATIVE ) {
jdbcTypeRegistry.addDescriptor( TimestampUtcAsOffsetDateTimeJdbcType.INSTANCE );
jdbcTypeRegistry.addDescriptor( TimeUtcAsOffsetTimeJdbcType.INSTANCE );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,32 @@ public Class<?> getPreferredJavaTypeClass(WrapperOptions options) {
return byte[].class;
}

private BlobJdbcType getDescriptor(Object value, WrapperOptions options) {
if ( value instanceof byte[] ) {
// performance shortcut for binding BLOB data in byte[] format
return PRIMITIVE_ARRAY_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
return STREAM_BINDING;
}
else {
return BLOB_BINDING;
}
}

@Override
public <X> BasicBinder<X> getBlobBinder(final JavaType<X> javaType) {
return new BasicBinder<>( javaType, this ) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
BlobJdbcType descriptor = BLOB_BINDING;
if ( value instanceof byte[] ) {
// performance shortcut for binding BLOB data in byte[] format
descriptor = PRIMITIVE_ARRAY_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
descriptor = STREAM_BINDING;
}
descriptor.getBlobBinder( javaType ).doBind( st, value, index, options );
getDescriptor( value, options ).getBlobBinder( javaType ).doBind( st, value, index, options );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
BlobJdbcType descriptor = BLOB_BINDING;
if ( value instanceof byte[] ) {
// performance shortcut for binding BLOB data in byte[] format
descriptor = PRIMITIVE_ARRAY_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
descriptor = STREAM_BINDING;
}
descriptor.getBlobBinder( javaType ).doBind( st, value, name, options );
getDescriptor( value, options ).getBlobBinder( javaType ).doBind( st, value, name, options );
}
};
}
Expand Down Expand Up @@ -202,22 +199,14 @@ public <X> BasicBinder<X> getBlobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final BinaryStream binaryStream = javaType.unwrap(
value,
BinaryStream.class,
options
);
final BinaryStream binaryStream = javaType.unwrap( value, BinaryStream.class, options );
st.setBinaryStream( index, binaryStream.getInputStream(), binaryStream.getLength() );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final BinaryStream binaryStream = javaType.unwrap(
value,
BinaryStream.class,
options
);
final BinaryStream binaryStream = javaType.unwrap( value, BinaryStream.class, options );
st.setBinaryStream( name, binaryStream.getInputStream(), binaryStream.getLength() );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* Descriptor for {@link Types#CLOB CLOB} handling.
Expand Down Expand Up @@ -47,11 +45,8 @@ public String toString() {
public JdbcType resolveIndicatedType(
JdbcTypeIndicators indicators,
JavaType<?> domainJtd) {
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeRegistry();
return indicators.isNationalized()
? jdbcTypeRegistry.getDescriptor( indicators.resolveJdbcTypeCode( Types.NCLOB ) )
: jdbcTypeRegistry.getDescriptor( indicators.resolveJdbcTypeCode( Types.CLOB ) );
final int jdbcTypeCode = indicators.resolveJdbcTypeCode( indicators.isNationalized() ? Types.NCLOB : Types.CLOB );
return indicators.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( jdbcTypeCode );
}

@Override
Expand Down Expand Up @@ -92,9 +87,20 @@ public String toString() {

@Override
public Class<?> getPreferredJavaTypeClass(WrapperOptions options) {
return options.useStreamForLobBinding() ?
STREAM_BINDING.getPreferredJavaTypeClass( options ) :
CLOB_BINDING.getPreferredJavaTypeClass( options );
return String.class;
}

private ClobJdbcType getDescriptor(Object value, WrapperOptions options) {
if ( value instanceof String ) {
// performance shortcut for binding CLOB data in String format
return STRING_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
return STREAM_BINDING;
}
else {
return CLOB_BINDING;
}
}

@Override
Expand All @@ -103,23 +109,13 @@ public <X> BasicBinder<X> getClobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
if ( options.useStreamForLobBinding() ) {
STREAM_BINDING.getClobBinder( javaType ).doBind( st, value, index, options );
}
else {
CLOB_BINDING.getClobBinder( javaType ).doBind( st, value, index, options );
}
getDescriptor( value, options ).getClobBinder( javaType ).doBind( st, value, index, options );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
if ( options.useStreamForLobBinding() ) {
STREAM_BINDING.getClobBinder( javaType ).doBind( st, value, name, options );
}
else {
CLOB_BINDING.getClobBinder( javaType ).doBind( st, value, name, options );
}
getDescriptor( value, options ).getClobBinder( javaType ).doBind( st, value, name, options );
}
};
}
Expand Down Expand Up @@ -222,11 +218,7 @@ public <X> BasicBinder<X> getClobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaType.unwrap(
value,
CharacterStream.class,
options
);
final CharacterStream characterStream = javaType.unwrap( value, CharacterStream.class, options );
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}

Expand Down Expand Up @@ -261,22 +253,14 @@ public <X> BasicBinder<X> getClobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaType.unwrap(
value,
CharacterStream.class,
options
);
final CharacterStream characterStream = javaType.unwrap( value, CharacterStream.class, options );
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaType.unwrap(
value,
CharacterStream.class,
options
);
final CharacterStream characterStream = javaType.unwrap( value, CharacterStream.class, options );
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,49 @@ public String toString() {

@Override
public Class<?> getPreferredJavaTypeClass(WrapperOptions options) {
return options.useStreamForLobBinding() ?
STREAM_BINDING.getPreferredJavaTypeClass( options ) :
NCLOB_BINDING.getPreferredJavaTypeClass( options );
return String.class;
}

private NClobJdbcType getDescriptor(Object value, WrapperOptions options) {
if ( value instanceof String ) {
// performance shortcut for binding CLOB data in String format
return STRING_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
return STREAM_BINDING;
}
else {
return NCLOB_BINDING;
}
}

@Override
public <X> BasicBinder<X> getNClobBinder(final JavaType<X> javaType) {
return new BasicBinder<>( javaType, this ) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
getDescriptor( value, options ).getNClobBinder( javaType ).doBind( st, value, index, options );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
getDescriptor( value, options ).getNClobBinder( javaType ).doBind( st, value, name, options );
}
};
}
};

public static final NClobJdbcType STRING_BINDING = new NClobJdbcType() {
@Override
public String toString() {
return "NClobTypeDescriptor(STRING_BINDING)";
}

@Override
public Class<?> getPreferredJavaTypeClass(WrapperOptions options) {
return String.class;
}

@Override
Expand All @@ -90,23 +130,34 @@ public <X> BasicBinder<X> getNClobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
if ( options.useStreamForLobBinding() ) {
STREAM_BINDING.getNClobBinder( javaType ).doBind( st, value, index, options );
}
else {
NCLOB_BINDING.getNClobBinder( javaType ).doBind( st, value, index, options );
}
st.setNString( index, javaType.unwrap( value, String.class, options ) );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
if ( options.useStreamForLobBinding() ) {
STREAM_BINDING.getNClobBinder( javaType ).doBind( st, value, name, options );
}
else {
NCLOB_BINDING.getNClobBinder( javaType ).doBind( st, value, name, options );
}
st.setNString( name, javaType.unwrap( value, String.class, options ) );
}
};
}
@Override
public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
return new BasicExtractor<>( javaType, this ) {
@Override
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
return javaType.wrap( rs.getNString( paramIndex ), options );
}

@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options)
throws SQLException {
return javaType.wrap( statement.getNString( index ), options );
}

@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
throws SQLException {
return javaType.wrap( statement.getNString( name ), options );
}
};
}
Expand Down Expand Up @@ -158,22 +209,14 @@ public <X> BasicBinder<X> getNClobBinder(final JavaType<X> javaType) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaType.unwrap(
value,
CharacterStream.class,
options
);
final CharacterStream characterStream = javaType.unwrap( value, CharacterStream.class, options );
st.setNCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaType.unwrap(
value,
CharacterStream.class,
options
);
final CharacterStream characterStream = javaType.unwrap( value, CharacterStream.class, options );
st.setNCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ else if ( "setNClob".equals( methodName ) ) {
throw new IllegalStateException( "PreparedStatement#setNClob unexpectedly called" );
}
}
else if ( "setNString".equals( methodName ) ) {
return null;
}
else {
throw new UnsupportedOperationException( methodName + " is not supported." );

Expand Down

0 comments on commit 72261fd

Please sign in to comment.