Skip to content

Commit 7e2963a

Browse files
authored
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.
1 parent 6344b94 commit 7e2963a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByQueryOperationSupport.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.stream.Stream;
2020

2121
import org.springframework.data.couchbase.core.ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport;
22+
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
2223
import org.springframework.data.couchbase.core.query.Query;
2324
import org.springframework.util.Assert;
2425

@@ -142,7 +143,11 @@ public Stream<T> stream() {
142143

143144
@Override
144145
public long count() {
145-
return reactiveSupport.count().block();
146+
Long l = reactiveSupport.count().block();
147+
if ( l == null ){
148+
throw new CouchbaseQueryExecutionException("count query did not return a count : "+query.export());
149+
}
150+
return l;
146151
}
147152

148153
@Override

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperationSupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ public Mono<Long> count() {
217217
} else {
218218
return throwable;
219219
}
220-
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(TemplateUtils.SELECT_COUNT)).next());
220+
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(row.getNames().iterator().next()))
221+
.next());
221222
}
222223

223224
@Override

src/test/java/org/springframework/data/couchbase/domain/AirportRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
114114
Long countFancyExpression(@Param("projectIds") List<String> projectIds, @Param("planIds") List<String> planIds,
115115
@Param("active") Boolean active);
116116

117+
@Query("SELECT 1 FROM `#{#n1ql.bucket}` WHERE 0 = 1" )
118+
Long countBad();
119+
120+
@Query("SELECT count(*) FROM `#{#n1ql.bucket}`" )
121+
Long countGood();
122+
117123
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
118124
Page<Airport> findAllByIataNot(String iata, Pageable pageable);
119125

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.data.auditing.DateTimeProvider;
5151
import org.springframework.data.couchbase.CouchbaseClientFactory;
5252
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
53+
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
5354
import org.springframework.data.couchbase.core.CouchbaseTemplate;
5455
import org.springframework.data.couchbase.core.RemoveResult;
5556
import org.springframework.data.couchbase.core.query.N1QLExpression;
@@ -474,6 +475,16 @@ void count() {
474475
}
475476
}
476477

478+
@Test
479+
void badCount(){
480+
assertThrows(CouchbaseQueryExecutionException.class, () -> airportRepository.countBad());
481+
}
482+
483+
@Test
484+
void goodCount(){
485+
airportRepository.countGood();
486+
}
487+
477488
@Test
478489
void threadSafeParametersTest() throws Exception {
479490
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };

0 commit comments

Comments
 (0)