5
5
*/
6
6
package org .hibernate .reactive .sql .model ;
7
7
8
- import java .sql .PreparedStatement ;
9
- import java .sql .SQLException ;
10
- import java .util .Collections ;
11
8
import java .util .concurrent .CompletionStage ;
12
9
13
10
import org .hibernate .engine .jdbc .mutation .JdbcValueBindings ;
32
29
import org .hibernate .sql .model .jdbc .JdbcDeleteMutation ;
33
30
import org .hibernate .sql .model .jdbc .UpsertOperation ;
34
31
35
- import org .jboss .logging .Logger ;
36
-
37
32
import static java .lang .invoke .MethodHandles .lookup ;
33
+ import static java .util .Collections .emptyList ;
38
34
import static org .hibernate .reactive .logging .impl .LoggerFactory .make ;
39
35
import static org .hibernate .reactive .util .impl .CompletionStages .voidFuture ;
40
36
import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
41
37
42
- public class ReactiveDeleteOrUpsertOperation extends DeleteOrUpsertOperation implements ReactiveSelfExecutingUpdateOperation {
38
+ public class ReactiveDeleteOrUpsertOperation extends DeleteOrUpsertOperation
39
+ implements ReactiveSelfExecutingUpdateOperation {
43
40
private static final Log LOG = make ( Log .class , lookup () );
44
41
private final OptionalTableUpdate upsert ;
45
42
private final UpsertOperation upsertOperation ;
46
- private final UpsertStatementInfo upsertStatementInfo ;
43
+
47
44
public ReactiveDeleteOrUpsertOperation (
48
45
EntityMutationTarget mutationTarget ,
49
46
EntityTableMapping tableMapping ,
@@ -52,7 +49,6 @@ public ReactiveDeleteOrUpsertOperation(
52
49
super ( mutationTarget , tableMapping , upsertOperation , optionalTableUpdate );
53
50
this .upsert = optionalTableUpdate ;
54
51
this .upsertOperation = upsertOperation ;
55
- this .upsertStatementInfo = new UpsertStatementInfo ( );
56
52
}
57
53
58
54
@ Override
@@ -76,39 +72,33 @@ public CompletionStage<Void> performReactiveMutation(
76
72
return doReactiveMutation ( getTableDetails (), jdbcValueBindings , valuesAnalysis , session );
77
73
}
78
74
79
- /**
80
- *
81
- * @see DeleteOrUpsertOperation#performMutation(JdbcValueBindings, ValuesAnalysis, SharedSessionContractImplementor)
82
- * @param tableMapping
83
- * @param jdbcValueBindings
84
- * @param valuesAnalysis
85
- * @param session
86
- * @return
87
- */
88
75
private CompletionStage <Void > doReactiveMutation (
89
76
TableMapping tableMapping ,
90
77
JdbcValueBindings jdbcValueBindings ,
91
78
UpdateValuesAnalysis valuesAnalysis ,
92
79
SharedSessionContractImplementor session ) {
93
80
94
81
return voidFuture ()
95
- .thenCompose ( v -> {
96
- if ( !valuesAnalysis .getTablesWithNonNullValues ().contains ( tableMapping ) ) {
97
- return performReactiveDelete ( jdbcValueBindings , session );
98
- }
99
- else {
100
- return performReactiveUpsert ( jdbcValueBindings ,session );
101
- }
102
- } )
82
+ .thenCompose ( v -> !valuesAnalysis .getTablesWithNonNullValues ().contains ( tableMapping )
83
+ ? performReactiveDelete ( jdbcValueBindings , session )
84
+ : performReactiveUpsert ( jdbcValueBindings , session )
85
+ )
103
86
.whenComplete ( (o , throwable ) -> jdbcValueBindings .afterStatement ( tableMapping ) );
104
87
}
105
88
106
- private CompletionStage <Void > performReactiveUpsert (JdbcValueBindings jdbcValueBindings , SharedSessionContractImplementor session ) {
107
- final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable ( upsertOperation , session );
108
- final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( getTableDetails ().getTableName () );
109
- upsertStatementInfo .setStatementDetails ( statementDetails );
89
+ private CompletionStage <Void > performReactiveUpsert (
90
+ JdbcValueBindings jdbcValueBindings ,
91
+ SharedSessionContractImplementor session ) {
92
+ final String tableName = getTableDetails ().getTableName ();
93
+ MODEL_MUTATION_LOGGER .tracef ( "#performReactiveUpsert(%s)" , tableName );
110
94
111
- // If we get here the statement is needed - make sure it is resolved
95
+ final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable (
96
+ upsertOperation ,
97
+ session
98
+ );
99
+ final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( tableName );
100
+
101
+ session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
112
102
Object [] params = PreparedStatementAdaptor .bind ( statement -> {
113
103
PreparedStatementDetails details = new PrepareStatementDetailsAdaptor (
114
104
statementDetails ,
@@ -119,40 +109,26 @@ private CompletionStage<Void> performReactiveUpsert(JdbcValueBindings jdbcValueB
119
109
} );
120
110
121
111
ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier ) session ).getReactiveConnection ();
122
- String sqlString = statementDetails .getSqlString ();
123
112
return reactiveConnection
124
- .update ( sqlString , params ).thenCompose (this ::checkUpsertResults );
125
- }
126
-
127
- private CompletionStage <Void > checkUpsertResults ( Integer rowCount ) {
128
- if ( rowCount > 0 ) {
129
- try {
130
- upsert .getExpectation ()
131
- .verifyOutcome (
132
- rowCount ,
133
- upsertStatementInfo .getStatementDetails (),
134
- -1 ,
135
- upsertStatementInfo .getStatementSqlString ()
136
- );
137
- return voidFuture ();
138
- }
139
- catch (SQLException e ) {
140
- LOG .log ( Logger .Level .ERROR , e );
141
- }
142
- }
143
- return voidFuture ();
113
+ .update ( statementDetails .getSqlString (), params )
114
+ .thenAccept ( rowCount -> MODEL_MUTATION_LOGGER
115
+ .tracef ( "`%s` rows upserted into `%s`" , rowCount , getTableDetails ().getTableName () )
116
+ );
144
117
}
145
118
146
- private CompletionStage <Void > performReactiveDelete (JdbcValueBindings jdbcValueBindings , SharedSessionContractImplementor session ) {
147
- MODEL_MUTATION_LOGGER .tracef ( "#performDelete(%s)" , getTableDetails ().getTableName () );
119
+ private CompletionStage <Void > performReactiveDelete (
120
+ JdbcValueBindings jdbcValueBindings ,
121
+ SharedSessionContractImplementor session ) {
122
+ final String tableName = getTableDetails ().getTableName ();
123
+ MODEL_MUTATION_LOGGER .tracef ( "#performReactiveDelete(%s)" , tableName );
148
124
149
125
final TableDeleteStandard upsertDeleteAst = new TableDeleteStandard (
150
126
upsert .getMutatingTable (),
151
127
getMutationTarget (),
152
128
"upsert delete" ,
153
129
upsert .getKeyBindings (),
154
- Collections . emptyList (),
155
- Collections . emptyList ()
130
+ emptyList (),
131
+ emptyList ()
156
132
);
157
133
158
134
final SqlAstTranslator <JdbcDeleteMutation > translator = session
@@ -163,33 +139,18 @@ private CompletionStage<Void> performReactiveDelete(JdbcValueBindings jdbcValueB
163
139
final JdbcDeleteMutation upsertDelete = translator .translate ( null , MutationQueryOptions .INSTANCE );
164
140
165
141
final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable ( upsertDelete , session );
166
- final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( getTableDetails ().getTableName () );
142
+ final PreparedStatementDetails statementDetails = statementGroup .resolvePreparedStatementDetails ( tableName );
143
+
144
+ session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
167
145
Object [] params = PreparedStatementAdaptor .bind ( statement -> {
168
146
PreparedStatementDetails details = new PrepareStatementDetailsAdaptor ( statementDetails , statement , session .getJdbcServices () );
169
147
jdbcValueBindings .beforeStatement ( details );
170
148
} );
171
- session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
172
149
173
150
ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier ) session ).getReactiveConnection ();
174
151
String sqlString = statementDetails .getSqlString ();
175
152
return reactiveConnection
176
- .update ( sqlString , params ).thenCompose (this ::checkUpsertResults );
177
- }
178
-
179
- static class UpsertStatementInfo {
180
-
181
- PreparedStatementDetails statementDetails ;
182
-
183
- public void setStatementDetails (PreparedStatementDetails statementDetails ) {
184
- this .statementDetails = statementDetails ;
185
- }
186
-
187
- public PreparedStatement getStatementDetails () {
188
- return statementDetails .getStatement ();
189
- }
190
-
191
- public String getStatementSqlString () {
192
- return statementDetails .getSqlString ();
193
- }
153
+ .update ( sqlString , params )
154
+ .thenAccept ( rowCount -> MODEL_MUTATION_LOGGER .tracef ( "`%s` rows upsert-deleted from `%s`" , rowCount , tableName ) );
194
155
}
195
156
}
0 commit comments