4
4
*/
5
5
package org .hibernate .query .sqm .mutation .internal .temptable ;
6
6
7
- import java .util .UUID ;
8
- import java .util .function .Function ;
9
-
10
7
import org .hibernate .LockMode ;
11
8
import org .hibernate .LockOptions ;
12
- import org .hibernate .boot .TempTableDdlTransactionHandling ;
13
9
import org .hibernate .dialect .Dialect ;
14
10
import org .hibernate .dialect .temptable .TemporaryTable ;
15
11
import org .hibernate .dialect .temptable .TemporaryTableColumn ;
16
12
import org .hibernate .dialect .temptable .TemporaryTableHelper ;
13
+ import org .hibernate .dialect .temptable .TemporaryTableHelper .TemporaryTableCreationWork ;
14
+ import org .hibernate .dialect .temptable .TemporaryTableHelper .TemporaryTableDropWork ;
17
15
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
18
16
import org .hibernate .engine .jdbc .spi .JdbcServices ;
19
- import org .hibernate .engine .spi .SessionFactoryImplementor ;
20
17
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
21
- import org .hibernate .query .sqm .mutation .spi .AfterUseAction ;
22
- import org .hibernate .query .sqm .mutation .spi .BeforeUseAction ;
23
- import org .hibernate .resource .transaction .spi .IsolationDelegate ;
24
18
import org .hibernate .metamodel .mapping .BasicValuedMapping ;
25
19
import org .hibernate .metamodel .mapping .EntityMappingType ;
26
20
import org .hibernate .metamodel .mapping .ModelPart ;
27
21
import org .hibernate .query .sqm .ComparisonOperator ;
28
22
import org .hibernate .query .sqm .mutation .internal .MultiTableSqmMutationConverter ;
23
+ import org .hibernate .query .sqm .mutation .spi .AfterUseAction ;
24
+ import org .hibernate .query .sqm .mutation .spi .BeforeUseAction ;
29
25
import org .hibernate .spi .NavigablePath ;
30
26
import org .hibernate .sql .ast .SqlAstJoinType ;
31
- import org .hibernate .sql .ast .SqlAstTranslatorFactory ;
32
27
import org .hibernate .sql .ast .tree .expression .ColumnReference ;
33
28
import org .hibernate .sql .ast .tree .expression .QueryLiteral ;
34
29
import org .hibernate .sql .ast .tree .from .NamedTableReference ;
38
33
import org .hibernate .sql .ast .tree .insert .InsertSelectStatement ;
39
34
import org .hibernate .sql .ast .tree .predicate .ComparisonPredicate ;
40
35
import org .hibernate .sql .ast .tree .predicate .Predicate ;
36
+ import org .hibernate .sql .ast .tree .select .QueryPart ;
41
37
import org .hibernate .sql .ast .tree .select .QuerySpec ;
42
38
import org .hibernate .sql .exec .spi .ExecutionContext ;
43
- import org .hibernate .sql .exec .spi .JdbcOperationQueryMutation ;
44
39
import org .hibernate .sql .exec .spi .JdbcParameterBindings ;
45
40
import org .hibernate .sql .results .internal .SqlSelectionImpl ;
46
41
42
+ import java .util .UUID ;
43
+ import java .util .function .Function ;
44
+
45
+ import static org .hibernate .boot .TempTableDdlTransactionHandling .ISOLATE_AND_TRANSACT ;
46
+ import static org .hibernate .boot .TempTableDdlTransactionHandling .NONE ;
47
+
47
48
/**
48
49
* @author Steve Ebersole
49
50
*/
@@ -58,12 +59,8 @@ public static int saveMatchingIdsIntoIdTable(
58
59
Function <SharedSessionContractImplementor , String > sessionUidAccess ,
59
60
JdbcParameterBindings jdbcParameterBindings ,
60
61
ExecutionContext executionContext ) {
61
- final SessionFactoryImplementor factory = executionContext .getSession ().getFactory ();
62
-
63
62
final TableGroup mutatingTableGroup = sqmConverter .getMutatingTableGroup ();
64
-
65
- assert mutatingTableGroup .getModelPart () instanceof EntityMappingType ;
66
- final EntityMappingType mutatingEntityDescriptor = (EntityMappingType ) mutatingTableGroup .getModelPart ();
63
+ final var mutatingEntityDescriptor = (EntityMappingType ) mutatingTableGroup .getModelPart ();
67
64
68
65
final NamedTableReference idTableReference = new NamedTableReference (
69
66
idTable .getTableExpression (),
@@ -92,15 +89,14 @@ public static int saveMatchingIdsIntoIdTable(
92
89
93
90
mutatingEntityDescriptor .getIdentifierMapping ().forEachSelectable (
94
91
(selectionIndex , selection ) -> {
95
- final TableReference tableReference = mutatingTableGroup .resolveTableReference (
96
- mutatingTableGroup .getNavigablePath (),
97
- selection .getContainingTableExpression ()
98
- );
99
92
matchingIdSelection .getSelectClause ().addSqlSelection (
100
93
new SqlSelectionImpl (
101
94
selectionIndex ,
102
95
sqmConverter .getSqlExpressionResolver ().resolveSqlExpression (
103
- tableReference ,
96
+ mutatingTableGroup .resolveTableReference (
97
+ mutatingTableGroup .getNavigablePath (),
98
+ selection .getContainingTableExpression ()
99
+ ),
104
100
selection
105
101
)
106
102
)
@@ -129,40 +125,41 @@ public static int saveIntoTemporaryTable(
129
125
InsertSelectStatement temporaryTableInsert ,
130
126
JdbcParameterBindings jdbcParameterBindings ,
131
127
ExecutionContext executionContext ) {
132
- final SessionFactoryImplementor factory = executionContext .getSession ().getFactory ();
128
+ final var factory = executionContext .getSession ().getFactory ();
133
129
final JdbcServices jdbcServices = factory .getJdbcServices ();
134
130
final JdbcEnvironment jdbcEnvironment = jdbcServices .getJdbcEnvironment ();
135
- final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment .getSqlAstTranslatorFactory ();
136
131
final LockOptions lockOptions = executionContext .getQueryOptions ().getLockOptions ();
137
132
final LockMode lockMode = lockOptions .getLockMode ();
138
133
// Acquire a WRITE lock for the rows that are about to be modified
139
134
lockOptions .setLockMode ( LockMode .WRITE );
140
135
// Visit the table joins and reset the lock mode if we encounter OUTER joins that are not supported
141
- if ( temporaryTableInsert .getSourceSelectStatement () != null
136
+ final QueryPart sourceSelectStatement = temporaryTableInsert .getSourceSelectStatement ();
137
+ if ( sourceSelectStatement != null
142
138
&& !jdbcEnvironment .getDialect ().supportsOuterJoinForUpdate () ) {
143
- temporaryTableInsert . getSourceSelectStatement () .visitQuerySpecs (
139
+ sourceSelectStatement .visitQuerySpecs (
144
140
querySpec -> {
145
141
querySpec .getFromClause ().visitTableJoins (
146
142
tableJoin -> {
147
- if ( tableJoin .isInitialized () && tableJoin .getJoinType () != SqlAstJoinType .INNER ) {
143
+ if ( tableJoin .isInitialized ()
144
+ && tableJoin .getJoinType () != SqlAstJoinType .INNER ) {
148
145
lockOptions .setLockMode ( lockMode );
149
146
}
150
147
}
151
148
);
152
149
}
153
150
);
154
151
}
155
- final JdbcOperationQueryMutation jdbcInsert = sqlAstTranslatorFactory .buildMutationTranslator ( factory , temporaryTableInsert )
156
- .translate ( jdbcParameterBindings , executionContext .getQueryOptions () );
152
+ final var jdbcInsert =
153
+ jdbcEnvironment .getSqlAstTranslatorFactory ()
154
+ .buildMutationTranslator ( factory , temporaryTableInsert )
155
+ .translate ( jdbcParameterBindings , executionContext .getQueryOptions () );
157
156
lockOptions .setLockMode ( lockMode );
158
157
159
158
return jdbcServices .getJdbcMutationExecutor ().execute (
160
159
jdbcInsert ,
161
160
jdbcParameterBindings ,
162
- sql -> executionContext .getSession ()
163
- .getJdbcCoordinator ()
164
- .getStatementPreparer ()
165
- .prepareStatement ( sql ),
161
+ sql -> executionContext .getSession ().getJdbcCoordinator ()
162
+ .getStatementPreparer ().prepareStatement ( sql ),
166
163
(integer , preparedStatement ) -> {},
167
164
executionContext
168
165
);
@@ -201,7 +198,7 @@ public static QuerySpec createIdTableSelectQuerySpec(
201
198
202
199
querySpec .getFromClause ().addRoot ( idTableGroup );
203
200
204
- applyIdTableSelections ( querySpec , idTableReference , idTable , fkModelPart , executionContext );
201
+ applyIdTableSelections ( querySpec , idTableReference , idTable , fkModelPart );
205
202
applyIdTableRestrictions ( querySpec , idTableReference , idTable , sessionUidAccess , executionContext );
206
203
207
204
return querySpec ;
@@ -211,12 +208,11 @@ private static void applyIdTableSelections(
211
208
QuerySpec querySpec ,
212
209
TableReference tableReference ,
213
210
TemporaryTable idTable ,
214
- ModelPart fkModelPart ,
215
- ExecutionContext executionContext ) {
211
+ ModelPart fkModelPart ) {
216
212
if ( fkModelPart == null ) {
217
213
final int size = idTable .getEntityDescriptor ().getIdentifierMapping ().getJdbcTypeCount ();
218
214
for ( int i = 0 ; i < size ; i ++ ) {
219
- final TemporaryTableColumn temporaryTableColumn = idTable .getColumns ().get ( i );
215
+ final var temporaryTableColumn = idTable .getColumns ().get ( i );
220
216
if ( temporaryTableColumn != idTable .getSessionUidColumn () ) {
221
217
querySpec .getSelectClause ().addSqlSelection (
222
218
new SqlSelectionImpl (
@@ -282,25 +278,21 @@ private static void applyIdTableRestrictions(
282
278
public static void performBeforeTemporaryTableUseActions (
283
279
TemporaryTable temporaryTable ,
284
280
ExecutionContext executionContext ) {
285
- final SessionFactoryImplementor factory = executionContext .getSession ().getFactory ();
281
+ final var factory = executionContext .getSession ().getFactory ();
286
282
final Dialect dialect = factory .getJdbcServices ().getDialect ();
287
283
if ( dialect .getTemporaryTableBeforeUseAction () == BeforeUseAction .CREATE ) {
288
- final TemporaryTableHelper .TemporaryTableCreationWork temporaryTableCreationWork = new TemporaryTableHelper .TemporaryTableCreationWork (
289
- temporaryTable ,
290
- factory
291
- );
292
-
293
- final TempTableDdlTransactionHandling ddlTransactionHandling = dialect .getTemporaryTableDdlTransactionHandling ();
294
- if ( ddlTransactionHandling == TempTableDdlTransactionHandling .NONE ) {
284
+ final var temporaryTableCreationWork =
285
+ new TemporaryTableCreationWork ( temporaryTable , factory );
286
+ final var ddlTransactionHandling = dialect .getTemporaryTableDdlTransactionHandling ();
287
+ if ( ddlTransactionHandling == NONE ) {
295
288
executionContext .getSession ().doWork ( temporaryTableCreationWork );
296
289
}
297
290
else {
298
- final IsolationDelegate isolationDelegate = executionContext .getSession ()
299
- .getJdbcCoordinator ()
300
- .getJdbcSessionOwner ()
301
- .getTransactionCoordinator ()
302
- .createIsolationDelegate ();
303
- isolationDelegate .delegateWork ( temporaryTableCreationWork , ddlTransactionHandling == TempTableDdlTransactionHandling .ISOLATE_AND_TRANSACT );
291
+ final var isolationDelegate =
292
+ executionContext .getSession ().getJdbcCoordinator ().getJdbcSessionOwner ()
293
+ .getTransactionCoordinator ().createIsolationDelegate ();
294
+ isolationDelegate .delegateWork ( temporaryTableCreationWork ,
295
+ ddlTransactionHandling == ISOLATE_AND_TRANSACT );
304
296
}
305
297
}
306
298
}
@@ -310,7 +302,7 @@ public static void performAfterTemporaryTableUseActions(
310
302
Function <SharedSessionContractImplementor , String > sessionUidAccess ,
311
303
AfterUseAction afterUseAction ,
312
304
ExecutionContext executionContext ) {
313
- final SessionFactoryImplementor factory = executionContext .getSession ().getFactory ();
305
+ final var factory = executionContext .getSession ().getFactory ();
314
306
final Dialect dialect = factory .getJdbcServices ().getDialect ();
315
307
switch ( afterUseAction ) {
316
308
case CLEAN :
@@ -322,25 +314,17 @@ public static void performAfterTemporaryTableUseActions(
322
314
);
323
315
break ;
324
316
case DROP :
325
- final TemporaryTableHelper .TemporaryTableDropWork temporaryTableDropWork = new TemporaryTableHelper .TemporaryTableDropWork (
326
- temporaryTable ,
327
- factory
328
- );
329
-
330
- final TempTableDdlTransactionHandling ddlTransactionHandling = dialect .getTemporaryTableDdlTransactionHandling ();
331
- if ( ddlTransactionHandling == TempTableDdlTransactionHandling .NONE ) {
317
+ final var temporaryTableDropWork = new TemporaryTableDropWork ( temporaryTable , factory );
318
+ final var ddlTransactionHandling = dialect .getTemporaryTableDdlTransactionHandling ();
319
+ if ( ddlTransactionHandling == NONE ) {
332
320
executionContext .getSession ().doWork ( temporaryTableDropWork );
333
321
}
334
322
else {
335
- final IsolationDelegate isolationDelegate = executionContext .getSession ()
336
- .getJdbcCoordinator ()
337
- .getJdbcSessionOwner ()
338
- .getTransactionCoordinator ()
339
- .createIsolationDelegate ();
340
- isolationDelegate .delegateWork (
341
- temporaryTableDropWork ,
342
- ddlTransactionHandling == TempTableDdlTransactionHandling .ISOLATE_AND_TRANSACT
343
- );
323
+ final var isolationDelegate =
324
+ executionContext .getSession ().getJdbcCoordinator ().getJdbcSessionOwner ()
325
+ .getTransactionCoordinator ().createIsolationDelegate ();
326
+ isolationDelegate .delegateWork ( temporaryTableDropWork ,
327
+ ddlTransactionHandling == ISOLATE_AND_TRANSACT );
344
328
}
345
329
}
346
330
}
0 commit comments