-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19602 - Adjust JdbcOperation to allow more-than-one statement #10752
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
Draft
sebersole
wants to merge
3
commits into
hibernate:main
Choose a base branch
from
sebersole:JdbcOperation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
hibernate-core/src/main/java/org/hibernate/sql/exec/internal/AbstractDatabaseOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.sql.exec.internal; | ||
|
||
import org.hibernate.internal.util.collections.CollectionHelper; | ||
import org.hibernate.sql.exec.spi.ExecutionContext; | ||
import org.hibernate.sql.exec.spi.DatabaseOperation; | ||
import org.hibernate.sql.exec.spi.PostAction; | ||
import org.hibernate.sql.exec.spi.PreAction; | ||
import org.hibernate.sql.exec.spi.SecondaryAction; | ||
import org.hibernate.sql.exec.spi.StatementAccess; | ||
|
||
import java.sql.Connection; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@SuppressWarnings("ALL") | ||
public abstract class AbstractDatabaseOperation implements DatabaseOperation { | ||
protected final PreAction[] preActions; | ||
protected final PostAction[] postActions; | ||
|
||
public AbstractDatabaseOperation() { | ||
this( null, null ); | ||
} | ||
|
||
public AbstractDatabaseOperation(PreAction[] preActions, PostAction[] postActions) { | ||
this.preActions = preActions; | ||
this.postActions = postActions; | ||
} | ||
|
||
protected void performPreActions( | ||
StatementAccess statementAccess, | ||
Connection jdbcConnection, | ||
ExecutionContext executionContext) { | ||
if ( preActions != null ) { | ||
for ( int i = 0; i < preActions.length; i++ ) { | ||
preActions[i].performPreAction( statementAccess, jdbcConnection, executionContext ); | ||
} | ||
} | ||
} | ||
|
||
protected void performPostActions( | ||
StatementAccess statementAccess, | ||
Connection jdbcConnection, | ||
ExecutionContext executionContext) { | ||
if ( postActions != null ) { | ||
for ( int i = 0; i < postActions.length; i++ ) { | ||
postActions[i].performPostAction( statementAccess, jdbcConnection, executionContext ); | ||
} | ||
} | ||
} | ||
|
||
protected static PreAction[] toPreActionArray(List<PreAction> actions) { | ||
if ( CollectionHelper.isEmpty( actions ) ) { | ||
return null; | ||
} | ||
return actions.toArray( new PreAction[0] ); | ||
} | ||
|
||
protected static PostAction[] toPostActionArray(List<PostAction> actions) { | ||
if ( CollectionHelper.isEmpty( actions ) ) { | ||
return null; | ||
} | ||
return actions.toArray( new PostAction[0] ); | ||
} | ||
|
||
protected abstract static class Builder<T extends Builder<T>> { | ||
protected List<PreAction> preActions; | ||
protected List<PostAction> postActions; | ||
|
||
protected abstract T getThis(); | ||
|
||
/** | ||
* Appends the {@code actions} to the growing list of pre-actions | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T appendPreAction(PreAction... actions) { | ||
if ( preActions == null ) { | ||
preActions = new ArrayList<>(); | ||
} | ||
Collections.addAll( preActions, actions ); | ||
return getThis(); | ||
} | ||
|
||
/** | ||
* Prepends the {@code actions} to the growing list of pre-actions | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T prependPreAction(PreAction... actions) { | ||
if ( preActions == null ) { | ||
preActions = new ArrayList<>(); | ||
} | ||
for ( int i = actions.length - 1; i >= 0; i-- ) { | ||
preActions.add( 0, actions[i] ); | ||
} | ||
return getThis(); | ||
} | ||
|
||
/** | ||
* Appends the {@code actions} to the growing list of post-actions | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T appendPostAction(PostAction... actions) { | ||
if ( postActions == null ) { | ||
postActions = new ArrayList<>(); | ||
} | ||
Collections.addAll( postActions, actions ); | ||
return getThis(); | ||
} | ||
|
||
/** | ||
* Prepends the {@code actions} to the growing list of post-actions | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T prependPostAction(PostAction... actions) { | ||
if ( postActions == null ) { | ||
postActions = new ArrayList<>(); | ||
} | ||
for ( int i = actions.length - 1; i >= 0; i-- ) { | ||
postActions.add( 0, actions[i] ); | ||
} | ||
return getThis(); | ||
} | ||
|
||
/** | ||
* Adds a secondary action. Assumes the action implements both | ||
* {@linkplain PreAction} and {@linkplain PostAction}. | ||
* | ||
* @see #prependPreAction | ||
* @see #appendPostAction | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T addSecondaryActionPair(SecondaryAction action) { | ||
return addSecondaryActionPair( (PreAction) action, (PostAction) action ); | ||
} | ||
|
||
/** | ||
* Adds a PreAction/PostAction pair. | ||
* | ||
* @see #prependPreAction | ||
* @see #appendPostAction | ||
* | ||
* @return {@code this}, for method chaining. | ||
*/ | ||
public T addSecondaryActionPair(PreAction preAction, PostAction postAction) { | ||
prependPreAction( preAction ); | ||
appendPostAction( postAction ); | ||
return getThis(); | ||
} | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
...rnate-core/src/main/java/org/hibernate/sql/exec/internal/DatabaseOperationSelectImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.sql.exec.internal; | ||
|
||
import org.hibernate.engine.jdbc.spi.JdbcServices; | ||
import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor; | ||
import org.hibernate.sql.exec.spi.DatabaseOperationSelect; | ||
import org.hibernate.sql.exec.spi.ExecutionContext; | ||
import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect; | ||
import org.hibernate.sql.exec.spi.JdbcParameterBindings; | ||
import org.hibernate.sql.exec.spi.JdbcSelectExecutor; | ||
import org.hibernate.sql.exec.spi.PostAction; | ||
import org.hibernate.sql.exec.spi.PreAction; | ||
import org.hibernate.sql.results.spi.ResultsConsumer; | ||
import org.hibernate.sql.results.spi.RowTransformer; | ||
|
||
import java.sql.Connection; | ||
import java.util.Set; | ||
|
||
/** | ||
* Standard DatabaseOperationSelect implementation. | ||
* | ||
* @author Steve Ebersole | ||
*/ | ||
public class DatabaseOperationSelectImpl | ||
extends AbstractDatabaseOperation | ||
implements DatabaseOperationSelect { | ||
private final JdbcOperationQuerySelect primaryOperation; | ||
|
||
public DatabaseOperationSelectImpl(JdbcOperationQuerySelect primaryOperation) { | ||
this( null, null, primaryOperation ); | ||
} | ||
|
||
public DatabaseOperationSelectImpl( | ||
PreAction[] preActions, | ||
PostAction[] postActions, | ||
JdbcOperationQuerySelect primaryOperation) { | ||
super( preActions, postActions ); | ||
this.primaryOperation = primaryOperation; | ||
} | ||
|
||
@Override | ||
public JdbcOperationQuerySelect getPrimaryOperation() { | ||
return primaryOperation; | ||
} | ||
|
||
@Override | ||
public Set<String> getAffectedTableNames() { | ||
return primaryOperation.getAffectedTableNames(); | ||
} | ||
|
||
@Override | ||
public <T, R> T execute( | ||
Class<R> resultType, | ||
int expectedNumberOfRows, | ||
JdbcSelectExecutor.StatementCreator statementCreator, | ||
JdbcParameterBindings jdbcParameterBindings, | ||
RowTransformer<R> rowTransformer, | ||
ResultsConsumer<T, R> resultsConsumer, | ||
ExecutionContext executionContext) { | ||
if ( preActions == null && postActions == null ) { | ||
return performPrimaryOperation( | ||
resultType, | ||
statementCreator, | ||
jdbcParameterBindings, | ||
rowTransformer, | ||
resultsConsumer, | ||
executionContext | ||
); | ||
} | ||
|
||
final SharedSessionContractImplementor session = executionContext.getSession(); | ||
final LogicalConnectionImplementor logicalConnection = session.getJdbcCoordinator().getLogicalConnection(); | ||
final SessionFactoryImplementor sessionFactory = session.getSessionFactory(); | ||
|
||
final Connection connection = logicalConnection.getPhysicalConnection(); | ||
final StatementAccessImpl statementAccess = new StatementAccessImpl( | ||
connection, | ||
logicalConnection, | ||
sessionFactory | ||
); | ||
|
||
try { | ||
try { | ||
performPreActions( statementAccess, connection, executionContext ); | ||
return performPrimaryOperation( | ||
resultType, | ||
statementCreator, | ||
jdbcParameterBindings, | ||
rowTransformer, | ||
resultsConsumer, | ||
executionContext | ||
); | ||
} | ||
finally { | ||
performPostActions( statementAccess, connection, executionContext ); | ||
} | ||
} | ||
finally { | ||
statementAccess.release(); | ||
} | ||
} | ||
|
||
private <T, R> T performPrimaryOperation( | ||
Class<R> resultType, | ||
JdbcSelectExecutor.StatementCreator statementCreator, | ||
JdbcParameterBindings jdbcParameterBindings, | ||
RowTransformer<R> rowTransformer, | ||
ResultsConsumer<T, R> resultsConsumer, | ||
ExecutionContext executionContext) { | ||
final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory(); | ||
final JdbcServices jdbcServices = sessionFactory.getJdbcServices(); | ||
final JdbcSelectExecutor jdbcSelectExecutor = jdbcServices.getJdbcSelectExecutor(); | ||
return jdbcSelectExecutor.executeQuery( | ||
primaryOperation, | ||
jdbcParameterBindings, | ||
executionContext, | ||
rowTransformer, | ||
resultType, | ||
statementCreator, | ||
resultsConsumer | ||
); | ||
} | ||
|
||
public static Builder builder(JdbcOperationQuerySelect primaryAction) { | ||
return new Builder( primaryAction ); | ||
} | ||
|
||
public static class Builder extends AbstractDatabaseOperation.Builder<Builder> { | ||
private final JdbcOperationQuerySelect primaryAction; | ||
|
||
private Builder(JdbcOperationQuerySelect primaryAction) { | ||
this.primaryAction = primaryAction; | ||
} | ||
|
||
@Override | ||
protected Builder getThis() { | ||
return this; | ||
} | ||
|
||
public DatabaseOperationSelectImpl build() { | ||
if ( preActions == null && postActions == null ) { | ||
return new DatabaseOperationSelectImpl( primaryAction ); | ||
} | ||
final PreAction[] preActions = toPreActionArray( this.preActions ); | ||
final PostAction[] postActions = toPostActionArray( this.postActions ); | ||
return new DatabaseOperationSelectImpl( preActions, postActions, primaryAction ); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.sql.exec.internal; | ||
|
||
import org.hibernate.sql.exec.spi.ExecutionContext; | ||
import org.hibernate.sql.exec.spi.DatabaseOperation; | ||
import org.hibernate.sql.exec.spi.StatementAccess; | ||
|
||
import java.sql.Connection; | ||
|
||
/** | ||
* An action to be performed before or after the primary action of a DatabaseOperation. | ||
* | ||
* @see DatabaseOperation#getPrimaryOperation() | ||
* | ||
* @author Steve Ebersole | ||
*/ | ||
public interface JdbcAction { | ||
/** | ||
* Perform the action. | ||
* <p/> | ||
* Generally the action should use the passed {@code jdbcStatement} to interact with the | ||
* database, although the {@code jdbcConnection} can be used to create specialized statements, | ||
* access the {@linkplain java.sql.DatabaseMetaData database metadata}, etc. | ||
* | ||
* @param jdbcStatementAccess Access to a JDBC Statement object which may be used to perform the action. | ||
* @param jdbcConnection The JDBC Connection. | ||
* @param executionContext Access to contextual information useful while executing. | ||
*/ | ||
void perform(StatementAccess jdbcStatementAccess, Connection jdbcConnection, ExecutionContext executionContext); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.