6
6
7
7
import java .sql .PreparedStatement ;
8
8
import java .sql .SQLException ;
9
- import java .util .Collections ;
10
- import java .util .List ;
11
9
import java .util .Locale ;
12
- import java .util .Set ;
13
10
14
11
import org .hibernate .engine .jdbc .mutation .JdbcValueBindings ;
15
12
import org .hibernate .engine .jdbc .mutation .ParameterUsage ;
16
13
import org .hibernate .engine .jdbc .mutation .group .PreparedStatementDetails ;
17
14
import org .hibernate .engine .jdbc .mutation .internal .MutationQueryOptions ;
18
15
import org .hibernate .engine .jdbc .mutation .internal .PreparedStatementGroupSingleTable ;
19
16
import org .hibernate .engine .jdbc .mutation .spi .Binding ;
20
- import org .hibernate .engine .jdbc .mutation . spi .BindingGroup ;
17
+ import org .hibernate .engine .jdbc .spi .JdbcServices ;
21
18
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
22
19
import org .hibernate .persister .entity .mutation .EntityMutationTarget ;
23
20
import org .hibernate .persister .entity .mutation .EntityTableMapping ;
24
21
import org .hibernate .persister .entity .mutation .UpdateValuesAnalysis ;
25
- import org .hibernate .sql .ast .SqlAstTranslator ;
26
22
import org .hibernate .sql .model .MutationTarget ;
27
23
import org .hibernate .sql .model .MutationType ;
28
24
import org .hibernate .sql .model .SelfExecutingUpdateOperation ;
29
25
import org .hibernate .sql .model .TableMapping ;
30
26
import org .hibernate .sql .model .ValuesAnalysis ;
31
- import org .hibernate .sql .model .ast .ColumnValueParameter ;
32
27
import org .hibernate .sql .model .internal .OptionalTableUpdate ;
33
28
import org .hibernate .sql .model .internal .TableDeleteStandard ;
34
29
30
+ import static java .util .Collections .emptyList ;
35
31
import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
36
32
37
33
/**
@@ -92,12 +88,12 @@ public void performMutation(
92
88
SharedSessionContractImplementor session ) {
93
89
final UpdateValuesAnalysis analysis = (UpdateValuesAnalysis ) valuesAnalysis ;
94
90
95
- if ( !analysis .getTablesWithNonNullValues ().contains ( tableMapping ) ) {
96
- // all the new values are null - delete
97
- performDelete ( jdbcValueBindings , session );
91
+ if ( analysis .getTablesWithNonNullValues ().contains ( tableMapping ) ) {
92
+ performUpsert ( jdbcValueBindings , session );
98
93
}
99
94
else {
100
- performUpsert ( jdbcValueBindings , session );
95
+ // all the new values are null - delete
96
+ performDelete ( jdbcValueBindings , session );
101
97
}
102
98
}
103
99
@@ -109,31 +105,21 @@ private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionCon
109
105
mutationTarget ,
110
106
"upsert delete" ,
111
107
optionalTableUpdate .getKeyBindings (),
112
- Collections . emptyList (),
113
- Collections . emptyList ()
108
+ emptyList (),
109
+ emptyList ()
114
110
);
115
111
116
- final SqlAstTranslator <JdbcDeleteMutation > translator = session
117
- .getJdbcServices ()
118
- .getJdbcEnvironment ()
119
- .getSqlAstTranslatorFactory ()
120
- .buildModelMutationTranslator ( upsertDeleteAst , session .getFactory () );
121
- final JdbcDeleteMutation upsertDelete = translator .translate ( null , MutationQueryOptions .INSTANCE );
122
-
123
- final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable ( upsertDelete , session );
124
- final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( tableMapping .getTableName () );
125
-
112
+ final JdbcServices jdbcServices = session .getJdbcServices ();
113
+ final var upsertDelete =
114
+ jdbcServices .getJdbcEnvironment ().getSqlAstTranslatorFactory ()
115
+ .buildModelMutationTranslator ( upsertDeleteAst , session .getFactory () )
116
+ .translate ( null , MutationQueryOptions .INSTANCE );
117
+ final var statementGroup = new PreparedStatementGroupSingleTable ( upsertDelete , session );
118
+ final var statementDetails = statementGroup .resolvePreparedStatementDetails ( tableMapping .getTableName () );
126
119
try {
127
120
final PreparedStatement upsertDeleteStatement = statementDetails .resolveStatement ();
128
- session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
129
-
130
- bindDeleteKeyValues (
131
- jdbcValueBindings ,
132
- optionalTableUpdate .getParameters (),
133
- statementDetails ,
134
- session
135
- );
136
-
121
+ jdbcServices .getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
122
+ bindDeleteKeyValues ( jdbcValueBindings , statementDetails , session );
137
123
final int rowCount = session .getJdbcCoordinator ().getResultSetReturn ()
138
124
.executeUpdate ( upsertDeleteStatement , statementDetails .getSqlString () );
139
125
MODEL_MUTATION_LOGGER .tracef ( "`%s` rows upsert-deleted from `%s`" , rowCount , tableMapping .getTableName () );
@@ -145,29 +131,22 @@ private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionCon
145
131
146
132
private void bindDeleteKeyValues (
147
133
JdbcValueBindings jdbcValueBindings ,
148
- List <ColumnValueParameter > parameters ,
149
134
PreparedStatementDetails statementDetails ,
150
135
SharedSessionContractImplementor session ) {
151
136
final PreparedStatement statement = statementDetails .resolveStatement ();
152
-
153
- final BindingGroup bindingGroup = jdbcValueBindings .getBindingGroup ( tableMapping .getTableName () );
154
- final Set <Binding > bindings = bindingGroup .getBindings ();
155
-
156
137
int jdbcBindingPosition = 1 ;
157
- for ( Binding binding : bindings ) {
158
- if ( binding .getValueDescriptor ().getUsage () != ParameterUsage .RESTRICT ) {
159
- continue ;
138
+ for ( Binding binding : jdbcValueBindings .getBindingGroup ( tableMapping .getTableName () ).getBindings () ) {
139
+ if ( binding .getValueDescriptor ().getUsage () == ParameterUsage .RESTRICT ) {
140
+ bindKeyValue (
141
+ jdbcBindingPosition ++,
142
+ binding ,
143
+ binding .getValueDescriptor (),
144
+ statement ,
145
+ statementDetails .getSqlString (),
146
+ tableMapping ,
147
+ session
148
+ );
160
149
}
161
-
162
- bindKeyValue (
163
- jdbcBindingPosition ++,
164
- binding ,
165
- binding .getValueDescriptor (),
166
- statement ,
167
- statementDetails .getSqlString (),
168
- tableMapping ,
169
- session
170
- );
171
150
}
172
151
}
173
152
@@ -199,18 +178,15 @@ private static void bindKeyValue(
199
178
private void performUpsert (JdbcValueBindings jdbcValueBindings , SharedSessionContractImplementor session ) {
200
179
MODEL_MUTATION_LOGGER .tracef ( "#performUpsert(%s)" , tableMapping .getTableName () );
201
180
202
- final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable ( upsertOperation , session );
203
- final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( tableMapping .getTableName () );
204
-
181
+ final var statementGroup = new PreparedStatementGroupSingleTable ( upsertOperation , session );
182
+ final var statementDetails = statementGroup .resolvePreparedStatementDetails ( tableMapping .getTableName () );
205
183
try {
206
184
final PreparedStatement updateStatement = statementDetails .resolveStatement ();
207
185
session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
208
-
209
186
jdbcValueBindings .beforeStatement ( statementDetails );
210
-
211
- final int rowCount = session .getJdbcCoordinator ().getResultSetReturn ()
212
- .executeUpdate ( updateStatement , statementDetails .getSqlString () );
213
-
187
+ final int rowCount =
188
+ session .getJdbcCoordinator ().getResultSetReturn ()
189
+ .executeUpdate ( updateStatement , statementDetails .getSqlString () );
214
190
MODEL_MUTATION_LOGGER .tracef ( "`%s` rows upserted into `%s`" , rowCount , tableMapping .getTableName () );
215
191
}
216
192
finally {
0 commit comments