Skip to content

Commit 94dd216

Browse files
committed
Add override of toN1qlRemoveString() to StringQuery.
Closes #1131.
1 parent 29766bf commit 94dd216

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ public Flux<RemoveResult> all() {
8080
}
8181

8282
private QueryOptions buildQueryOptions() {
83-
final QueryOptions options = QueryOptions.queryOptions();
84-
if (scanConsistency != null) {
85-
options.scanConsistency(scanConsistency);
86-
}
87-
return options;
83+
return query.buildQueryOptions(scanConsistency);
8884
}
8985

9086
@Override

src/main/java/org/springframework/data/couchbase/core/query/Query.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,36 @@ StringBasedN1qlQueryParser.N1qlSpelValues getN1qlSpelValues(ReactiveCouchbaseTem
316316
return isCount ? sbnqp.getCountContext() : sbnqp.getStatementContext();
317317
}
318318

319+
/**
320+
* build QueryOptions from parameters and scanConsistency
321+
*
322+
* @param scanConsistency
323+
* @return QueryOptions
324+
*/
319325
/**
320326
* build QueryOptions from parameters and scanConsistency
321327
*
322328
* @param scanConsistency
323329
* @return QueryOptions
324330
*/
325331
public QueryOptions buildQueryOptions(QueryScanConsistency scanConsistency) {
326-
final QueryOptions options = QueryOptions.queryOptions();
332+
QueryOptions options = QueryOptions.queryOptions();
333+
if (options == null) { // add/override what we got from PseudoArgs
334+
options = QueryOptions.queryOptions();
335+
}
327336
if (getParameters() != null) {
328337
if (getParameters() instanceof JsonArray) {
329338
options.parameters((JsonArray) getParameters());
330339
} else {
331340
options.parameters((JsonObject) getParameters());
332341
}
333342
}
343+
if (scanConsistency == null || scanConsistency == QueryScanConsistency.NOT_BOUNDED && getScanConsistency() != null) {
344+
scanConsistency = getScanConsistency();
345+
}
334346
if (scanConsistency != null) {
335347
options.scanConsistency(scanConsistency);
336348
}
337-
338349
return options;
339350
}
340351

src/main/java/org/springframework/data/couchbase/core/query/StringQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.couchbase.client.java.json.JsonArray;
2121
import com.couchbase.client.java.json.JsonValue;
22+
import org.springframework.data.couchbase.repository.query.StringBasedN1qlQueryParser;
2223

2324
/**
2425
* Query created from the string in @Query annotation in the repository interface.
@@ -73,4 +74,15 @@ public String toN1qlSelectString(ReactiveCouchbaseTemplate template, String coll
7374
appendSkipAndLimit(statement);
7475
return statement.toString();
7576
}
77+
78+
@Override
79+
/**
80+
* toN1qlRemoveString - use toN1qlSelectString
81+
* @param template
82+
* @param collectionName
83+
* @param domainClass
84+
*/
85+
public String toN1qlRemoveString(ReactiveCouchbaseTemplate template, String collectionName, Class domainClass) {
86+
return toN1qlSelectString(template, collectionName, domainClass, domainClass, false, null);
87+
}
7688
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,9 +38,9 @@ public class Airport extends ComparableEntity {
3838

3939
String icao;
4040

41-
@CreatedBy private String createdBy;
41+
@Version Number version;
4242

43-
@Version long version;
43+
@CreatedBy private String createdBy;
4444

4545

4646
@PersistenceConstructor
@@ -62,6 +62,22 @@ public String getIcao() {
6262
return icao;
6363
}
6464

65+
public Airport withId(String id) {
66+
return new Airport(id, this.iata, this.icao);
67+
}
68+
69+
public Airport withIcao(String icao) {
70+
return new Airport(this.getId(), this.iata, icao);
71+
}
72+
73+
public Airport withIata(String iata) {
74+
return new Airport(this.getId(), iata, this.icao);
75+
}
76+
77+
public Airport clearVersion() {
78+
version = Long.valueOf(0);
79+
return this;
80+
}
6581
public String getCreatedBy() {
6682
return createdBy;
6783
}

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,32 +17,35 @@
1717
package org.springframework.data.couchbase.domain;
1818

1919
import java.util.List;
20+
import java.util.Optional;
2021

22+
import org.springframework.data.couchbase.core.RemoveResult;
23+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
2124
import org.springframework.data.couchbase.repository.Query;
2225
import org.springframework.data.couchbase.repository.ScanConsistency;
2326
import org.springframework.data.domain.Page;
2427
import org.springframework.data.domain.Pageable;
25-
import org.springframework.data.repository.PagingAndSortingRepository;
2628
import org.springframework.data.repository.query.Param;
2729
import org.springframework.stereotype.Repository;
2830

2931
import com.couchbase.client.java.query.QueryScanConsistency;
3032

3133
/**
32-
* template class for Reactive Couchbase operations
34+
* Airport repository for testing <br>
35+
* The DynamicProxyable interface exposes airportRepository.withScope(scope), withCollection() and withOptions() It's
36+
* necessary on the repository object itself because the withScope() etc methods need to return an object of type
37+
* AirportRepository so that one can code... airportRepository = airportRepository.withScope(scopeName) without having
38+
* to cast the result.
3339
*
3440
* @author Michael Nitschinger
3541
* @author Michael Reiche
3642
*/
3743
@Repository
38-
public interface AirportRepository extends PagingAndSortingRepository<Airport, String> {
44+
public interface AirportRepository extends CouchbaseRepository<Airport, String> {
3945

4046
@Override
4147
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
42-
Iterable<Airport> findAll();
43-
44-
@Override
45-
Airport save(Airport airport);
48+
List<Airport> findAll();
4649

4750
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
4851
List<Airport> findAllByIata(String iata);
@@ -57,6 +60,10 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
5760
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
5861
List<Airport> getAllByIata(String iata);
5962

63+
@Query("#{#n1ql.delete} WHERE #{#n1ql.filter} and iata = $1 #{#n1ql.returning}" )
64+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
65+
List<RemoveResult> deleteByIata(String iata);
66+
6067
@Query("SELECT __cas, * from `#{#n1ql.bucket}` where iata = $1")
6168
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
6269
List<Airport> getAllByIataNoID(String iata);
@@ -86,4 +93,8 @@ Long countFancyExpression(@Param("projectIds") List<String> projectIds, @Param("
8693

8794
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
8895
Page<Airport> findAllByIataNot(String iata, Pageable pageable);
96+
97+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
98+
Optional<Airport> findByIdAndIata(String id, String iata);
99+
89100
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,19 @@ void stringQueryTest() throws Exception {
310310
}
311311
}
312312

313+
@Test
314+
void stringDeleteTest() throws Exception {
315+
Airport airport = new Airport("airports::vie", "vie", "lowx");
316+
Airport otherAirport = new Airport("airports::xxx", "xxx", "lxxx");
317+
try {
318+
airportRepository.save(airport);
319+
airportRepository.save(otherAirport);
320+
assertEquals(1,airportRepository.deleteByIata("vie").size()); // gets exactly one with no exception
321+
} finally {
322+
airportRepository.deleteById(otherAirport.getId());
323+
}
324+
}
325+
313326
@Test
314327
void threadSafeStringParametersTest() throws Exception {
315328
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };

0 commit comments

Comments
 (0)