Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import reactor.core.publisher.Mono;

import org.springframework.data.couchbase.core.query.AnalyticsQuery;
import org.springframework.data.couchbase.core.support.TemplateUtils;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.java.analytics.AnalyticsOptions;
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
import com.couchbase.client.java.analytics.ReactiveAnalyticsResult;
Expand Down Expand Up @@ -92,10 +94,22 @@ public Flux<T> all() {
return throwable;
}
}).flatMapMany(ReactiveAnalyticsResult::rowsAsObject).map(row -> {
String id = row.getString("__id");
long cas = row.getLong("__cas");
row.removeKey("__id");
row.removeKey("__cas");
String id = "";
long cas = 0;
if (row.getString(TemplateUtils.SELECT_ID) == null) {
throw new CouchbaseException("analytics query did not project " + TemplateUtils.SELECT_ID
+ ". Either use #{#n1ql.selectEntity} or project " + TemplateUtils.SELECT_ID + " and "
+ TemplateUtils.SELECT_CAS + " : " + statement);
}
id = row.getString(TemplateUtils.SELECT_ID);
if (row.getLong(TemplateUtils.SELECT_CAS) == null) {
throw new CouchbaseException("analytics query did not project " + TemplateUtils.SELECT_CAS
+ ". Either use #{#n1ql.selectEntity} or project " + TemplateUtils.SELECT_ID + " and "
+ TemplateUtils.SELECT_CAS + " : " + statement);
}
cas = row.getLong(TemplateUtils.SELECT_CAS);
row.removeKey(TemplateUtils.SELECT_ID);
row.removeKey(TemplateUtils.SELECT_CAS);
return template.support().decodeEntity(id, row.toString(), cas, domainType);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.couchbase.core.support.TemplateUtils;
import org.springframework.util.Assert;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.java.query.QueryScanConsistency;
import com.couchbase.client.java.query.ReactiveQueryResult;

Expand Down Expand Up @@ -147,7 +148,17 @@ public Flux<T> all() {
String id = "";
long cas = 0;
if (distinctFields == null) {
if (row.getString(TemplateUtils.SELECT_ID) == null) {
throw new CouchbaseException(
"query did not project " + TemplateUtils.SELECT_ID + ". Either use #{#n1ql.selectEntity} or project "
+ TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + " : " + statement);
}
id = row.getString(TemplateUtils.SELECT_ID);
if (row.getLong(TemplateUtils.SELECT_CAS) == null) {
throw new CouchbaseException(
"query did not project " + TemplateUtils.SELECT_CAS + ". Either use #{#n1ql.selectEntity} or project "
+ TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + " : " + statement);
}
cas = row.getLong(TemplateUtils.SELECT_CAS);
row.removeKey(TemplateUtils.SELECT_ID);
row.removeKey(TemplateUtils.SELECT_CAS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> getAllByIata(String iata);

@Query("SELECT __cas, * from `#{#n1ql.bucket}` where iata = $1")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> getAllByIataNoID(String iata);

@Query("SELECT __id, * from `#{#n1ql.bucket}` where iata = $1")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> getAllByIataNoCAS(String iata);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
long countByIataIn(String... iata);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.springframework.data.couchbase.domain;
Copy link
Contributor

Choose a reason for hiding this comment

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

needs a license header?


public enum Iata {
vie,
vie, // must be lower-case to match "vie" as airport.iata is always specified in lowercase
xxx
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -34,7 +35,6 @@
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import com.couchbase.client.java.query.QueryScanConsistency;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -66,7 +66,9 @@
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.error.IndexExistsException;
import com.couchbase.client.java.query.QueryScanConsistency;

/**
* Repository tests
Expand Down Expand Up @@ -175,11 +177,13 @@ 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);
assertEquals(airport2.getId(), vie.getId());
} finally {
airportRepository.delete(vie);
}
}

@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");
Expand Down Expand Up @@ -271,6 +275,19 @@ void threadSafeParametersTest() throws Exception {
}
}

@Test
void stringQueryTest() throws Exception {
Airport airport = new Airport("airports::vie", "vie", "lowx");
try {
airportRepository.save(airport);
airportRepository.getAllByIata("vie").get(0); // gets at least one with no exception
assertThrows(CouchbaseException.class, () -> airportRepository.getAllByIataNoID("vie"));
assertThrows(CouchbaseException.class, () -> airportRepository.getAllByIataNoCAS("vie"));
} finally {
airportRepository.deleteById(airport.getId());
}
}

@Test
void threadSafeStringParametersTest() throws Exception {
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
Expand Down Expand Up @@ -332,14 +349,15 @@ void deleteAllById() {
void couchbaseRepositoryQuery() throws Exception {
User user = new User("1", "Dave", "Wilson");
userRepository.save(user);
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(QueryCriteria.where("firstname").is("Dave").and("`1`").is("`1`")).all();
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
.matching(QueryCriteria.where("firstname").is("Dave").and("`1`").is("`1`")).all();
String input = "findByFirstname";
Method method = UserRepository.class.getMethod(input, String.class);
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
couchbaseTemplate.getConverter().getMappingContext());
CouchbaseRepositoryQuery query = new CouchbaseRepositoryQuery(couchbaseTemplate, queryMethod, null);
List<User> users = (List<User>)query.execute(new String[] { "Dave" });
List<User> users = (List<User>) query.execute(new String[] { "Dave" });
assertEquals(user, users.get(0));
}

Expand Down