@@ -78,6 +78,8 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
78
78
79
79
private final R2dbcExceptionTranslator exceptionTranslator ;
80
80
81
+ private final ExecuteFunction executeFunction ;
82
+
81
83
private final ReactiveDataAccessStrategy dataAccessStrategy ;
82
84
83
85
private final boolean namedParameters ;
@@ -87,11 +89,12 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
87
89
private final ProjectionFactory projectionFactory ;
88
90
89
91
DefaultDatabaseClient (ConnectionFactory connector , R2dbcExceptionTranslator exceptionTranslator ,
90
- ReactiveDataAccessStrategy dataAccessStrategy , boolean namedParameters , ProjectionFactory projectionFactory ,
91
- DefaultDatabaseClientBuilder builder ) {
92
+ ExecuteFunction executeFunction , ReactiveDataAccessStrategy dataAccessStrategy , boolean namedParameters ,
93
+ ProjectionFactory projectionFactory , DefaultDatabaseClientBuilder builder ) {
92
94
93
95
this .connector = connector ;
94
96
this .exceptionTranslator = exceptionTranslator ;
97
+ this .executeFunction = executeFunction ;
95
98
this .dataAccessStrategy = dataAccessStrategy ;
96
99
this .namedParameters = namedParameters ;
97
100
this .projectionFactory = projectionFactory ;
@@ -264,25 +267,26 @@ protected DataAccessException translateException(String task, @Nullable String s
264
267
* Customization hook.
265
268
*/
266
269
protected <T > DefaultTypedExecuteSpec <T > createTypedExecuteSpec (Map <Integer , SettableValue > byIndex ,
267
- Map <String , SettableValue > byName , Supplier <String > sqlSupplier , Class <T > typeToRead ) {
268
- return new DefaultTypedExecuteSpec <>(byIndex , byName , sqlSupplier , typeToRead );
270
+ Map <String , SettableValue > byName , Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ,
271
+ Class <T > typeToRead ) {
272
+ return new DefaultTypedExecuteSpec <>(byIndex , byName , sqlSupplier , filterFunction , typeToRead );
269
273
}
270
274
271
275
/**
272
276
* Customization hook.
273
277
*/
274
278
protected <T > DefaultTypedExecuteSpec <T > createTypedExecuteSpec (Map <Integer , SettableValue > byIndex ,
275
- Map <String , SettableValue > byName , Supplier <String > sqlSupplier ,
279
+ Map <String , SettableValue > byName , Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ,
276
280
BiFunction <Row , RowMetadata , T > mappingFunction ) {
277
- return new DefaultTypedExecuteSpec <>(byIndex , byName , sqlSupplier , mappingFunction );
281
+ return new DefaultTypedExecuteSpec <>(byIndex , byName , sqlSupplier , filterFunction , mappingFunction );
278
282
}
279
283
280
284
/**
281
285
* Customization hook.
282
286
*/
283
287
protected ExecuteSpecSupport createGenericExecuteSpec (Map <Integer , SettableValue > byIndex ,
284
- Map <String , SettableValue > byName , Supplier <String > sqlSupplier ) {
285
- return new DefaultGenericExecuteSpec (byIndex , byName , sqlSupplier );
288
+ Map <String , SettableValue > byName , Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
289
+ return new DefaultGenericExecuteSpec (byIndex , byName , sqlSupplier , filterFunction );
286
290
}
287
291
288
292
/**
@@ -327,19 +331,22 @@ class ExecuteSpecSupport {
327
331
final Map <Integer , SettableValue > byIndex ;
328
332
final Map <String , SettableValue > byName ;
329
333
final Supplier <String > sqlSupplier ;
334
+ final StatementFilterFunction filterFunction ;
330
335
331
336
ExecuteSpecSupport (Supplier <String > sqlSupplier ) {
332
337
333
338
this .byIndex = Collections .emptyMap ();
334
339
this .byName = Collections .emptyMap ();
335
340
this .sqlSupplier = sqlSupplier ;
341
+ this .filterFunction = StatementFilterFunctions .empty ();
336
342
}
337
343
338
344
ExecuteSpecSupport (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
339
- Supplier <String > sqlSupplier ) {
345
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
340
346
this .byIndex = byIndex ;
341
347
this .byName = byName ;
342
348
this .sqlSupplier = sqlSupplier ;
349
+ this .filterFunction = filterFunction ;
343
350
}
344
351
345
352
<T > FetchSpec <T > exchange (Supplier <String > sqlSupplier , BiFunction <Row , RowMetadata , T > mappingFunction ) {
@@ -404,7 +411,7 @@ <T> FetchSpec<T> exchange(Supplier<String> sqlSupplier, BiFunction<Row, RowMetad
404
411
return statement ;
405
412
};
406
413
407
- Function <Connection , Flux <Result >> resultFunction = toExecuteFunction (sql , executeFunction );
414
+ Function <Connection , Flux <Result >> resultFunction = toFunction (sql , filterFunction , executeFunction );
408
415
409
416
return new DefaultSqlResult <>(DefaultDatabaseClient .this , //
410
417
sql , //
@@ -426,7 +433,7 @@ public ExecuteSpecSupport bind(int index, Object value) {
426
433
byIndex .put (index , SettableValue .fromOrEmpty (value , value .getClass ()));
427
434
}
428
435
429
- return createInstance (byIndex , this .byName , this .sqlSupplier );
436
+ return createInstance (byIndex , this .byName , this .sqlSupplier , this . filterFunction );
430
437
}
431
438
432
439
public ExecuteSpecSupport bindNull (int index , Class <?> type ) {
@@ -436,7 +443,7 @@ public ExecuteSpecSupport bindNull(int index, Class<?> type) {
436
443
Map <Integer , SettableValue > byIndex = new LinkedHashMap <>(this .byIndex );
437
444
byIndex .put (index , SettableValue .empty (type ));
438
445
439
- return createInstance (byIndex , this .byName , this .sqlSupplier );
446
+ return createInstance (byIndex , this .byName , this .sqlSupplier , this . filterFunction );
440
447
}
441
448
442
449
public ExecuteSpecSupport bind (String name , Object value ) {
@@ -455,7 +462,7 @@ public ExecuteSpecSupport bind(String name, Object value) {
455
462
byName .put (name , SettableValue .fromOrEmpty (value , value .getClass ()));
456
463
}
457
464
458
- return createInstance (this .byIndex , byName , this .sqlSupplier );
465
+ return createInstance (this .byIndex , byName , this .sqlSupplier , this . filterFunction );
459
466
}
460
467
461
468
public ExecuteSpecSupport bindNull (String name , Class <?> type ) {
@@ -466,7 +473,14 @@ public ExecuteSpecSupport bindNull(String name, Class<?> type) {
466
473
Map <String , SettableValue > byName = new LinkedHashMap <>(this .byName );
467
474
byName .put (name , SettableValue .empty (type ));
468
475
469
- return createInstance (this .byIndex , byName , this .sqlSupplier );
476
+ return createInstance (this .byIndex , byName , this .sqlSupplier , this .filterFunction );
477
+ }
478
+
479
+ public ExecuteSpecSupport filter (StatementFilterFunction filter ) {
480
+
481
+ Assert .notNull (filter , "Statement FilterFunction must not be null!" );
482
+
483
+ return createInstance (this .byIndex , byName , this .sqlSupplier , this .filterFunction .andThen (filter ));
470
484
}
471
485
472
486
private void assertNotPreparedOperation () {
@@ -476,8 +490,8 @@ private void assertNotPreparedOperation() {
476
490
}
477
491
478
492
protected ExecuteSpecSupport createInstance (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
479
- Supplier <String > sqlSupplier ) {
480
- return new ExecuteSpecSupport (byIndex , byName , sqlSupplier );
493
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
494
+ return new ExecuteSpecSupport (byIndex , byName , sqlSupplier , filterFunction );
481
495
}
482
496
}
483
497
@@ -487,8 +501,8 @@ protected ExecuteSpecSupport createInstance(Map<Integer, SettableValue> byIndex,
487
501
protected class DefaultGenericExecuteSpec extends ExecuteSpecSupport implements GenericExecuteSpec {
488
502
489
503
DefaultGenericExecuteSpec (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
490
- Supplier <String > sqlSupplier ) {
491
- super (byIndex , byName , sqlSupplier );
504
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
505
+ super (byIndex , byName , sqlSupplier , filterFunction );
492
506
}
493
507
494
508
DefaultGenericExecuteSpec (Supplier <String > sqlSupplier ) {
@@ -500,7 +514,7 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
500
514
501
515
Assert .notNull (resultType , "Result type must not be null!" );
502
516
503
- return createTypedExecuteSpec (this .byIndex , this .byName , this .sqlSupplier , resultType );
517
+ return createTypedExecuteSpec (this .byIndex , this .byName , this .sqlSupplier , this . filterFunction , resultType );
504
518
}
505
519
506
520
@ Override
@@ -549,10 +563,15 @@ public DefaultGenericExecuteSpec bindNull(String name, Class<?> type) {
549
563
return (DefaultGenericExecuteSpec ) super .bindNull (name , type );
550
564
}
551
565
566
+ @ Override
567
+ public DefaultGenericExecuteSpec filter (StatementFilterFunction filter ) {
568
+ return (DefaultGenericExecuteSpec ) super .filter (filter );
569
+ }
570
+
552
571
@ Override
553
572
protected ExecuteSpecSupport createInstance (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
554
- Supplier <String > sqlSupplier ) {
555
- return createGenericExecuteSpec (byIndex , byName , sqlSupplier );
573
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
574
+ return createGenericExecuteSpec (byIndex , byName , sqlSupplier , filterFunction );
556
575
}
557
576
}
558
577
@@ -566,9 +585,9 @@ protected class DefaultTypedExecuteSpec<T> extends ExecuteSpecSupport implements
566
585
private final BiFunction <Row , RowMetadata , T > mappingFunction ;
567
586
568
587
DefaultTypedExecuteSpec (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
569
- Supplier <String > sqlSupplier , Class <T > typeToRead ) {
588
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction , Class <T > typeToRead ) {
570
589
571
- super (byIndex , byName , sqlSupplier );
590
+ super (byIndex , byName , sqlSupplier , filterFunction );
572
591
573
592
this .typeToRead = typeToRead ;
574
593
@@ -581,9 +600,10 @@ protected class DefaultTypedExecuteSpec<T> extends ExecuteSpecSupport implements
581
600
}
582
601
583
602
DefaultTypedExecuteSpec (Map <Integer , SettableValue > byIndex , Map <String , SettableValue > byName ,
584
- Supplier <String > sqlSupplier , BiFunction <Row , RowMetadata , T > mappingFunction ) {
603
+ Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ,
604
+ BiFunction <Row , RowMetadata , T > mappingFunction ) {
585
605
586
- super (byIndex , byName , sqlSupplier );
606
+ super (byIndex , byName , sqlSupplier , filterFunction );
587
607
588
608
this .typeToRead = null ;
589
609
this .mappingFunction = mappingFunction ;
@@ -594,7 +614,7 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
594
614
595
615
Assert .notNull (resultType , "Result type must not be null!" );
596
616
597
- return createTypedExecuteSpec (this .byIndex , this .byName , this .sqlSupplier , resultType );
617
+ return createTypedExecuteSpec (this .byIndex , this .byName , this .sqlSupplier , this . filterFunction , resultType );
598
618
}
599
619
600
620
@ Override
@@ -643,10 +663,15 @@ public DefaultTypedExecuteSpec<T> bindNull(String name, Class<?> type) {
643
663
return (DefaultTypedExecuteSpec <T >) super .bindNull (name , type );
644
664
}
645
665
666
+ @ Override
667
+ public DefaultTypedExecuteSpec <T > filter (StatementFilterFunction filter ) {
668
+ return (DefaultTypedExecuteSpec <T >) super .filter (filter );
669
+ }
670
+
646
671
@ Override
647
672
protected DefaultTypedExecuteSpec <T > createInstance (Map <Integer , SettableValue > byIndex ,
648
- Map <String , SettableValue > byName , Supplier <String > sqlSupplier ) {
649
- return createTypedExecuteSpec (byIndex , byName , sqlSupplier , this .typeToRead );
673
+ Map <String , SettableValue > byName , Supplier <String > sqlSupplier , StatementFilterFunction filterFunction ) {
674
+ return createTypedExecuteSpec (byIndex , byName , sqlSupplier , filterFunction , this .typeToRead );
650
675
}
651
676
}
652
677
@@ -735,7 +760,8 @@ <R> FetchSpec<R> execute(PreparedOperation<?> preparedOperation, BiFunction<Row,
735
760
736
761
String sql = getRequiredSql (preparedOperation );
737
762
Function <Connection , Statement > selectFunction = wrapPreparedOperation (sql , preparedOperation );
738
- Function <Connection , Flux <Result >> resultFunction = DefaultDatabaseClient .toExecuteFunction (sql , selectFunction );
763
+ Function <Connection , Flux <Result >> resultFunction = toFunction (sql , StatementFilterFunctions .empty (),
764
+ selectFunction );
739
765
740
766
return new DefaultSqlResult <>(DefaultDatabaseClient .this , //
741
767
sql , //
@@ -1432,7 +1458,8 @@ private <R> FetchSpec<R> exchangeInsert(BiFunction<Row, RowMetadata, R> mappingF
1432
1458
String sql = getRequiredSql (operation );
1433
1459
Function <Connection , Statement > insertFunction = wrapPreparedOperation (sql , operation )
1434
1460
.andThen (statement -> statement .returnGeneratedValues ());
1435
- Function <Connection , Flux <Result >> resultFunction = toExecuteFunction (sql , insertFunction );
1461
+ Function <Connection , Flux <Result >> resultFunction = toFunction (sql , StatementFilterFunctions .empty (),
1462
+ insertFunction );
1436
1463
1437
1464
return new DefaultSqlResult <>(this , //
1438
1465
sql , //
@@ -1445,7 +1472,8 @@ private UpdatedRowsFetchSpec exchangeUpdate(PreparedOperation<?> operation) {
1445
1472
1446
1473
String sql = getRequiredSql (operation );
1447
1474
Function <Connection , Statement > executeFunction = wrapPreparedOperation (sql , operation );
1448
- Function <Connection , Flux <Result >> resultFunction = toExecuteFunction (sql , executeFunction );
1475
+ Function <Connection , Flux <Result >> resultFunction = toFunction (sql , StatementFilterFunctions .empty (),
1476
+ executeFunction );
1449
1477
1450
1478
return new DefaultSqlResult <>(this , //
1451
1479
sql , //
@@ -1476,12 +1504,15 @@ private Function<Connection, Statement> wrapPreparedOperation(String sql, Prepar
1476
1504
};
1477
1505
}
1478
1506
1479
- private static Function <Connection , Flux <Result >> toExecuteFunction (String sql ,
1480
- Function <Connection , Statement > executeFunction ) {
1507
+ private Function <Connection , Flux <Result >> toFunction (String sql , StatementFilterFunction filterFunction ,
1508
+ Function <Connection , Statement > statementFactory ) {
1481
1509
1482
1510
return it -> {
1483
1511
1484
- Flux <Result > from = Flux .defer (() -> executeFunction .apply (it ).execute ()).cast (Result .class );
1512
+ Flux <Result > from = Flux .defer (() -> {
1513
+ Statement statement = statementFactory .apply (it );
1514
+ return filterFunction .filter (statement , executeFunction );
1515
+ }).cast (Result .class );
1485
1516
return from .checkpoint ("SQL \" " + sql + "\" [DatabaseClient]" );
1486
1517
};
1487
1518
}
0 commit comments