Skip to content

Commit 1cc1674

Browse files
author
mhyeon-lee
committed
DATAJDBC-534 Derived Query support count projection
1 parent db23e56 commit 1cc1674

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.r2dbc.repository.query;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.stream.Collectors;
2122

@@ -27,7 +28,7 @@
2728
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
2829
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2930
import org.springframework.data.relational.core.query.Criteria;
30-
import org.springframework.data.relational.core.sql.SqlIdentifier;
31+
import org.springframework.data.relational.core.sql.*;
3132
import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
3233
import org.springframework.data.relational.repository.query.RelationalParameterAccessor;
3334
import org.springframework.data.relational.repository.query.RelationalQueryCreator;
@@ -41,6 +42,7 @@
4142
* @author Roman Chigvintsev
4243
* @author Mark Paluch
4344
* @author Mingyuan Wu
45+
* @author Myeonghyeon Lee
4446
* @since 1.1
4547
*/
4648
class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<?>> {
@@ -132,28 +134,39 @@ private PreparedOperation<?> select(@Nullable Criteria criteria, Sort sort, Stat
132134
return statementMapper.getMappedObject(selectSpec);
133135
}
134136

135-
private SqlIdentifier[] getSelectProjection() {
137+
private Expression[] getSelectProjection() {
136138

137-
List<SqlIdentifier> columnNames;
139+
List<Expression> expressions;
138140

141+
Table table = Table.create(entityMetadata.getTableName());
139142
if (!projectedProperties.isEmpty()) {
140143

141144
RelationalPersistentEntity<?> entity = entityMetadata.getTableEntity();
142-
columnNames = new ArrayList<>(projectedProperties.size());
145+
expressions = new ArrayList<>(projectedProperties.size());
143146

144147
for (String projectedProperty : projectedProperties) {
145148

146149
RelationalPersistentProperty property = entity.getPersistentProperty(projectedProperty);
147-
columnNames.add(property != null ? property.getColumnName() : SqlIdentifier.unquoted(projectedProperty));
150+
Column column = table.column(property != null ? property.getColumnName() : SqlIdentifier.unquoted(projectedProperty));
151+
expressions.add(column);
148152
}
149153

150154
} else if (tree.isExistsProjection()) {
151-
columnNames = dataAccessStrategy.getIdentifierColumns(entityMetadata.getJavaType());
155+
156+
expressions = dataAccessStrategy.getIdentifierColumns(entityMetadata.getJavaType()).stream()
157+
.map(table::column)
158+
.collect(Collectors.toList());
159+
} else if (tree.isCountProjection()) {
160+
161+
SqlIdentifier idColumn = entityMetadata.getTableEntity().getRequiredIdProperty().getColumnName();
162+
expressions = Collections.singletonList(Functions.count(table.column(idColumn)));
152163
} else {
153-
columnNames = dataAccessStrategy.getAllColumns(entityMetadata.getJavaType());
164+
expressions = dataAccessStrategy.getAllColumns(entityMetadata.getJavaType()).stream()
165+
.map(table::column)
166+
.collect(Collectors.toList());
154167
}
155168

156-
return columnNames.toArray(new SqlIdentifier[0]);
169+
return expressions.toArray(new Expression[0]);
157170
}
158171

159172
private Sort getSort(Sort sort) {

src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Roman Chigvintsev
5757
* @author Mark Paluch
5858
* @author Mingyuan Wu
59+
* @author Myeonghyeon Lee
5960
*/
6061
@RunWith(MockitoJUnitRunner.class)
6162
public class PartTreeR2dbcQueryUnitTests {
@@ -618,6 +619,18 @@ public void createsQueryToFindAllEntitiesByStringAttributeWithDistinct() throws
618619
+ ".foo FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1");
619620
}
620621

622+
@Test // DATAJDBC-534
623+
public void createsQueryForCountProjection() throws Exception {
624+
625+
R2dbcQueryMethod queryMethod = getQueryMethod("countByFirstName", String.class);
626+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
627+
dataAccessStrategy);
628+
BindableQuery query = r2dbcQuery.createQuery((getAccessor(queryMethod, new Object[] { "John" })));
629+
630+
assertThat(query.get())
631+
.isEqualTo("SELECT COUNT(users.id) FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1");
632+
}
633+
621634
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
622635
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
623636
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
@@ -699,6 +712,8 @@ interface UserRepository extends Repository<User, Long> {
699712
Mono<UserProjection> findDistinctByFirstName(String firstName);
700713

701714
Mono<Integer> deleteByFirstName(String firstName);
715+
716+
Mono<Long> countByFirstName(String firstName);
702717
}
703718

704719
@Table("users")

0 commit comments

Comments
 (0)