55 */
66package org .hibernate .reactive .sql .model ;
77
8- import java .sql .PreparedStatement ;
9- import java .sql .SQLException ;
10- import java .util .Collections ;
118import java .util .concurrent .CompletionStage ;
129
1310import org .hibernate .engine .jdbc .mutation .JdbcValueBindings ;
3229import org .hibernate .sql .model .jdbc .JdbcDeleteMutation ;
3330import org .hibernate .sql .model .jdbc .UpsertOperation ;
3431
35- import org .jboss .logging .Logger ;
36-
3732import static java .lang .invoke .MethodHandles .lookup ;
33+ import static java .util .Collections .emptyList ;
3834import static org .hibernate .reactive .logging .impl .LoggerFactory .make ;
3935import static org .hibernate .reactive .util .impl .CompletionStages .voidFuture ;
4036import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
4137
42- public class ReactiveDeleteOrUpsertOperation extends DeleteOrUpsertOperation implements ReactiveSelfExecutingUpdateOperation {
38+ public class ReactiveDeleteOrUpsertOperation extends DeleteOrUpsertOperation
39+ implements ReactiveSelfExecutingUpdateOperation {
4340 private static final Log LOG = make ( Log .class , lookup () );
4441 private final OptionalTableUpdate upsert ;
4542 private final UpsertOperation upsertOperation ;
46- private final UpsertStatementInfo upsertStatementInfo ;
43+
4744 public ReactiveDeleteOrUpsertOperation (
4845 EntityMutationTarget mutationTarget ,
4946 EntityTableMapping tableMapping ,
@@ -52,7 +49,6 @@ public ReactiveDeleteOrUpsertOperation(
5249 super ( mutationTarget , tableMapping , upsertOperation , optionalTableUpdate );
5350 this .upsert = optionalTableUpdate ;
5451 this .upsertOperation = upsertOperation ;
55- this .upsertStatementInfo = new UpsertStatementInfo ( );
5652 }
5753
5854 @ Override
@@ -76,39 +72,33 @@ public CompletionStage<Void> performReactiveMutation(
7672 return doReactiveMutation ( getTableDetails (), jdbcValueBindings , valuesAnalysis , session );
7773 }
7874
79- /**
80- *
81- * @see DeleteOrUpsertOperation#performMutation(JdbcValueBindings, ValuesAnalysis, SharedSessionContractImplementor)
82- * @param tableMapping
83- * @param jdbcValueBindings
84- * @param valuesAnalysis
85- * @param session
86- * @return
87- */
8875 private CompletionStage <Void > doReactiveMutation (
8976 TableMapping tableMapping ,
9077 JdbcValueBindings jdbcValueBindings ,
9178 UpdateValuesAnalysis valuesAnalysis ,
9279 SharedSessionContractImplementor session ) {
9380
9481 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+ )
10386 .whenComplete ( (o , throwable ) -> jdbcValueBindings .afterStatement ( tableMapping ) );
10487 }
10588
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 );
11094
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 () );
112102 Object [] params = PreparedStatementAdaptor .bind ( statement -> {
113103 PreparedStatementDetails details = new PrepareStatementDetailsAdaptor (
114104 statementDetails ,
@@ -119,40 +109,26 @@ private CompletionStage<Void> performReactiveUpsert(JdbcValueBindings jdbcValueB
119109 } );
120110
121111 ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier ) session ).getReactiveConnection ();
122- String sqlString = statementDetails .getSqlString ();
123112 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+ );
144117 }
145118
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 );
148124
149125 final TableDeleteStandard upsertDeleteAst = new TableDeleteStandard (
150126 upsert .getMutatingTable (),
151127 getMutationTarget (),
152128 "upsert delete" ,
153129 upsert .getKeyBindings (),
154- Collections . emptyList (),
155- Collections . emptyList ()
130+ emptyList (),
131+ emptyList ()
156132 );
157133
158134 final SqlAstTranslator <JdbcDeleteMutation > translator = session
@@ -163,33 +139,18 @@ private CompletionStage<Void> performReactiveDelete(JdbcValueBindings jdbcValueB
163139 final JdbcDeleteMutation upsertDelete = translator .translate ( null , MutationQueryOptions .INSTANCE );
164140
165141 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 () );
167145 Object [] params = PreparedStatementAdaptor .bind ( statement -> {
168146 PreparedStatementDetails details = new PrepareStatementDetailsAdaptor ( statementDetails , statement , session .getJdbcServices () );
169147 jdbcValueBindings .beforeStatement ( details );
170148 } );
171- session .getJdbcServices ().getSqlStatementLogger ().logStatement ( statementDetails .getSqlString () );
172149
173150 ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier ) session ).getReactiveConnection ();
174151 String sqlString = statementDetails .getSqlString ();
175152 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 ) );
194155 }
195156}
0 commit comments