Add override of toN1qlRemoveString() to StringQuery. (#1135)
Closes #1131. Original pull request: #1135. Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
@@ -80,11 +80,7 @@ public class ReactiveRemoveByQueryOperationSupport implements ReactiveRemoveByQu
|
||||
}
|
||||
|
||||
private QueryOptions buildQueryOptions() {
|
||||
final QueryOptions options = QueryOptions.queryOptions();
|
||||
if (scanConsistency != null) {
|
||||
options.scanConsistency(scanConsistency);
|
||||
}
|
||||
return options;
|
||||
return query.buildQueryOptions(scanConsistency);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -323,7 +323,10 @@ public class Query {
|
||||
* @return QueryOptions
|
||||
*/
|
||||
public QueryOptions buildQueryOptions(QueryScanConsistency scanConsistency) {
|
||||
final QueryOptions options = QueryOptions.queryOptions();
|
||||
QueryOptions options = QueryOptions.queryOptions();
|
||||
if (options == null) { // add/override what we got from PseudoArgs
|
||||
options = QueryOptions.queryOptions();
|
||||
}
|
||||
if (getParameters() != null) {
|
||||
if (getParameters() instanceof JsonArray) {
|
||||
options.parameters((JsonArray) getParameters());
|
||||
@@ -331,10 +334,13 @@ public class Query {
|
||||
options.parameters((JsonObject) getParameters());
|
||||
}
|
||||
}
|
||||
if (scanConsistency == null
|
||||
|| scanConsistency == QueryScanConsistency.NOT_BOUNDED && getScanConsistency() != null) {
|
||||
scanConsistency = getScanConsistency();
|
||||
}
|
||||
if (scanConsistency != null) {
|
||||
options.scanConsistency(scanConsistency);
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,4 +73,16 @@ public class StringQuery extends Query {
|
||||
appendSkipAndLimit(statement);
|
||||
return statement.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* toN1qlRemoveString - use toN1qlSelectString
|
||||
*
|
||||
* @param template
|
||||
* @param collectionName
|
||||
* @param domainClass
|
||||
*/
|
||||
@Override
|
||||
public String toN1qlRemoveString(ReactiveCouchbaseTemplate template, String collectionName, Class domainClass) {
|
||||
return toN1qlSelectString(template, collectionName, domainClass, domainClass, false, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2019 the original author or authors.
|
||||
* Copyright 2017-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,9 +38,9 @@ public class Airport extends ComparableEntity {
|
||||
|
||||
String icao;
|
||||
|
||||
@CreatedBy private String createdBy;
|
||||
@Version Number version;
|
||||
|
||||
@Version long version;
|
||||
@CreatedBy private String createdBy;
|
||||
|
||||
|
||||
@PersistenceConstructor
|
||||
@@ -62,6 +62,22 @@ public class Airport extends ComparableEntity {
|
||||
return icao;
|
||||
}
|
||||
|
||||
public Airport withId(String id) {
|
||||
return new Airport(id, this.iata, this.icao);
|
||||
}
|
||||
|
||||
public Airport withIcao(String icao) {
|
||||
return new Airport(this.getId(), this.iata, icao);
|
||||
}
|
||||
|
||||
public Airport withIata(String iata) {
|
||||
return new Airport(this.getId(), iata, this.icao);
|
||||
}
|
||||
|
||||
public Airport clearVersion() {
|
||||
version = Long.valueOf(0);
|
||||
return this;
|
||||
}
|
||||
public String getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2019 the original author or authors.
|
||||
* Copyright 2017-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,32 +17,35 @@
|
||||
package org.springframework.data.couchbase.domain;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.couchbase.core.RemoveResult;
|
||||
import org.springframework.data.couchbase.repository.CouchbaseRepository;
|
||||
import org.springframework.data.couchbase.repository.Query;
|
||||
import org.springframework.data.couchbase.repository.ScanConsistency;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.couchbase.client.java.query.QueryScanConsistency;
|
||||
|
||||
/**
|
||||
* template class for Reactive Couchbase operations
|
||||
* Airport repository for testing <br>
|
||||
* The DynamicProxyable interface exposes airportRepository.withScope(scope), withCollection() and withOptions() It's
|
||||
* necessary on the repository object itself because the withScope() etc methods need to return an object of type
|
||||
* AirportRepository so that one can code... airportRepository = airportRepository.withScope(scopeName) without having
|
||||
* to cast the result.
|
||||
*
|
||||
* @author Michael Nitschinger
|
||||
* @author Michael Reiche
|
||||
*/
|
||||
@Repository
|
||||
public interface AirportRepository extends PagingAndSortingRepository<Airport, String> {
|
||||
public interface AirportRepository extends CouchbaseRepository<Airport, String> {
|
||||
|
||||
@Override
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
Iterable<Airport> findAll();
|
||||
|
||||
@Override
|
||||
Airport save(Airport airport);
|
||||
List<Airport> findAll();
|
||||
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<Airport> findAllByIata(String iata);
|
||||
@@ -57,6 +60,10 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<Airport> getAllByIata(String iata);
|
||||
|
||||
@Query("#{#n1ql.delete} WHERE #{#n1ql.filter} and iata = $1 #{#n1ql.returning}")
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<RemoveResult> deleteByIata(String iata);
|
||||
|
||||
@Query("SELECT __cas, * from `#{#n1ql.bucket}` where iata = $1")
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
List<Airport> getAllByIataNoID(String iata);
|
||||
@@ -86,4 +93,8 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
|
||||
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
Page<Airport> findAllByIataNot(String iata, Pageable pageable);
|
||||
|
||||
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
|
||||
Optional<Airport> findByIdAndIata(String id, String iata);
|
||||
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
vie = new Airport("airports::vie", "vie", "loww");
|
||||
vie = airportRepository.save(vie);
|
||||
Airport airport2 = airportRepository.findByIata(Iata.vie);
|
||||
assertNotNull(airport2, "should have found "+vie);
|
||||
assertNotNull(airport2, "should have found " + vie);
|
||||
assertEquals(airport2.getId(), vie.getId());
|
||||
} finally {
|
||||
airportRepository.delete(vie);
|
||||
@@ -310,6 +310,19 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringDeleteTest() throws Exception {
|
||||
Airport airport = new Airport("airports::vie", "vie", "lowx");
|
||||
Airport otherAirport = new Airport("airports::xxx", "xxx", "lxxx");
|
||||
try {
|
||||
airportRepository.save(airport);
|
||||
airportRepository.save(otherAirport);
|
||||
assertEquals(1, airportRepository.deleteByIata("vie").size()); // gets exactly one with no exception
|
||||
} finally {
|
||||
airportRepository.deleteById(otherAirport.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void threadSafeStringParametersTest() throws Exception {
|
||||
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
|
||||
@@ -397,7 +410,6 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis); // so they are executed out-of-order
|
||||
|
||||
Reference in New Issue
Block a user