Skip to content

Commit 8eddbd2

Browse files
committed
Add QueryWrapper support for R2DBC query customization
- Introduce QueryWrapper interface for query modification - Add DefaultQueryWrapper as no-op implementation - Integrate query wrapper into R2dbcEntityTemplate operations - Support query customization for count, exists, select, update, delete operations Signed-off-by: finger <[email protected]>
1 parent 6ae2380 commit 8eddbd2

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.springframework.data.r2dbc.core;
2+
3+
import org.springframework.data.relational.core.query.Query;
4+
5+
public class DefaultQueryWrapper implements QueryWrapper{
6+
@Override
7+
public Query wrapper(Query query, Class<?> domainType) {
8+
return query;
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.springframework.data.r2dbc.core;
2+
3+
import org.springframework.data.relational.core.query.Query;
4+
5+
public interface QueryWrapper {
6+
7+
Query wrapper(Query query, Class<?> domainType);
8+
}

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
114114

115115
private Function<Statement, Statement> statementFilterFunction = Function.identity();
116116

117+
private final QueryWrapper queryWrapper;
117118
/**
118119
* Create a new {@link R2dbcEntityTemplate} given {@link ConnectionFactory}.
119120
*
@@ -132,6 +133,7 @@ public R2dbcEntityTemplate(ConnectionFactory connectionFactory) {
132133
this.converter = dataAccessStrategy.getConverter();
133134
this.mappingContext = converter.getMappingContext();
134135
this.projectionFactory = new SpelAwareProxyProjectionFactory();
136+
this.queryWrapper=new DefaultQueryWrapper();
135137
}
136138

137139
/**
@@ -157,14 +159,22 @@ public R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect)
157159
public R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect, R2dbcConverter converter) {
158160
this(databaseClient, new DefaultReactiveDataAccessStrategy(dialect, converter));
159161
}
160-
161162
/**
162163
* Create a new {@link R2dbcEntityTemplate} given {@link DatabaseClient} and {@link ReactiveDataAccessStrategy}.
163164
*
164165
* @param databaseClient must not be {@literal null}.
165166
* @since 1.2
166167
*/
167168
public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy) {
169+
this(databaseClient,strategy,new DefaultQueryWrapper());
170+
}
171+
/**
172+
* Create a new {@link R2dbcEntityTemplate} given {@link DatabaseClient} and {@link ReactiveDataAccessStrategy}.
173+
*
174+
* @param databaseClient must not be {@literal null}.
175+
* @since 1.2
176+
*/
177+
public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy,QueryWrapper queryWrapper) {
168178

169179
Assert.notNull(databaseClient, "DatabaseClient must not be null");
170180
Assert.notNull(strategy, "ReactiveDataAccessStrategy must not be null");
@@ -174,6 +184,7 @@ public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStra
174184
this.converter = dataAccessStrategy.getConverter();
175185
this.mappingContext = strategy.getConverter().getMappingContext();
176186
this.projectionFactory = new SpelAwareProxyProjectionFactory();
187+
this.queryWrapper = queryWrapper;
177188
}
178189

179190
/**
@@ -274,7 +285,7 @@ public Mono<Long> count(Query query, Class<?> entityClass) throws DataAccessExce
274285
}
275286

276287
Mono<Long> doCount(Query query, Class<?> entityClass, SqlIdentifier tableName) {
277-
288+
query = queryWrapper.wrapper(query,entityClass);
278289
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
279290

280291
StatementMapper.SelectSpec selectSpec = statementMapper //
@@ -305,7 +316,7 @@ public Mono<Boolean> exists(Query query, Class<?> entityClass) throws DataAccess
305316
}
306317

307318
Mono<Boolean> doExists(Query query, Class<?> entityClass, SqlIdentifier tableName) {
308-
319+
query = queryWrapper.wrapper(query,entityClass);
309320
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
310321
StatementMapper.SelectSpec selectSpec = statementMapper.createSelect(tableName).limit(1)
311322
.withProjection(Expressions.just("1"));
@@ -352,12 +363,12 @@ <T, P extends Publisher<T>> P doSelect(Query query, Class<?> entityClass, SqlIde
352363

353364
private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdentifier tableName,
354365
Class<T> returnType, Function<? super Statement, ? extends Statement> filterFunction) {
355-
366+
query = queryWrapper.wrapper(query,entityType);
356367
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityType);
357-
368+
final Query finalQuery = query;
358369
StatementMapper.SelectSpec selectSpec = statementMapper //
359370
.createSelect(tableName) //
360-
.doWithTable((table, spec) -> spec.withProjection(getSelectProjection(table, query, entityType, returnType)));
371+
.doWithTable((table, spec) -> spec.withProjection(getSelectProjection(table, finalQuery, entityType, returnType)));
361372

362373
if (query.getLimit() > 0) {
363374
selectSpec = selectSpec.limit(query.getLimit());
@@ -402,7 +413,7 @@ public Mono<Long> update(Query query, Update update, Class<?> entityClass) throw
402413
}
403414

404415
Mono<Long> doUpdate(Query query, Update update, Class<?> entityClass, SqlIdentifier tableName) {
405-
416+
query = queryWrapper.wrapper(query, entityClass);
406417
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
407418

408419
StatementMapper.UpdateSpec selectSpec = statementMapper //
@@ -427,7 +438,7 @@ public Mono<Long> delete(Query query, Class<?> entityClass) throws DataAccessExc
427438
}
428439

429440
Mono<Long> doDelete(Query query, Class<?> entityClass, SqlIdentifier tableName) {
430-
441+
query = queryWrapper.wrapper(query, entityClass);
431442
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
432443

433444
StatementMapper.DeleteSpec deleteSpec = statementMapper //

0 commit comments

Comments
 (0)