Skip to content

clean up typing issues around ProcedureCall and its implementation #9968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,23 @@ public static void cleanTemporaryTableRows(
TemporaryTableExporter exporter,
Function<SharedSessionContractImplementor,String> sessionUidAccess,
SharedSessionContractImplementor session) {
PreparedStatement ps = null;
PreparedStatement preparedStatement = null;
try {
final String sql = exporter.getSqlTruncateCommand( temporaryTable, sessionUidAccess, session );

ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql, false );

preparedStatement = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
if ( temporaryTable.getSessionUidColumn() != null ) {
final String sessionUid = sessionUidAccess.apply( session );
ps.setString( 1, sessionUid );
preparedStatement.setString( 1, sessionUid );
}

session.getJdbcCoordinator().getResultSetReturn().executeUpdate( ps, sql );
session.getJdbcCoordinator().getResultSetReturn().executeUpdate( preparedStatement, sql );
}
catch( Throwable t ) {
log.unableToCleanupTemporaryIdTable(t);
}
finally {
if ( ps != null ) {
if ( preparedStatement != null ) {
try {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( ps );
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( preparedStatement );
}
catch( Throwable ignore ) {
// ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.hibernate.engine.jdbc.internal;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -76,7 +77,7 @@ public Statement createStatement() {

@Override
public PreparedStatement prepareStatement(String sql) {
return buildPreparedStatementPreparationTemplate( sql, false ).prepareStatement();
return prepareStatement( sql, false );
}

@Override
Expand All @@ -85,6 +86,12 @@ public PreparedStatement prepareStatement(String sql, final boolean isCallable)
return buildPreparedStatementPreparationTemplate( sql, isCallable ).prepareStatement();
}

@Override
public CallableStatement prepareCallableStatement(String sql) {
jdbcCoordinator.executeBatch();
return (CallableStatement) prepareStatement( sql, true );
}

private StatementPreparationTemplate buildPreparedStatementPreparationTemplate(String sql, final boolean isCallable) {
return new StatementPreparationTemplate( sql ) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public MutationExecutorStandard(
int batchSize,
SharedSessionContractImplementor session) {
this.mutationOperationGroup = mutationOperationGroup;
this.generatedValuesDelegate = mutationOperationGroup.asEntityMutationOperationGroup() != null ?
mutationOperationGroup.asEntityMutationOperationGroup().getMutationDelegate() :
null;
this.generatedValuesDelegate = mutationOperationGroup.asEntityMutationOperationGroup() != null
? mutationOperationGroup.asEntityMutationOperationGroup().getMutationDelegate()
: null;

final BatchKey batchKey = batchKeySupplier.getBatchKey();

Expand All @@ -88,11 +88,11 @@ public MutationExecutorStandard(

for ( int i = mutationOperationGroup.getNumberOfOperations() - 1; i >= 0; i-- ) {
final MutationOperation operation = mutationOperationGroup.getOperation( i );
if ( operation instanceof SelfExecutingUpdateOperation ) {
if ( operation instanceof SelfExecutingUpdateOperation selfExecutingUpdateOperation ) {
if ( selfExecutingMutations == null ) {
selfExecutingMutations = new ArrayList<>();
}
selfExecutingMutations.add( 0, ( (SelfExecutingUpdateOperation) operation ) );
selfExecutingMutations.add( 0, selfExecutingUpdateOperation );
}
else {
final PreparableMutationOperation preparableMutationOperation = (PreparableMutationOperation) operation;
Expand Down Expand Up @@ -245,9 +245,11 @@ protected GeneratedValues performNonBatchedOperations(
session
);

final Object id = entityGroup.getMutationType() == MutationType.INSERT && details.getMutatingTableDetails().isIdentifierTable() ?
generatedValues.getGeneratedValue( entityTarget.getTargetPart().getIdentifierMapping() ) :
null;
final Object id =
entityGroup.getMutationType() == MutationType.INSERT
&& details.getMutatingTableDetails().isIdentifierTable()
? generatedValues.getGeneratedValue( entityTarget.getTargetPart().getIdentifierMapping() )
: null;
nonBatchedStatementGroup.forEachStatement( (tableName, statementDetails) -> {
if ( !statementDetails.getMutatingTableDetails().isIdentifierTable() ) {
performNonBatchedMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.hibernate.engine.jdbc.spi;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.Statement;

Expand Down Expand Up @@ -37,6 +38,17 @@ public interface StatementPreparer {
*/
PreparedStatement prepareStatement(String sql);

/**
* Prepare a statement.
*
* @param sql The SQL the statement to be prepared
*
* @return the prepared statement
*
* @since 7.0
*/
CallableStatement prepareCallableStatement(String sql);

/**
* Prepare a statement.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.annotations.SourceType;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.StatementPreparer;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.EventType;
import org.hibernate.generator.EventTypeSets;
Expand Down Expand Up @@ -79,23 +80,21 @@ public EnumSet<EventType> getEventTypes() {

@Override
public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
if ( valueGenerator == null ) {
return propertyType.wrap( getCurrentTimestamp( session ), session );
}
else {
return valueGenerator.generate();
}
return valueGenerator == null
? propertyType.wrap( getCurrentTimestamp( session ), session )
: valueGenerator.generate();
}

private Timestamp getCurrentTimestamp(SharedSessionContractImplementor session) {
Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
boolean callable = dialect.isCurrentTimestampSelectStringCallable();
String timestampSelectString = dialect.getCurrentTimestampSelectString();
final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
final boolean callable = dialect.isCurrentTimestampSelectStringCallable();
final String timestampSelectString = dialect.getCurrentTimestampSelectString();
final JdbcCoordinator coordinator = session.getJdbcCoordinator();
final StatementPreparer statementPreparer = coordinator.getStatementPreparer();
PreparedStatement statement = null;
JdbcCoordinator coordinator = session.getJdbcCoordinator();
try {
statement = prepareStatement( coordinator, timestampSelectString, callable );
Timestamp ts = callable
statement = statementPreparer.prepareStatement( timestampSelectString, callable );
final Timestamp ts = callable
? extractCalledResult( statement, coordinator, timestampSelectString )
: extractResult( statement, coordinator, timestampSelectString );
logResult( ts );
Expand All @@ -116,23 +115,16 @@ private Timestamp getCurrentTimestamp(SharedSessionContractImplementor session)
}
}

private static PreparedStatement prepareStatement(
JdbcCoordinator coordinator,
String timestampSelectString,
boolean callable) {
return coordinator.getStatementPreparer().prepareStatement( timestampSelectString, callable );
}

private static Timestamp extractResult(PreparedStatement statement, JdbcCoordinator coordinator, String sql)
throws SQLException {
ResultSet resultSet = coordinator.getResultSetReturn().extract( statement, sql );
final ResultSet resultSet = coordinator.getResultSetReturn().extract( statement, sql );
resultSet.next();
return resultSet.getTimestamp( 1 );
}

private static Timestamp extractCalledResult(PreparedStatement statement, JdbcCoordinator coordinator, String sql)
throws SQLException {
CallableStatement callable = (CallableStatement) statement;
final CallableStatement callable = (CallableStatement) statement;
callable.registerOutParameter( 1, TIMESTAMP );
coordinator.getResultSetReturn().execute( callable, sql );
return callable.getTimestamp( 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ public final GeneratedValues performInsertReturning(String sql, SharedSessionCon

try {
//fetch the generated id in a separate query
PreparedStatement idSelect = statementPreparer.prepareStatement( selectSQL, false );
final PreparedStatement idSelect = statementPreparer.prepareStatement( selectSQL );
try {
bindParameters( binder.getEntity(), idSelect, session );
ResultSet resultSet = jdbcCoordinator.getResultSetReturn().extract( idSelect, selectSQL );
final ResultSet resultSet = jdbcCoordinator.getResultSetReturn().extract( idSelect, selectSQL );
try {
return extractReturningValues( resultSet, session );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public interface ProcedureCall
* @throws ParameterStrategyException If the ProcedureCall is defined using named parameters
* @throws NoSuchParameterException If no parameter with that position exists
*/
ProcedureParameter getParameterRegistration(int position);
ProcedureParameter<?> getParameterRegistration(int position);

/**
* Basic form for registering a named parameter.
Expand Down Expand Up @@ -205,14 +205,14 @@ <T> ProcedureParameter<T> registerParameter(String parameterName, BasicTypeRefer
* @throws ParameterStrategyException If the ProcedureCall is defined using positional parameters
* @throws NoSuchParameterException If no parameter with that name exists
*/
ProcedureParameter getParameterRegistration(String name);
ProcedureParameter<?> getParameterRegistration(String name);

/**
* Retrieve all registered parameters.
*
* @return The (immutable) list of all registered parameters.
*/
List<ProcedureParameter> getRegisteredParameters();
List<ProcedureParameter<?>> getRegisteredParameters();

/**
* Retrieves access to outputs of this procedure call. Can be called multiple times, returning the same
Expand Down Expand Up @@ -243,8 +243,8 @@ default void close() {
@Override
ProcedureCall addSynchronizedEntityName(String entityName) throws MappingException;

@Override @SuppressWarnings("rawtypes")
ProcedureCall addSynchronizedEntityClass(Class entityClass) throws MappingException;
@Override
ProcedureCall addSynchronizedEntityClass(@SuppressWarnings("rawtypes") Class entityClass) throws MappingException;

@Override
NamedCallableQueryMemento toMemento(String name);
Expand Down Expand Up @@ -283,8 +283,8 @@ default void close() {
ProcedureCall setFlushMode(FlushModeType flushMode);

@Override
ProcedureCall registerStoredProcedureParameter(int position, Class type, ParameterMode mode);
ProcedureCall registerStoredProcedureParameter(int position, Class<?> type, ParameterMode mode);

@Override
ProcedureCall registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode);
ProcedureCall registerStoredProcedureParameter(String parameterName, Class<?> type, ParameterMode mode);
}
Loading