DATAJDBC-529 - Use specialized ResultSetExtractor for exists queries.
Exists queries do not look at the value returned in the ResultSet but just check if there is a row. Original pull request: #212.
This commit is contained in:
committed by
Mark Paluch
parent
2029a0c71d
commit
370ee4464e
@@ -80,10 +80,10 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery {
|
||||
* @param queryMethod must not be {@literal null}.
|
||||
* @param extractor must not be {@literal null}.
|
||||
* @param rowMapper must not be {@literal null}.
|
||||
* @return
|
||||
* @return a JdbcQueryExecution appropriate for {@literal queryMethod}. Guaranteed to be not {@literal null}.
|
||||
*/
|
||||
protected JdbcQueryExecution<?> getQueryExecution(JdbcQueryMethod queryMethod,
|
||||
@Nullable ResultSetExtractor<Object> extractor, RowMapper<Object> rowMapper) {
|
||||
@Nullable ResultSetExtractor<?> extractor, RowMapper<?> rowMapper) {
|
||||
|
||||
if (queryMethod.isModifyingQuery()) {
|
||||
return createModifyingQueryExecutor();
|
||||
|
||||
@@ -24,10 +24,13 @@ import org.springframework.data.relational.repository.query.RelationalParameterA
|
||||
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
/**
|
||||
* An {@link AbstractJdbcQuery} implementation based on a {@link PartTree}.
|
||||
*
|
||||
@@ -72,7 +75,9 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery {
|
||||
this.tree = new PartTree(queryMethod.getName(), queryMethod.getEntityInformation().getJavaType());
|
||||
JdbcQueryCreator.validate(this.tree, this.parameters, this.converter.getMappingContext());
|
||||
|
||||
this.execution = getQueryExecution(queryMethod, null, rowMapper);
|
||||
ResultSetExtractor<Boolean> extractor = tree.isExistsProjection() ? (ResultSet::next) : null;
|
||||
|
||||
this.execution = getQueryExecution(queryMethod, extractor, rowMapper);
|
||||
}
|
||||
|
||||
private Sort getDynamicSort(RelationalParameterAccessor accessor) {
|
||||
|
||||
@@ -26,11 +26,11 @@ import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.api.SoftAssertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -355,6 +355,22 @@ public class JdbcRepositoryIntegrationTests {
|
||||
assertThat(loaded.pointInTime).isNull();
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-529
|
||||
public void existsWorksAsExpected() {
|
||||
|
||||
DummyEntity dummy = repository.save(createDummyEntity());
|
||||
|
||||
SoftAssertions.assertSoftly(softly -> {
|
||||
|
||||
softly.assertThat(repository.existsByName(dummy.getName())) //
|
||||
.describedAs("Positive") //
|
||||
.isTrue();
|
||||
softly.assertThat(repository.existsByName("not an existing name")) //
|
||||
.describedAs("Positive") //
|
||||
.isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
private static DummyEntity createDummyEntity() {
|
||||
|
||||
DummyEntity entity = new DummyEntity();
|
||||
@@ -380,6 +396,8 @@ public class JdbcRepositoryIntegrationTests {
|
||||
|
||||
@Query("SELECT id_Prop from dummy_entity where id_Prop = :id")
|
||||
DummyEntity withMissingColumn(@Param("id") Long id);
|
||||
|
||||
boolean existsByName(String name);
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
Reference in New Issue
Block a user