Skip to content

Commit

Permalink
--wip-- consolidated MutationExeceutors to standard implementations
Browse files Browse the repository at this point in the history
Still needs:
 - interface redesign
 - test and make sure it's working across all dbs / dialects
  • Loading branch information
mbladel committed Nov 22, 2023
1 parent b816138 commit e7b66f4
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.engine.jdbc.mutation.OperationResultChecker;
import org.hibernate.engine.jdbc.mutation.ParameterUsage;
import org.hibernate.engine.jdbc.mutation.TableInclusionChecker;
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.values.GeneratedValues;
import org.hibernate.persister.entity.mutation.EntityTableMapping;
import org.hibernate.sql.model.TableMapping;
import org.hibernate.sql.model.ValuesAnalysis;

Expand All @@ -40,17 +43,25 @@ public final Object execute(
TableInclusionChecker inclusionChecker,
OperationResultChecker resultChecker,
SharedSessionContractImplementor session) {
performNonBatchedOperations( valuesAnalysis, inclusionChecker, resultChecker, session );
final Object generatedValues = performNonBatchedOperations(
modelReference,
valuesAnalysis,
inclusionChecker,
resultChecker,
session
);
performSelfExecutingOperations( valuesAnalysis, inclusionChecker, session );
performBatchedOperations( valuesAnalysis, inclusionChecker );
return null;
return generatedValues;
}

protected void performNonBatchedOperations(
protected Object performNonBatchedOperations(
Object modelReference,
ValuesAnalysis valuesAnalysis,
TableInclusionChecker inclusionChecker,
OperationResultChecker resultChecker,
SharedSessionContractImplementor session) {
return null;
}

protected void performSelfExecutingOperations(
Expand All @@ -69,6 +80,7 @@ protected void performBatchedOperations(
*/
protected void performNonBatchedMutation(
PreparedStatementDetails statementDetails,
Object id,
JdbcValueBindings valueBindings,
TableInclusionChecker inclusionChecker,
OperationResultChecker resultChecker,
Expand All @@ -88,6 +100,20 @@ protected void performNonBatchedMutation(
return;
}

if ( id != null ) {
assert !tableDetails.isIdentifierTable() : "Unsupported identifier table with generated id";
( (EntityTableMapping) tableDetails ).getKeyMapping().breakDownKeyJdbcValues(
id,
(jdbcValue, columnMapping) -> valueBindings.bindValue(
jdbcValue,
tableDetails.getTableName(),
columnMapping.getColumnName(),
ParameterUsage.SET
),
session
);
}

// If we get here the statement is needed - make sure it is resolved
session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
valueBindings.beforeStatement( statementDetails );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.EventType;
import org.hibernate.generator.values.GeneratedValuesMutationDelegate;
import org.hibernate.jdbc.TooManyRowsAffectedException;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.sql.model.MutationTarget;
Expand Down Expand Up @@ -98,54 +99,40 @@ public static boolean identifiedResultsCheck(
public static PreparedStatementGroup toPreparedStatementGroup(
MutationType mutationType,
MutationTarget<?> mutationTarget,
GeneratedValuesMutationDelegate delegate,
List<PreparableMutationOperation> mutations,
SharedSessionContractImplementor session) {
if ( mutations == null || mutations.isEmpty() ) {
return GROUP_OF_NONE;
}

if ( mutations.size() == 1 ) {
return new PreparedStatementGroupSingleTable( mutations.get( 0 ), session );
return new PreparedStatementGroupSingleTable( mutations.get( 0 ), delegate, session );
}

return new PreparedStatementGroupStandard( mutationType, mutationTarget, mutations, session );
return new PreparedStatementGroupStandard( mutationType, mutationTarget, delegate, mutations, session );
}

public static PreparedStatementDetails standardPreparation(
PreparableMutationOperation jdbcMutation,
GeneratedValuesMutationDelegate delegate,
SharedSessionContractImplementor session) {
return new PreparedStatementDetailsStandard(
jdbcMutation,
() -> standardStatementPreparation( jdbcMutation, session ),
() -> delegate != null ?
delegateStatementPreparation( jdbcMutation, delegate, session ) :
standardStatementPreparation( jdbcMutation, session ),
session.getJdbcServices()
);
}

/**
* @deprecated Use {@link #delegatePreparation} instead
*/
@Deprecated( since = "7.0" )
public static PreparedStatementDetails identityPreparation(
public static PreparedStatement delegateStatementPreparation(
PreparableMutationOperation jdbcMutation,
GeneratedValuesMutationDelegate delegate,
SharedSessionContractImplementor session) {
return delegatePreparation( jdbcMutation, session );
}

public static PreparedStatementDetails delegatePreparation(
PreparableMutationOperation jdbcMutation,
SharedSessionContractImplementor session) {
return new PreparedStatementDetailsStandard(
jdbcMutation,
() -> {
final EntityMutationTarget target = (EntityMutationTarget) jdbcMutation.getMutationTarget();
final PreparedStatement statement = target
.getMutationDelegate( jdbcMutation.getMutationType() )
.prepareStatement( jdbcMutation.getSqlString(), session );
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().register( null, statement );
return statement;
},
session.getJdbcServices()
);
final PreparedStatement statement = delegate.prepareStatement( jdbcMutation.getSqlString(), session );
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().register( null, statement );
return statement;
}

public static PreparedStatement standardStatementPreparation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
* MutationExecutorPostInsertSingleTable variants
*
* @author Steve Ebersole
*
* @deprecated This was consolidated into {@link MutationExecutorSingleNonBatched}.
*/
@Deprecated( since = "7.0", forRemoval = true )
public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBindingsImpl.JdbcValueDescriptorAccess {
protected final EntityMutationTarget mutationTarget;
private final MutationType mutationType;
Expand All @@ -70,7 +73,11 @@ public MutationExecutorPostInsert(
this.mutationOperationGroup = mutationOperationGroup;

final PreparableMutationOperation mutationOperation = (PreparableMutationOperation) mutationOperationGroup.getOperation( mutationTarget.getIdentifierTableName() );
this.mutationStatementDetails = ModelMutationHelper.delegatePreparation( mutationOperation, session );
this.mutationStatementDetails = ModelMutationHelper.standardPreparation(
mutationOperation,
mutationTarget.getMutationDelegate( mutationOperation.getMutationType() ),
session
);
this.mutationType = mutationOperation.getMutationType();

this.valueBindings = new JdbcValueBindingsImpl(
Expand Down Expand Up @@ -104,6 +111,7 @@ public MutationExecutorPostInsert(
this.secondaryTablesStatementGroup = ModelMutationHelper.toPreparedStatementGroup(
mutationType,
mutationTarget,
null,
secondaryTableMutations,
session
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.hibernate.sql.model.ValuesAnalysis;
import org.hibernate.sql.model.jdbc.JdbcValueDescriptor;

import static org.hibernate.engine.jdbc.mutation.internal.ModelMutationHelper.delegatePreparation;
import static org.hibernate.engine.jdbc.mutation.internal.ModelMutationHelper.standardPreparation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;

/**
Expand All @@ -40,7 +40,10 @@
* MutationExecutorPostInsertSingleTable variants
*
* @author Steve Ebersole
*
* @deprecated This was consolidated into {@link MutationExecutorSingleNonBatched}.
*/
@Deprecated( since = "7.0", forRemoval = true )
public class MutationExecutorPostInsertSingleTable implements MutationExecutor, JdbcValueBindingsImpl.JdbcValueDescriptorAccess {
private final EntityMutationTarget mutationTarget;
private final SharedSessionContractImplementor session;
Expand All @@ -58,7 +61,7 @@ public MutationExecutorPostInsertSingleTable(
assert mutationOperationGroup.getNumberOfOperations() == 1;

this.operation = (PreparableMutationOperation) mutationOperationGroup.getOperation( mutationTarget.getIdentifierTableName() );
this.statemementDetails = delegatePreparation( operation, session );
this.statemementDetails = standardPreparation( operation, mutationTarget.getMutationDelegate( operation.getMutationType() ) , session );

this.valueBindings = new JdbcValueBindingsImpl(
operation.getMutationType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.engine.jdbc.mutation.OperationResultChecker;
import org.hibernate.engine.jdbc.mutation.TableInclusionChecker;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.values.GeneratedValuesMutationDelegate;
import org.hibernate.sql.model.PreparableMutationOperation;
import org.hibernate.sql.model.ValuesAnalysis;

Expand All @@ -17,12 +18,15 @@
*/
public class MutationExecutorSingleNonBatched extends AbstractSingleMutationExecutor {
private final PreparedStatementGroupSingleTable statementGroup;
private final GeneratedValuesMutationDelegate generatedValuesDelegate;

public MutationExecutorSingleNonBatched(
PreparableMutationOperation mutationOperation,
GeneratedValuesMutationDelegate generatedValuesDelegate,
SharedSessionContractImplementor session) {
super( mutationOperation, session );
this.statementGroup = new PreparedStatementGroupSingleTable( mutationOperation, session );
this.generatedValuesDelegate = generatedValuesDelegate;
this.statementGroup = new PreparedStatementGroupSingleTable( mutationOperation, generatedValuesDelegate, session );
prepareForNonBatchedWork( null, session );
}

Expand All @@ -32,18 +36,31 @@ protected PreparedStatementGroupSingleTable getStatementGroup() {
}

@Override
protected void performNonBatchedOperations(
protected Object performNonBatchedOperations(
Object modelReference,
ValuesAnalysis valuesAnalysis,
TableInclusionChecker inclusionChecker,
OperationResultChecker resultChecker,
SharedSessionContractImplementor session) {
performNonBatchedMutation(
statementGroup.getSingleStatementDetails(),
getJdbcValueBindings(),
inclusionChecker,
resultChecker,
session
);
if ( generatedValuesDelegate != null ) {
return generatedValuesDelegate.performMutation(
statementGroup.getSingleStatementDetails(),
getJdbcValueBindings(),
modelReference,
session
);
}
else {
performNonBatchedMutation(
statementGroup.getSingleStatementDetails(),
null,
getJdbcValueBindings(),
inclusionChecker,
resultChecker,
session
);
return null;
}
}

@Override
Expand Down
Loading

0 comments on commit e7b66f4

Please sign in to comment.