@@ -112,6 +112,8 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
112
112
113
113
private @ Nullable ReactiveEntityCallbacks entityCallbacks ;
114
114
115
+ private Function <Statement , Statement > statementFilterFunction = Function .identity ();
116
+
115
117
/**
116
118
* Create a new {@link R2dbcEntityTemplate} given {@link ConnectionFactory}.
117
119
*
@@ -174,6 +176,19 @@ public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStra
174
176
this .projectionFactory = new SpelAwareProxyProjectionFactory ();
175
177
}
176
178
179
+ /**
180
+ * Set a {@link Function Statement Filter Function} that is applied to every {@link Statement}.
181
+ *
182
+ * @param statementFilterFunction must not be {@literal null}.
183
+ * @since 3.4
184
+ */
185
+ public void setStatementFilterFunction (Function <Statement , Statement > statementFilterFunction ) {
186
+
187
+ Assert .notNull (statementFilterFunction , "StatementFilterFunction must not be null" );
188
+
189
+ this .statementFilterFunction = statementFilterFunction ;
190
+ }
191
+
177
192
@ Override
178
193
public DatabaseClient getDatabaseClient () {
179
194
return this .databaseClient ;
@@ -274,6 +289,7 @@ Mono<Long> doCount(Query query, Class<?> entityClass, SqlIdentifier tableName) {
274
289
PreparedOperation <?> operation = statementMapper .getMappedObject (selectSpec );
275
290
276
291
return this .databaseClient .sql (operation ) //
292
+ .filter (statementFilterFunction ) //
277
293
.map ((r , md ) -> r .get (0 , Long .class )) //
278
294
.first () //
279
295
.defaultIfEmpty (0L );
@@ -302,6 +318,7 @@ Mono<Boolean> doExists(Query query, Class<?> entityClass, SqlIdentifier tableNam
302
318
PreparedOperation <?> operation = statementMapper .getMappedObject (selectSpec );
303
319
304
320
return this .databaseClient .sql (operation ) //
321
+ .filter (statementFilterFunction ) //
305
322
.map ((r , md ) -> r ) //
306
323
.first () //
307
324
.hasElement ();
@@ -362,7 +379,7 @@ private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdent
362
379
PreparedOperation <?> operation = statementMapper .getMappedObject (selectSpec );
363
380
364
381
return getRowsFetchSpec (
365
- databaseClient .sql (operation ).filter (filterFunction ),
382
+ databaseClient .sql (operation ).filter (statementFilterFunction . andThen ( filterFunction ) ),
366
383
entityType ,
367
384
returnType
368
385
);
@@ -397,7 +414,7 @@ Mono<Long> doUpdate(Query query, Update update, Class<?> entityClass, SqlIdentif
397
414
}
398
415
399
416
PreparedOperation <?> operation = statementMapper .getMappedObject (selectSpec );
400
- return this .databaseClient .sql (operation ).fetch ().rowsUpdated ();
417
+ return this .databaseClient .sql (operation ).filter ( statementFilterFunction ). fetch ().rowsUpdated ();
401
418
}
402
419
403
420
@ Override
@@ -422,7 +439,7 @@ Mono<Long> doDelete(Query query, Class<?> entityClass, SqlIdentifier tableName)
422
439
}
423
440
424
441
PreparedOperation <?> operation = statementMapper .getMappedObject (deleteSpec );
425
- return this .databaseClient .sql (operation ).fetch ().rowsUpdated ().defaultIfEmpty (0L );
442
+ return this .databaseClient .sql (operation ).filter ( statementFilterFunction ). fetch ().rowsUpdated ().defaultIfEmpty (0L );
426
443
}
427
444
428
445
// -------------------------------------------------------------------------
@@ -441,7 +458,8 @@ public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entit
441
458
Assert .notNull (operation , "PreparedOperation must not be null" );
442
459
Assert .notNull (entityClass , "Entity class must not be null" );
443
460
444
- return new EntityCallbackAdapter <>(getRowsFetchSpec (databaseClient .sql (operation ), entityClass , resultType ),
461
+ return new EntityCallbackAdapter <>(
462
+ getRowsFetchSpec (databaseClient .sql (operation ).filter (statementFilterFunction ), entityClass , resultType ),
445
463
getTableNameOrEmpty (entityClass ));
446
464
}
447
465
@@ -451,7 +469,8 @@ public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, BiFunction<Row
451
469
Assert .notNull (operation , "PreparedOperation must not be null" );
452
470
Assert .notNull (rowMapper , "Row mapper must not be null" );
453
471
454
- return new EntityCallbackAdapter <>(databaseClient .sql (operation ).map (rowMapper ), SqlIdentifier .EMPTY );
472
+ return new EntityCallbackAdapter <>(databaseClient .sql (operation ).filter (statementFilterFunction ).map (rowMapper ),
473
+ SqlIdentifier .EMPTY );
455
474
}
456
475
457
476
@ Override
@@ -462,7 +481,8 @@ public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entit
462
481
Assert .notNull (entityClass , "Entity class must not be null" );
463
482
Assert .notNull (rowMapper , "Row mapper must not be null" );
464
483
465
- return new EntityCallbackAdapter <>(databaseClient .sql (operation ).map (rowMapper ), getTableNameOrEmpty (entityClass ));
484
+ return new EntityCallbackAdapter <>(databaseClient .sql (operation ).filter (statementFilterFunction ).map (rowMapper ),
485
+ getTableNameOrEmpty (entityClass ));
466
486
}
467
487
468
488
// -------------------------------------------------------------------------
@@ -541,6 +561,8 @@ private <T> Mono<T> doInsert(T entity, SqlIdentifier tableName, OutboundRow outb
541
561
return this .databaseClient .sql (operation ) //
542
562
.filter (statement -> {
543
563
564
+ statement = statementFilterFunction .apply (statement );
565
+
544
566
if (identifierColumns .isEmpty ()) {
545
567
return statement .returnGeneratedValues ();
546
568
}
@@ -632,6 +654,7 @@ private <T> Mono<T> doUpdate(T entity, SqlIdentifier tableName, RelationalPersis
632
654
PreparedOperation <?> operation = mapper .getMappedObject (updateSpec );
633
655
634
656
return this .databaseClient .sql (operation ) //
657
+ .filter (statementFilterFunction ) //
635
658
.fetch () //
636
659
.rowsUpdated () //
637
660
.handle ((rowsUpdated , sink ) -> {
0 commit comments