Specific message for string queries that do not project __id and __cas.

com.couchbase.client.core.error.CouchbaseException: query did not project __id.
 Either use #{#n1ql.selectEntity} or project __id and __cas :
 SELECT __cas, * from `b406ab45-ef95-441f-a318-76b8e8af2d76` where iata = $1

Closes #1097.
Original pull request: #1114.

Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
Michael Reiche
2021-04-06 08:03:02 -07:00
committed by GitHub
parent 37587a1a0f
commit 9b848f6254
5 changed files with 59 additions and 8 deletions

View File

@@ -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);

View File

@@ -1,6 +1,6 @@
package org.springframework.data.couchbase.domain;
public enum Iata {
vie,
vie, // must be lower-case to match "vie" as airport.iata is always specified in lowercase
xxx
}

View File

@@ -20,6 +20,7 @@ import static java.util.Arrays.asList;
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;
@@ -34,7 +35,6 @@ import java.util.concurrent.Executors;
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;
@@ -66,7 +66,9 @@ import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
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
@@ -175,11 +177,13 @@ 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);
assertEquals(airport2.getId(), vie.getId());
} finally {
airportRepository.delete(vie);
}
}
@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");
@@ -271,6 +275,19 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
}
}
@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" };
@@ -332,14 +349,15 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
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));
}