Skip to content

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
@@ -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();
}
}
}
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 );
}
}
}
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);
}
Loading