Skip to content

Commit a300fcc

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 8eddbd2 commit a300fcc

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.r2dbc.core;
217

318
import org.springframework.data.relational.core.query.Query;
419

5-
public class DefaultQueryWrapper implements QueryWrapper{
6-
@Override
7-
public Query wrapper(Query query, Class<?> domainType) {
8-
return query;
9-
}
20+
/**
21+
* Default implementation of {@link QueryWrapper} that returns the original query unchanged.
22+
*
23+
* @author Your Name
24+
* @since 3.4
25+
*/
26+
public class DefaultQueryWrapper implements QueryWrapper {
27+
28+
@Override
29+
public Query wrapper(Query query, Class<?> domainType) {
30+
return query;
31+
}
1032
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.r2dbc.core;
217

318
import org.springframework.data.relational.core.query.Query;
419

20+
/**
21+
* Interface for query wrapper implementations that allow customization of queries before execution.
22+
*
23+
* @author Your Name
24+
* @since 3.4
25+
*/
526
public interface QueryWrapper {
627

7-
Query wrapper(Query query, Class<?> domainType);
28+
/**
29+
* Wraps the given query for the specified domain type.
30+
*
31+
* @param query the original query to wrap, must not be {@literal null}.
32+
* @param domainType the domain type for which the query is executed, must not be {@literal null}.
33+
* @return the wrapped query, must not be {@literal null}.
34+
*/
35+
Query wrapper(Query query, Class<?> domainType);
836
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public R2dbcEntityTemplate(ConnectionFactory connectionFactory) {
133133
this.converter = dataAccessStrategy.getConverter();
134134
this.mappingContext = converter.getMappingContext();
135135
this.projectionFactory = new SpelAwareProxyProjectionFactory();
136-
this.queryWrapper=new DefaultQueryWrapper();
136+
this.queryWrapper = new DefaultQueryWrapper();
137137
}
138138

139139
/**
@@ -166,15 +166,17 @@ public R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect,
166166
* @since 1.2
167167
*/
168168
public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy) {
169-
this(databaseClient,strategy,new DefaultQueryWrapper());
169+
this(databaseClient, strategy, new DefaultQueryWrapper());
170170
}
171171
/**
172-
* Create a new {@link R2dbcEntityTemplate} given {@link DatabaseClient} and {@link ReactiveDataAccessStrategy}.
172+
* Create a new {@link R2dbcEntityTemplate} given {@link DatabaseClient}, {@link ReactiveDataAccessStrategy}, and {@link QueryWrapper}.
173173
*
174174
* @param databaseClient must not be {@literal null}.
175-
* @since 1.2
175+
* @param strategy must not be {@literal null}.
176+
* @param queryWrapper must not be {@literal null}.
177+
* @since 3.4
176178
*/
177-
public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy,QueryWrapper queryWrapper) {
179+
public R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy, QueryWrapper queryWrapper) {
178180

179181
Assert.notNull(databaseClient, "DatabaseClient must not be null");
180182
Assert.notNull(strategy, "ReactiveDataAccessStrategy must not be null");
@@ -285,7 +287,7 @@ public Mono<Long> count(Query query, Class<?> entityClass) throws DataAccessExce
285287
}
286288

287289
Mono<Long> doCount(Query query, Class<?> entityClass, SqlIdentifier tableName) {
288-
query = queryWrapper.wrapper(query,entityClass);
290+
query = queryWrapper.wrapper(query, entityClass);
289291
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
290292

291293
StatementMapper.SelectSpec selectSpec = statementMapper //
@@ -316,7 +318,7 @@ public Mono<Boolean> exists(Query query, Class<?> entityClass) throws DataAccess
316318
}
317319

318320
Mono<Boolean> doExists(Query query, Class<?> entityClass, SqlIdentifier tableName) {
319-
query = queryWrapper.wrapper(query,entityClass);
321+
query = queryWrapper.wrapper(query, entityClass);
320322
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
321323
StatementMapper.SelectSpec selectSpec = statementMapper.createSelect(tableName).limit(1)
322324
.withProjection(Expressions.just("1"));
@@ -363,7 +365,7 @@ <T, P extends Publisher<T>> P doSelect(Query query, Class<?> entityClass, SqlIde
363365

364366
private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdentifier tableName,
365367
Class<T> returnType, Function<? super Statement, ? extends Statement> filterFunction) {
366-
query = queryWrapper.wrapper(query,entityType);
368+
query = queryWrapper.wrapper(query, entityType);
367369
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityType);
368370
final Query finalQuery = query;
369371
StatementMapper.SelectSpec selectSpec = statementMapper //

0 commit comments

Comments
 (0)