Skip to content

Add override of toN1qlRemoveString() to StringQuery. #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ public Flux<RemoveResult> all() {
}

private QueryOptions buildQueryOptions() {
final QueryOptions options = QueryOptions.queryOptions();
if (scanConsistency != null) {
options.scanConsistency(scanConsistency);
}
return options;
return query.buildQueryOptions(scanConsistency);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,24 @@ StringBasedN1qlQueryParser.N1qlSpelValues getN1qlSpelValues(ReactiveCouchbaseTem
* @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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that line does not make sense, since the line above you always set it to non-null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. The code is copied from the Collections branch to reduce the future merge. And in that code 'options' is an argument and needs to be checked.

options = QueryOptions.queryOptions();
}
if (getParameters() != null) {
if (getParameters() instanceof JsonArray) {
options.parameters((JsonArray) getParameters());
} else {
options.parameters((JsonObject) getParameters());
}
}
if (scanConsistency == null
|| scanConsistency == QueryScanConsistency.NOT_BOUNDED && getScanConsistency() != null) {
scanConsistency = getScanConsistency();
}
if (scanConsistency != null) {
options.scanConsistency(scanConsistency);
}

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ public String toN1qlSelectString(ReactiveCouchbaseTemplate template, String coll
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -62,6 +62,22 @@ public String getIcao() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -86,4 +93,8 @@ Long countFancyExpression(@Param("projectIds") List<String> projectIds, @Param("

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Page<Airport> findAllByIataNot(String iata, Pageable pageable);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Optional<Airport> findByIdAndIata(String id, String iata);

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void findByEnum() {
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);
Expand Down Expand Up @@ -310,6 +310,19 @@ void stringQueryTest() throws Exception {
}
}

@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" };
Expand Down Expand Up @@ -397,7 +410,6 @@ void findBySimplePropertyAudited() {
}
}


private void sleep(int millis) {
try {
Thread.sleep(millis); // so they are executed out-of-order
Expand Down