HHH-9286 - Add BasicBinder#bind(CallableStatement st, J value, String name, WrapperOptions options) method

This commit is contained in:
Andrea Boriero 2016-01-20 16:40:14 +00:00
parent 49cd637107
commit 774974a905
30 changed files with 403 additions and 2 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.dialect;
import java.io.FilterReader;
import java.io.Reader;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
@ -121,6 +122,27 @@ public abstract class AbstractHANADialect extends Dialect {
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaTypeDescriptor.unwrap(
value,
CharacterStream.class,
options
);
if ( value instanceof ClobImplementer ) {
st.setCharacterStream(
name,
new CloseSuppressingReader( characterStream.asReader() ),
characterStream.getLength()
);
}
else {
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
}
};
}
};
@ -139,14 +161,38 @@ public abstract class AbstractHANADialect extends Dialect {
options );
if ( value instanceof NClobImplementer ) {
st.setCharacterStream( index, new CloseSuppressingReader( characterStream.asReader() ),
characterStream.getLength() );
st.setCharacterStream(
index,
new CloseSuppressingReader( characterStream.asReader() ),
characterStream.getLength()
);
}
else {
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaTypeDescriptor.unwrap(
value,
CharacterStream.class,
options
);
if ( value instanceof NClobImplementer ) {
st.setCharacterStream(
name,
new CloseSuppressingReader( characterStream.asReader() ),
characterStream.getLength()
);
}
else {
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
}
};
}
};

View File

@ -65,6 +65,12 @@ public class PostgresUUIDType extends AbstractSingleColumnStandardBasicType<UUID
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject( index, javaTypeDescriptor.unwrap( value, UUID.class, options ), getSqlType() );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject( name, javaTypeDescriptor.unwrap( value, UUID.class, options ), getSqlType() );
}
};
}

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type.descriptor;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -25,4 +26,16 @@ public interface ValueBinder<X> {
* @throws SQLException Indicates a JDBC error occurred.
*/
public void bind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException;
/**
* Bind a value to a CallableStatement.
*
* @param st The prepared statement to which to bind the value.
* @param value The value to bind.
* @param name The name to bind the value within the prepared statement
* @param options The options.
*
* @throws SQLException Indicates a JDBC error occurred.
*/
public void bind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException;
}

View File

@ -87,6 +87,23 @@ public class AttributeConverterSqlTypeDescriptorAdapter implements SqlTypeDescri
log.debugf( "Converted value on binding : %s -> %s", value, convertedValue );
realBinder.bind( st, convertedValue, index, options );
}
@Override
public void bind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException {
final Object convertedValue;
try {
convertedValue = converter.convertToDatabaseColumn( value );
}
catch (PersistenceException pe) {
throw pe;
}
catch (RuntimeException re) {
throw new PersistenceException( "Error attempting to apply AttributeConverter", re );
}
log.debugf( "Converted value on binding : %s -> %s", value, convertedValue );
realBinder.bind( st, convertedValue, name, options );
}
};
}

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.type.descriptor.sql;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -74,6 +75,36 @@ public abstract class BasicBinder<J> implements ValueBinder<J> {
}
}
@Override
public final void bind(CallableStatement st, J value, String name, WrapperOptions options) throws SQLException {
final boolean traceEnabled = log.isTraceEnabled();
if ( value == null ) {
if ( traceEnabled ) {
log.trace(
String.format(
NULL_BIND_MSG_TEMPLATE,
name,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
)
);
}
st.setNull( name, sqlDescriptor.getSqlType() );
}
else {
if ( traceEnabled ) {
log.trace(
String.format(
BIND_MSG_TEMPLATE,
name,
JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ),
getJavaDescriptor().extractLoggableRepresentation( value )
)
);
}
doBind( st, value, name, options );
}
}
/**
* Perform the binding. Safe to assume that value is not null.
*
@ -86,4 +117,17 @@ public abstract class BasicBinder<J> implements ValueBinder<J> {
*/
protected abstract void doBind(PreparedStatement st, J value, int index, WrapperOptions options)
throws SQLException;
/**
* Perform the binding. Safe to assume that value is not null.
*
* @param st The CallableStatement
* @param value The value to bind (not null).
* @param name The name at which to bind
* @param options The binding options
*
* @throws SQLException Indicates a problem binding to the prepared statement.
*/
protected abstract void doBind(CallableStatement st, J value, String name, WrapperOptions options)
throws SQLException;
}

View File

@ -45,6 +45,13 @@ public class BigIntTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setLong( index, javaTypeDescriptor.unwrap( value, Long.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setLong( name, javaTypeDescriptor.unwrap( value, Long.class, options ) );
}
};
}

View File

@ -47,6 +47,12 @@ public class BitTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBoolean( index, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBoolean( name, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
};
}

View File

@ -85,6 +85,20 @@ public abstract class BlobTypeDescriptor implements SqlTypeDescriptor {
}
descriptor.getBlobBinder( javaTypeDescriptor ).doBind( st, value, index, options );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
BlobTypeDescriptor descriptor = BLOB_BINDING;
if ( byte[].class.isInstance( value ) ) {
// performance shortcut for binding BLOB data in byte[] format
descriptor = PRIMITIVE_ARRAY_BINDING;
}
else if ( options.useStreamForLobBinding() ) {
descriptor = STREAM_BINDING;
}
descriptor.getBlobBinder( javaTypeDescriptor ).doBind( st, value, name, options );
}
};
}
};
@ -98,6 +112,12 @@ public abstract class BlobTypeDescriptor implements SqlTypeDescriptor {
throws SQLException {
st.setBytes( index, javaTypeDescriptor.unwrap( value, byte[].class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBytes( name, javaTypeDescriptor.unwrap( value, byte[].class, options ) );
}
};
}
};
@ -111,6 +131,12 @@ public abstract class BlobTypeDescriptor implements SqlTypeDescriptor {
throws SQLException {
st.setBlob( index, javaTypeDescriptor.unwrap( value, Blob.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBlob( name, javaTypeDescriptor.unwrap( value, Blob.class, options ) );
}
};
}
};
@ -129,6 +155,17 @@ public abstract class BlobTypeDescriptor implements SqlTypeDescriptor {
);
st.setBinaryStream( index, binaryStream.getInputStream(), binaryStream.getLength() );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final BinaryStream binaryStream = javaTypeDescriptor.unwrap(
value,
BinaryStream.class,
options
);
st.setBinaryStream( name, binaryStream.getInputStream(), binaryStream.getLength() );
}
};
}
};

View File

@ -43,6 +43,12 @@ public class BooleanTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBoolean( index, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBoolean( name, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
};
}

View File

@ -80,6 +80,17 @@ public abstract class ClobTypeDescriptor implements SqlTypeDescriptor {
CLOB_BINDING.getClobBinder( javaTypeDescriptor ).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( javaTypeDescriptor ).doBind( st, value, name, options );
}
else {
CLOB_BINDING.getClobBinder( javaTypeDescriptor ).doBind( st, value, name, options );
}
}
};
}
};
@ -93,6 +104,12 @@ public abstract class ClobTypeDescriptor implements SqlTypeDescriptor {
throws SQLException {
st.setClob( index, javaTypeDescriptor.unwrap( value, Clob.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setClob( name, javaTypeDescriptor.unwrap( value, Clob.class, options ) );
}
};
}
};
@ -111,6 +128,17 @@ public abstract class ClobTypeDescriptor implements SqlTypeDescriptor {
);
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaTypeDescriptor.unwrap(
value,
CharacterStream.class,
options
);
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
};
}
};
@ -129,6 +157,17 @@ public abstract class ClobTypeDescriptor implements SqlTypeDescriptor {
);
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaTypeDescriptor.unwrap(
value,
CharacterStream.class,
options
);
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
};
}

View File

@ -53,6 +53,18 @@ public class DateTypeDescriptor implements SqlTypeDescriptor {
st.setDate( index, date );
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final Date date = javaTypeDescriptor.unwrap( value, Date.class, options );
if ( value instanceof Calendar ) {
st.setDate( name, date, (Calendar) value );
}
else {
st.setDate( name, date );
}
}
};
}

View File

@ -46,6 +46,12 @@ public class DecimalTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBigDecimal( index, javaTypeDescriptor.unwrap( value, BigDecimal.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBigDecimal( name, javaTypeDescriptor.unwrap( value, BigDecimal.class, options ) );
}
};
}

View File

@ -45,6 +45,12 @@ public class DoubleTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setDouble( index, javaTypeDescriptor.unwrap( value, Double.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setDouble( name, javaTypeDescriptor.unwrap( value, Double.class, options ) );
}
};
}

View File

@ -45,6 +45,12 @@ public class IntegerTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setInt( index, javaTypeDescriptor.unwrap( value, Integer.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setInt( name, javaTypeDescriptor.unwrap( value, Integer.class, options ) );
}
};
}

View File

@ -80,6 +80,17 @@ public abstract class NClobTypeDescriptor implements SqlTypeDescriptor {
NCLOB_BINDING.getNClobBinder( javaTypeDescriptor ).doBind( st, value, index, options );
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
if ( options.useStreamForLobBinding() ) {
STREAM_BINDING.getNClobBinder( javaTypeDescriptor ).doBind( st, value, name, options );
}
else {
NCLOB_BINDING.getNClobBinder( javaTypeDescriptor ).doBind( st, value, name, options );
}
}
};
}
};
@ -93,6 +104,12 @@ public abstract class NClobTypeDescriptor implements SqlTypeDescriptor {
throws SQLException {
st.setNClob( index, javaTypeDescriptor.unwrap( value, NClob.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setNClob( name, javaTypeDescriptor.unwrap( value, NClob.class, options ) );
}
};
}
};
@ -111,6 +128,17 @@ public abstract class NClobTypeDescriptor implements SqlTypeDescriptor {
);
st.setCharacterStream( index, characterStream.asReader(), characterStream.getLength() );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final CharacterStream characterStream = javaTypeDescriptor.unwrap(
value,
CharacterStream.class,
options
);
st.setCharacterStream( name, characterStream.asReader(), characterStream.getLength() );
}
};
}
};

View File

@ -45,6 +45,12 @@ public class NVarcharTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setNString( index, javaTypeDescriptor.unwrap( value, String.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setNString( name, javaTypeDescriptor.unwrap( value, String.class, options ) );
}
};
}

View File

@ -45,6 +45,12 @@ public class RealTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setFloat( index, javaTypeDescriptor.unwrap( value, Float.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setFloat( name, javaTypeDescriptor.unwrap( value, Float.class, options ) );
}
};
}

View File

@ -45,6 +45,12 @@ public class SmallIntTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setShort( index, javaTypeDescriptor.unwrap( value, Short.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setShort( name, javaTypeDescriptor.unwrap( value, Short.class, options ) );
}
};
}

View File

@ -140,6 +140,12 @@ public class SqlTypeDescriptorRegistry {
throws SQLException {
st.setObject( index, value, jdbcTypeCode );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject( name, value, jdbcTypeCode );
}
};
}

View File

@ -53,6 +53,18 @@ public class TimeTypeDescriptor implements SqlTypeDescriptor {
st.setTime( index, time );
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final Time time = javaTypeDescriptor.unwrap( value, Time.class, options );
if ( value instanceof Calendar ) {
st.setTime( name, time, (Calendar) value );
}
else {
st.setTime( name, time );
}
}
};
}

View File

@ -53,6 +53,18 @@ public class TimestampTypeDescriptor implements SqlTypeDescriptor {
st.setTimestamp( index, timestamp );
}
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final Timestamp timestamp = javaTypeDescriptor.unwrap( value, Timestamp.class, options );
if ( value instanceof Calendar ) {
st.setTimestamp( name, timestamp, (Calendar) value );
}
else {
st.setTimestamp( name, timestamp );
}
}
};
}

View File

@ -48,6 +48,12 @@ public class TinyIntTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setByte( index, javaTypeDescriptor.unwrap( value, Byte.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setByte( name, javaTypeDescriptor.unwrap( value, Byte.class, options ) );
}
};
}

View File

@ -39,10 +39,17 @@ public class VarbinaryTypeDescriptor implements SqlTypeDescriptor {
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<X>( javaTypeDescriptor, this ) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBytes( index, javaTypeDescriptor.unwrap( value, byte[].class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBytes( name, javaTypeDescriptor.unwrap( value, byte[].class, options ) );
}
};
}

View File

@ -45,6 +45,12 @@ public class VarcharTypeDescriptor implements SqlTypeDescriptor {
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setString( index, javaTypeDescriptor.unwrap( value, String.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setString( name, javaTypeDescriptor.unwrap( value, String.class, options ) );
}
};
}

View File

@ -44,6 +44,13 @@ public class StoredPrefixedStringType
String stringValue = javaTypeDescriptor.unwrap( value, String.class, options );
st.setString( index, PREFIX + stringValue );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
String stringValue = javaTypeDescriptor.unwrap( value, String.class, options );
st.setString( name, PREFIX + stringValue );
}
};
}

View File

@ -54,6 +54,13 @@ public class GeoDBGeometryTypeDescriptor implements SqlTypeDescriptor {
final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options );
st.setBytes( index, GeoDbWkb.to( geometry ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options );
st.setBytes( name, GeoDbWkb.to( geometry ) );
}
};
}

View File

@ -62,6 +62,16 @@ public class MySQLGeometryTypeDescriptor implements SqlTypeDescriptor {
final byte[] bytes = ( buffer == null ? null : buffer.toByteArray() );
st.setBytes( index, bytes );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.MYSQL_WKB );
final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options );
final ByteBuffer buffer = encoder.encode( geometry, ByteOrder.NDR );
final byte[] bytes = ( buffer == null ? null : buffer.toByteArray() );
st.setBytes( name, bytes );
}
};
}

View File

@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.oracle;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -54,6 +55,19 @@ class SDOGeometryValueBinder<J> implements ValueBinder<J> {
}
}
@Override
public void bind(
CallableStatement st, J value, String name, WrapperOptions options) throws SQLException {
if ( value == null ) {
st.setNull( name, Types.STRUCT, SQL_TYPE_NAME );
}
else {
final Geometry geometry = javaTypeDescriptor.unwrap( value, Geometry.class, options );
final Object dbGeom = toNative( geometry, st.getConnection() );
st.setObject( name, dbGeom );
}
}
public Object store(SDOGeometry geom, Connection conn) throws SQLException {
return typeFactory.createStruct( geom, conn );
}

View File

@ -67,6 +67,15 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor {
st.setBytes( index, bytes );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 );
final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options );
final byte[] bytes = encoder.encode( geometry, ByteOrder.NDR ).toByteArray();
st.setBytes( name, bytes );
}
};
}

View File

@ -61,6 +61,14 @@ public class SqlServer2008GeometryTypeDescriptor implements SqlTypeDescriptor {
st.setObject( index, bytes );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options );
final byte[] bytes = Encoders.encode( geometry );
st.setObject( name, bytes );
}
};
}