#341 - Add support for derived delete query methods.
Original pull request: #345.
This commit is contained in:
@@ -39,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Roman Chigvintsev
|
||||
* @author Mark Paluch
|
||||
* @author Mingyuan Wu
|
||||
* @since 1.1
|
||||
*/
|
||||
public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<?>> {
|
||||
@@ -82,6 +83,10 @@ public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<
|
||||
protected PreparedOperation<?> complete(Criteria criteria, Sort sort) {
|
||||
|
||||
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityMetadata.getJavaType());
|
||||
if(tree.isDelete()){
|
||||
StatementMapper.DeleteSpec deleteSpec = statementMapper.createDelete(entityMetadata.getTableName()).withCriteria(criteria);
|
||||
return statementMapper.getMappedObject(deleteSpec);
|
||||
}
|
||||
StatementMapper.SelectSpec selectSpec = statementMapper.createSelect(entityMetadata.getTableName())
|
||||
.withProjection(getSelectProjection());
|
||||
|
||||
|
||||
@@ -592,6 +592,17 @@ public class PartTreeR2dbcQueryUnitTests {
|
||||
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1");
|
||||
}
|
||||
|
||||
@Test // gh-341
|
||||
public void createsQueryToDeleteByFirstName() throws Exception {
|
||||
R2dbcQueryMethod queryMethod = getQueryMethod("deleteByFirstName", String.class);
|
||||
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
|
||||
dataAccessStrategy);
|
||||
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
|
||||
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
|
||||
String expectedSql = "DELETE FROM "+ TABLE + " WHERE " + TABLE + ".first_name = $1" ;
|
||||
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
|
||||
}
|
||||
|
||||
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
|
||||
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
|
||||
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
|
||||
@@ -669,6 +680,8 @@ public class PartTreeR2dbcQueryUnitTests {
|
||||
Flux<User> findTop3ByFirstName(String firstName);
|
||||
|
||||
Mono<User> findFirstByFirstName(String firstName);
|
||||
|
||||
Mono<Integer> deleteByFirstName(String firstName);
|
||||
}
|
||||
|
||||
@Table("users")
|
||||
|
||||
Reference in New Issue
Block a user