-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
TableBasedUpdateHandlerImpl.java
141 lines (124 loc) · 4.62 KB
/
TableBasedUpdateHandlerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query.sqm.consume.multitable.spi.idtable;
import java.util.List;
import org.hibernate.metamodel.model.domain.spi.EntityTypeDescriptor;
import org.hibernate.query.sqm.consume.multitable.spi.HandlerCreationContext;
import org.hibernate.query.sqm.consume.multitable.spi.HandlerExecutionContext;
import org.hibernate.query.sqm.consume.multitable.spi.UpdateHandler;
import org.hibernate.query.sqm.tree.SqmDeleteOrUpdateStatement;
import org.hibernate.query.sqm.tree.SqmUpdateStatement;
import org.hibernate.sql.ast.consume.spi.UpdateToJdbcUpdateConverter;
import org.hibernate.sql.ast.produce.spi.SqlAstUpdateDescriptor;
import org.hibernate.sql.ast.produce.sqm.spi.SqmUpdateToSqlAstConverterMultiTable;
import org.hibernate.sql.exec.spi.JdbcMutationExecutor;
import org.hibernate.sql.exec.spi.JdbcUpdate;
/**
* @author Steve Ebersole
*/
public class TableBasedUpdateHandlerImpl
extends AbstractTableBasedHandler
implements UpdateHandler {
private TableBasedUpdateHandlerImpl(
SqmDeleteOrUpdateStatement sqmDeleteOrUpdateStatement,
EntityTypeDescriptor entityDescriptor,
IdTable idTableInfo,
IdTableSupport idTableSupport,
SessionUidSupport sessionUidSupport,
BeforeUseAction beforeUseAction,
AfterUseAction afterUseAction,
IdTableManagementTransactionality transactionality,
HandlerCreationContext creationContext) {
super(
sqmDeleteOrUpdateStatement,
entityDescriptor,
idTableInfo,
sessionUidSupport,
beforeUseAction,
afterUseAction,
new IdTableHelper(
idTableInfo,
idTableSupport,
transactionality,
creationContext.getSessionFactory().getJdbcServices()
),
creationContext
);
}
@Override
public SqmUpdateStatement getSqmDeleteOrUpdateStatement() {
return (SqmUpdateStatement) super.getSqmDeleteOrUpdateStatement();
}
@Override
protected void performMutations(HandlerExecutionContext executionContext) {
boolean hasNoSecondaryTables = getSqmDeleteOrUpdateStatement().getEntityFromElement()
.getNavigableReference()
.getEntityDescriptor()
.getSecondaryTableBindings()
.isEmpty();
final List<SqlAstUpdateDescriptor> updateDescriptors = SqmUpdateToSqlAstConverterMultiTable.interpret(
getSqmDeleteOrUpdateStatement(),
generateIdTableSelect( executionContext ),
executionContext.getQueryOptions(),
executionContext
);
for ( SqlAstUpdateDescriptor updateDescriptor : updateDescriptors ) {
// convert each SQL AST UpdateStatement into a JdbcUpdate operation
// and execute it
final JdbcUpdate jdbcUpdate = UpdateToJdbcUpdateConverter.createJdbcUpdate(
updateDescriptor.getSqlAstStatement(),
executionContext.getSessionFactory()
);
JdbcMutationExecutor.NO_AFTER_STATEMENT_CALL.execute( jdbcUpdate, executionContext );
}
}
public static class Builder {
private final SqmDeleteOrUpdateStatement sqmStatement;
private final EntityTypeDescriptor entityDescriptor;
private final IdTable idTableInfo;
private final IdTableSupport idTableSupport;
private SessionUidSupport sessionUidSupport = SessionUidSupport.NONE;
private BeforeUseAction beforeUseAction = BeforeUseAction.NONE;
private AfterUseAction afterUseAction = AfterUseAction.NONE;
private IdTableManagementTransactionality transactionality = IdTableManagementTransactionality.NONE;
public Builder(
SqmUpdateStatement sqmStatement,
EntityTypeDescriptor entityDescriptor,
IdTable idTableInfo,
IdTableSupport idTableSupport) {
this.sqmStatement = sqmStatement;
this.entityDescriptor = entityDescriptor;
this.idTableInfo = idTableInfo;
this.idTableSupport = idTableSupport;
}
public void setSessionUidSupport(SessionUidSupport sessionUidSupport) {
this.sessionUidSupport = sessionUidSupport;
}
public void setBeforeUseAction(BeforeUseAction beforeUseAction) {
this.beforeUseAction = beforeUseAction;
}
public void setAfterUseAction(AfterUseAction afterUseAction) {
this.afterUseAction = afterUseAction;
}
public void setTransactionality(IdTableManagementTransactionality transactionality) {
this.transactionality = transactionality;
}
public TableBasedUpdateHandlerImpl build(HandlerCreationContext creationContext) {
return new TableBasedUpdateHandlerImpl(
sqmStatement,
entityDescriptor,
idTableInfo,
idTableSupport,
sessionUidSupport,
beforeUseAction,
afterUseAction,
transactionality,
creationContext
);
}
}
}