Skip to content
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 @@ -144,16 +144,7 @@ public CompletionStage<Integer> reactiveExecute(DomainQueryExecutionContext exec
final BaseSqmToSqlAstConverter.AdditionalInsertValues additionalInsertValues = sqmConverter.visitInsertionTargetPaths(
(assignable, columnReferences) -> {
final SqmPathInterpretation<?> pathInterpretation = (SqmPathInterpretation<?>) assignable;
final int offset = CteTable.determineModelPartStartIndex(
entityDescriptor,
pathInterpretation.getExpressionType()
);
if ( offset == -1 ) {
throw new IllegalStateException( "Couldn't find matching cte column for: " + ( (Expression) assignable ).getExpressionType() );
}
final int end = offset + pathInterpretation.getExpressionType().getJdbcTypeCount();
// Find a matching cte table column and set that at the current index
final List<CteColumn> columns = getCteTable().getCteColumns().subList( offset, end );
final List<CteColumn> columns = getCteTable().findCteColumns( pathInterpretation.getExpressionType() );
insertStatement.addTargetColumnReferences( columnReferences );
targetPathCteColumns.addAll( columns );
targetPathColumns.add(
Expand Down Expand Up @@ -250,6 +241,17 @@ public CompletionStage<Integer> reactiveExecute(DomainQueryExecutionContext exec
);
}
}
if ( !assignsId && entityDescriptor.getGenerator().generatedOnExecution() ) {
querySpec.getSelectClause().addSqlSelection(
new SqlSelectionImpl(
0,
SqmInsertStrategyHelper.createRowNumberingExpression(
querySpec,
sessionFactory
)
)
);
}
final ValuesTableGroup valuesTableGroup = new ValuesTableGroup(
navigablePath,
entityDescriptor.getEntityPersister(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.function.Function;

import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.dialect.temptable.TemporaryTableStrategy;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.sqm.internal.DomainParameterXref;
import org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter;
import org.hibernate.query.sqm.mutation.internal.temptable.InsertExecutionDelegate;
import org.hibernate.query.sqm.mutation.spi.AfterUseAction;
import org.hibernate.reactive.query.sqm.mutation.internal.temptable.ReactiveTableBasedInsertHandler;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.TableGroup;
Expand All @@ -34,25 +34,29 @@ public class ReactiveInsertExecutionDelegate extends InsertExecutionDelegate imp
public ReactiveInsertExecutionDelegate(
MultiTableSqmMutationConverter sqmConverter,
TemporaryTable entityTable,
AfterUseAction afterUseAction,
TemporaryTableStrategy temporaryTableStrategy,
boolean forceDropAfterUse,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
DomainParameterXref domainParameterXref,
TableGroup insertingTableGroup,
Map<String, TableReference> tableReferenceByAlias,
List<Assignment> assignments,
boolean assignedId,
InsertSelectStatement insertStatement,
ConflictClause conflictClause,
JdbcParameter sessionUidParameter,
DomainQueryExecutionContext executionContext) {
super(
sqmConverter,
entityTable,
afterUseAction,
temporaryTableStrategy,
forceDropAfterUse,
sessionUidAccess,
domainParameterXref,
insertingTableGroup,
tableReferenceByAlias,
assignments,
assignedId,
insertStatement,
conflictClause,
sessionUidParameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.dialect.temptable.TemporaryTableColumn;
import org.hibernate.dialect.temptable.TemporaryTableStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -52,6 +53,7 @@
import org.hibernate.sql.results.internal.SqlSelectionImpl;

import static org.hibernate.reactive.query.sqm.mutation.internal.temptable.ReactiveTemporaryTableHelper.cleanTemporaryTableRows;
import static org.hibernate.reactive.util.impl.CompletionStages.falseFuture;
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;

/**
Expand Down Expand Up @@ -112,7 +114,10 @@ public static CompletionStage<Integer> saveMatchingIdsIntoIdTable(
new SqlSelectionImpl(
selectionIndex + 1,
sqmConverter.getSqlExpressionResolver().resolveSqlExpression(
tableReference,
mutatingTableGroup.resolveTableReference(
mutatingTableGroup.getNavigablePath(),
selection.getContainingTableExpression()
),
selection
)
)
Expand Down Expand Up @@ -155,7 +160,8 @@ public static CompletionStage<Integer> saveIntoTemporaryTable(
temporaryTableInsert.getSourceSelectStatement().visitQuerySpecs(
querySpec -> querySpec.getFromClause().visitTableJoins(
tableJoin -> {
if ( tableJoin.getJoinType() != SqlAstJoinType.INNER ) {
if ( tableJoin.isInitialized()
&& tableJoin.getJoinType() != SqlAstJoinType.INNER ) {
lockOptions.setLockMode( lockMode );
}
}
Expand Down Expand Up @@ -210,7 +216,7 @@ public static QuerySpec createIdTableSelectQuerySpec(

querySpec.getFromClause().addRoot( idTableGroup );

applyIdTableSelections( querySpec, idTableReference, idTable, fkModelPart );
applyIdTableSelections( querySpec, idTableReference, idTable, fkModelPart, entityDescriptor );
applyIdTableRestrictions( querySpec, idTableReference, idTable, sessionUidAccess, executionContext );

return querySpec;
Expand All @@ -221,9 +227,10 @@ private static void applyIdTableSelections(
QuerySpec querySpec,
TableReference tableReference,
TemporaryTable idTable,
ModelPart fkModelPart) {
ModelPart fkModelPart,
EntityMappingType entityDescriptor) {
if ( fkModelPart == null ) {
final int size = idTable.getEntityDescriptor().getIdentifierMapping().getJdbcTypeCount();
final int size = entityDescriptor.getIdentifierMapping().getJdbcTypeCount();
for ( int i = 0; i < size; i++ ) {
final TemporaryTableColumn temporaryTableColumn = idTable.getColumns().get( i );
if ( temporaryTableColumn != idTable.getSessionUidColumn() ) {
Expand Down Expand Up @@ -285,20 +292,45 @@ private static void applyIdTableRestrictions(
}
}

@Deprecated(forRemoval = true, since = "7.1")
public static CompletionStage<Void> performBeforeTemporaryTableUseActions(
TemporaryTable temporaryTable,
ExecutionContext executionContext) {
return performBeforeTemporaryTableUseActions(
temporaryTable,
executionContext.getSession().getDialect().getTemporaryTableBeforeUseAction(),
executionContext
).thenCompose( v -> voidFuture() );
}

public static CompletionStage<Boolean> performBeforeTemporaryTableUseActions(
TemporaryTable temporaryTable,
TemporaryTableStrategy temporaryTableStrategy,
ExecutionContext executionContext) {
return performBeforeTemporaryTableUseActions(
temporaryTable,
temporaryTableStrategy.getTemporaryTableBeforeUseAction(),
executionContext
);
}

public static CompletionStage<Boolean> performBeforeTemporaryTableUseActions(
TemporaryTable temporaryTable,
BeforeUseAction beforeUseAction,
ExecutionContext executionContext) {
final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
final Dialect dialect = factory.getJdbcServices().getDialect();
if ( dialect.getTemporaryTableBeforeUseAction() == BeforeUseAction.CREATE ) {
if ( beforeUseAction == BeforeUseAction.CREATE ) {
final TemporaryTableCreationWork temporaryTableCreationWork = new TemporaryTableCreationWork( temporaryTable, factory );
final TempTableDdlTransactionHandling ddlTransactionHandling = dialect.getTemporaryTableDdlTransactionHandling();
if ( ddlTransactionHandling == TempTableDdlTransactionHandling.NONE ) {
return temporaryTableCreationWork.reactiveExecute( ( (ReactiveConnectionSupplier) executionContext.getSession() ).getReactiveConnection() );
}
throw LOG.notYetImplemented();
}
return voidFuture();
else{
return falseFuture();
}
}

public static CompletionStage<Void> performAfterTemporaryTableUseActions(
Expand All @@ -312,13 +344,13 @@ public static CompletionStage<Void> performAfterTemporaryTableUseActions(
case CLEAN:
return cleanTemporaryTableRows( temporaryTable, dialect.getTemporaryTableExporter(), sessionUidAccess, executionContext.getSession() );
case DROP:
return dropAction( temporaryTable, executionContext, factory, dialect );
return dropAction( temporaryTable, executionContext, factory, dialect ).thenCompose( v -> voidFuture() );
default:
return voidFuture();
}
}

private static CompletionStage<Void> dropAction(
private static CompletionStage<Boolean> dropAction(
TemporaryTable temporaryTable,
ExecutionContext executionContext,
SessionFactoryImplementor factory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public CompletionStage<Integer> reactiveExecuteInsert(
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
// generally a global temp table should already track a Connection-specific uid,
// but just in case a particular env needs it...
ReactiveGlobalTemporaryTableStrategy::sessionIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public CompletionStage<Integer> reactiveExecuteUpdate(
sqmUpdateStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
ReactivePersistentTableStrategy::sessionIdentifier,
getSessionFactory()
).reactiveExecute( context ) );
Expand All @@ -75,7 +76,8 @@ public CompletionStage<Integer> reactiveExecuteDelete(
sqmDeleteStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
ReactiveGlobalTemporaryTableStrategy::sessionIdentifier,
getSessionFactory()
).reactiveExecute( context ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public CompletionStage<Integer> reactiveExecuteInsert(
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
afterUserAction(),
getTemporaryTableStrategy(),
isDropIdTables(),
ReactiveLocalTemporaryTableInsertStrategy::throwUnexpectedCallToSessionUIDError,
getSessionFactory()
).reactiveExecute( context );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public CompletionStage<Integer> reactiveExecuteUpdate(
sqmUpdate,
domainParameterXref,
getTemporaryTable(),
afterUseAction(),
getTemporaryTableStrategy(),
isDropIdTables(),
ReactiveLocalTemporaryTableMutationStrategy::throwUnexpectedAccessToSessionUID,
getSessionFactory()
).reactiveExecute( context );
Expand All @@ -52,7 +53,8 @@ public CompletionStage<Integer> reactiveExecuteDelete(
sqmDelete,
domainParameterXref,
getTemporaryTable(),
afterUseAction(),
getTemporaryTableStrategy(),
isDropIdTables(),
ReactiveLocalTemporaryTableMutationStrategy::throwUnexpectedAccessToSessionUID,
getSessionFactory()
).reactiveExecute( context );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public CompletionStage<Integer> reactiveExecuteInsert(
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
ReactivePersistentTableStrategy::sessionIdentifier,
getSessionFactory()
).reactiveExecute( context ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public CompletionStage<Integer> reactiveExecuteUpdate(
sqmUpdateStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
ReactivePersistentTableStrategy::sessionIdentifier,
getSessionFactory()
).reactiveExecute( context ) );
Expand All @@ -72,7 +73,8 @@ public CompletionStage<Integer> reactiveExecuteDelete(
sqmDeleteStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
getTemporaryTableStrategy(),
false,
ReactivePersistentTableStrategy::sessionIdentifier,
getSessionFactory()
).reactiveExecute( context ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.function.Supplier;

import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.dialect.temptable.TemporaryTableStrategy;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -83,7 +84,8 @@ public class ReactiveRestrictedDeleteExecutionDelegate

private final EntityMappingType entityDescriptor;
private final TemporaryTable idTable;
private final AfterUseAction afterUseAction;
private final TemporaryTableStrategy temporaryTableStrategy;
private final boolean forceDropAfterUse;
private final SqmDeleteStatement<?> sqmDelete;
private final DomainParameterXref domainParameterXref;
private final SessionFactoryImplementor sessionFactory;
Expand All @@ -94,7 +96,8 @@ public class ReactiveRestrictedDeleteExecutionDelegate
public ReactiveRestrictedDeleteExecutionDelegate(
EntityMappingType entityDescriptor,
TemporaryTable idTable,
AfterUseAction afterUseAction,
TemporaryTableStrategy temporaryTableStrategy,
boolean forceDropAfterUse,
SqmDeleteStatement<?> sqmDelete,
DomainParameterXref domainParameterXref,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
Expand All @@ -104,7 +107,8 @@ public ReactiveRestrictedDeleteExecutionDelegate(
SessionFactoryImplementor sessionFactory) {
this.entityDescriptor = entityDescriptor;
this.idTable = idTable;
this.afterUseAction = afterUseAction;
this.temporaryTableStrategy = temporaryTableStrategy;
this.forceDropAfterUse = forceDropAfterUse;
this.sqmDelete = sqmDelete;
this.domainParameterXref = domainParameterXref;
this.sessionUidAccess = sessionUidAccess;
Expand Down Expand Up @@ -517,14 +521,14 @@ public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T
);

return ReactiveExecuteWithTemporaryTableHelper
.performBeforeTemporaryTableUseActions( idTable, executionContext )
.performBeforeTemporaryTableUseActions( idTable, temporaryTableStrategy, executionContext )
.thenCompose( v -> executeUsingIdTable( predicate, executionContext, jdbcParameterBindings )
.handle( CompletionStages::handle )
.thenCompose( resultHandler -> ReactiveExecuteWithTemporaryTableHelper
.performAfterTemporaryTableUseActions(
idTable,
sessionUidAccess,
afterUseAction,
getAfterUseAction(),
executionContext
)
.thenCompose( resultHandler::getResultAsCompletionStage )
Expand Down Expand Up @@ -637,4 +641,9 @@ private CompletionStage<Integer> deleteFromTableUsingIdTable(
);
}

protected AfterUseAction getAfterUseAction() {
return forceDropAfterUse ? AfterUseAction.DROP : temporaryTableStrategy.getTemporaryTableAfterUseAction();
}


}
Loading
Loading