DATAJDBC-534 - Derived Query support for count projection.
Original pull request: #215.
This commit is contained in:
committed by
Jens Schauder
parent
aa05b792c3
commit
bc6c88f055
@@ -34,6 +34,8 @@ import org.springframework.data.relational.core.mapping.RelationalPersistentProp
|
||||
import org.springframework.data.relational.core.query.Criteria;
|
||||
import org.springframework.data.relational.core.sql.Column;
|
||||
import org.springframework.data.relational.core.sql.Expression;
|
||||
import org.springframework.data.relational.core.sql.Expressions;
|
||||
import org.springframework.data.relational.core.sql.Functions;
|
||||
import org.springframework.data.relational.core.sql.Select;
|
||||
import org.springframework.data.relational.core.sql.SelectBuilder;
|
||||
import org.springframework.data.relational.core.sql.StatementBuilder;
|
||||
@@ -54,6 +56,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
* @author Myeonghyeon Lee
|
||||
* @since 2.0
|
||||
*/
|
||||
class JdbcQueryCreator extends RelationalQueryCreator<ParametrizedQuery> {
|
||||
@@ -208,6 +211,8 @@ class JdbcQueryCreator extends RelationalQueryCreator<ParametrizedQuery> {
|
||||
|
||||
Column idColumn = table.column(entity.getIdColumn());
|
||||
builder = Select.builder().select(idColumn).from(table);
|
||||
} else if (tree.isCountProjection()) {
|
||||
builder = Select.builder().select(Functions.count(Expressions.asterisk())).from(table);
|
||||
} else {
|
||||
builder = selectBuilder(table);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
* @author Roman Chigvintsev
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
* @author Myeonghyeon Lee
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PartTreeJdbcQueryUnitTests {
|
||||
@@ -548,6 +549,17 @@ public class PartTreeJdbcQueryUnitTests {
|
||||
assertThat(query.getParameterSource().getValue("user_street")).isEqualTo("Hello");
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-534
|
||||
public void createsQueryForCountProjection() throws Exception {
|
||||
|
||||
JdbcQueryMethod queryMethod = getQueryMethod("countByFirstName", String.class);
|
||||
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
|
||||
ParametrizedQuery query = jdbcQuery.createQuery((getAccessor(queryMethod, new Object[] { "John" })));
|
||||
|
||||
assertThat(query.getQuery()).isEqualTo(
|
||||
"SELECT COUNT(*) FROM " + TABLE + " WHERE " + TABLE + ".\"FIRST_NAME\" = :first_name");
|
||||
}
|
||||
|
||||
private PartTreeJdbcQuery createQuery(JdbcQueryMethod queryMethod) {
|
||||
return new PartTreeJdbcQuery(mappingContext, queryMethod, H2Dialect.INSTANCE, converter,
|
||||
mock(NamedParameterJdbcOperations.class), mock(RowMapper.class));
|
||||
@@ -639,6 +651,8 @@ public class PartTreeJdbcQueryUnitTests {
|
||||
User findByAddressStreet(String street);
|
||||
|
||||
User findByAnotherEmbeddedList(Object list);
|
||||
|
||||
long countByFirstName(String name);
|
||||
}
|
||||
|
||||
@Table("users")
|
||||
|
||||
Reference in New Issue
Block a user