Throw an explicit exception if count query does not return a count. (#1195)

Also just use the first projected property instead of one explicitly named "count".

Closes #925,#960.
This commit is contained in:
Michael Reiche
2021-08-18 09:06:43 -07:00
committed by GitHub
parent 6344b94825
commit 7e2963a365
4 changed files with 25 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.couchbase.core.ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport;
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.util.Assert;
@@ -142,7 +143,11 @@ public class ExecutableFindByQueryOperationSupport implements ExecutableFindByQu
@Override
public long count() {
return reactiveSupport.count().block();
Long l = reactiveSupport.count().block();
if ( l == null ){
throw new CouchbaseQueryExecutionException("count query did not return a count : "+query.export());
}
return l;
}
@Override

View File

@@ -217,7 +217,8 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
} else {
return throwable;
}
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(TemplateUtils.SELECT_COUNT)).next());
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(row.getNames().iterator().next()))
.next());
}
@Override

View File

@@ -114,6 +114,12 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
Long countFancyExpression(@Param("projectIds") List<String> projectIds, @Param("planIds") List<String> planIds,
@Param("active") Boolean active);
@Query("SELECT 1 FROM `#{#n1ql.bucket}` WHERE 0 = 1" )
Long countBad();
@Query("SELECT count(*) FROM `#{#n1ql.bucket}`" )
Long countGood();
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Page<Airport> findAllByIataNot(String iata, Pageable pageable);

View File

@@ -50,6 +50,7 @@ import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.RemoveResult;
import org.springframework.data.couchbase.core.query.N1QLExpression;
@@ -474,6 +475,16 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
}
}
@Test
void badCount(){
assertThrows(CouchbaseQueryExecutionException.class, () -> airportRepository.countBad());
}
@Test
void goodCount(){
airportRepository.countGood();
}
@Test
void threadSafeParametersTest() throws Exception {
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };