DATACOUCH-580 - Do not automatically insert n1ql.filter predicate in string query.

Since it is difficult to determine where the n1ql.filter (which typically
filters on _class), and also because it is not mandator, including
the n1ql.filter in the @query is left up to the author of the query.
Example:
@query("#{#n1ql.selectEntity} where #{#n1ql.filter} and lastname = $1")
This commit is contained in:
mikereiche
2020-07-14 14:48:09 -07:00
parent d2456bb166
commit e2444403a7
5 changed files with 30 additions and 34 deletions

View File

@@ -26,7 +26,7 @@ import java.util.List;
@Repository
public interface AirlineRepository extends PagingAndSortingRepository<Airline, String> {
@Query("#{#n1ql.selectEntity} where (name = $1)")
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (name = $1)")
List<User> getByName(@Param("airline_name")String airlineName);
}

View File

@@ -36,9 +36,9 @@ public interface UserRepository extends PagingAndSortingRepository<User, String>
List<User> findByFirstnameAndLastname(String firstname, String lastname);
@Query("#{#n1ql.selectEntity} where firstname = $1 and lastname = $2")
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
List<User> getByFirstnameAndLastname(String firstname, String lastname);
@Query("#{#n1ql.selectEntity} where (firstname = $first or lastname = $last)")
List<User> getByFirstnameOrLastname(@Param("first")String firstname, @Param("last")String lastname);
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (firstname = $first or lastname = $last)")
List<User> getByFirstnameOrLastname(@Param("first") String firstname, @Param("last") String lastname);
}

View File

@@ -87,7 +87,7 @@ class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
Query query = creator.createQuery();
assertEquals(
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where firstname = $1 and lastname = $2 AND `_class` = \"org.springframework.data.couchbase.domain.User\"",
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and firstname = $1 and lastname = $2",
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
}
@@ -105,7 +105,7 @@ class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
Query query = creator.createQuery();
assertEquals(
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where (firstname = $first or lastname = $last) AND `_class` = \"org.springframework.data.couchbase.domain.User\"",
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and (firstname = $first or lastname = $last)",
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
}

View File

@@ -65,7 +65,6 @@ class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests {
CouchbaseTemplate couchbaseTemplate;
static NamedQueries namedQueries = new PropertiesBasedNamedQueries(new Properties());
@BeforeEach
public void beforeEach() {
context = new CouchbaseMappingContext();
@@ -74,7 +73,6 @@ class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests {
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
}
@Test
@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
void findUsingStringNq1l() throws Exception {
@@ -86,24 +84,24 @@ class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests {
Method method = AirlineRepository.class.getMethod(input, String.class);
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
new DefaultRepositoryMetadata(AirlineRepository.class), new SpelAwareProxyProjectionFactory(),
converter.getMappingContext());
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(
getAccessor(getParameters(method), "Continental"), queryMethod, converter, config().bucketname(),
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Continental"),
queryMethod, converter, config().bucketname(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
Query query = creator.createQuery();
System.out.println(query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
System.out.println(query.toN1qlString(couchbaseTemplate.reactive(), Airline.class, false));
try { Thread.sleep(3000); } catch (Exception e){}
ExecutableFindByQueryOperation.ExecutableFindByQuery q = (ExecutableFindByQueryOperation.ExecutableFindByQuery) couchbaseTemplate.findByQuery(
Airline.class).matching(query);
try {
Thread.sleep(3000);
} catch (Exception e) {}
ExecutableFindByQueryOperation.ExecutableFindByQuery q = (ExecutableFindByQueryOperation.ExecutableFindByQuery) couchbaseTemplate
.findByQuery(Airline.class).matching(query);
Optional<Airline> al = q.one();
assertEquals(airline.toString(), al.get().toString());
}catch(Exception e){
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {