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:
@@ -21,16 +21,17 @@ import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.repository.query.StringBasedN1qlQueryParser;
|
||||
|
||||
/**
|
||||
* @author Michael Reiche
|
||||
*
|
||||
* Query created from the string in @Query annotation in the repository interface.
|
||||
*
|
||||
* @Query("#{#n1ql.selectEntity} where firstname = $1 and lastname = $2")
|
||||
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
|
||||
*
|
||||
* It must include the SELECT ... FROM ... preferably via the #n1ql expression
|
||||
* In addition to any predicates in the string, a predicate for the domainType (class)
|
||||
* will be added.
|
||||
*
|
||||
* <pre>
|
||||
* @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
|
||||
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
|
||||
* </pre>
|
||||
*
|
||||
* It must include the SELECT ... FROM ... preferably via the #n1ql expression, in addition to any predicates required,
|
||||
* including the n1ql.filter (for _class = className)
|
||||
*
|
||||
* @author Michael Reiche
|
||||
*/
|
||||
public class StringQuery extends Query {
|
||||
|
||||
@@ -41,10 +42,10 @@ public class StringQuery extends Query {
|
||||
}
|
||||
|
||||
/**
|
||||
* inlineN1qlQuery (Query Annotation)
|
||||
* append the string query to the provided StringBuilder.
|
||||
* To be used along with the other append*() methods to construct the N1QL statement
|
||||
* @param sb - StringBuilder
|
||||
* inlineN1qlQuery (Query Annotation) append the string query to the provided StringBuilder. To be used along with the
|
||||
* other append*() methods to construct the N1QL statement
|
||||
*
|
||||
* @param sb - StringBuilder
|
||||
*/
|
||||
private void appendInlineN1qlStatement(final StringBuilder sb) {
|
||||
sb.append(inlineN1qlQuery);
|
||||
@@ -52,11 +53,8 @@ public class StringQuery extends Query {
|
||||
|
||||
@Override
|
||||
public String toN1qlString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
|
||||
StringBasedN1qlQueryParser.N1qlSpelValues n1ql = getN1qlSpelValues(template, domainClass, isCount);
|
||||
final StringBuilder statement = new StringBuilder();
|
||||
appendInlineN1qlStatement(statement); // apply the string statement
|
||||
appendWhereString(statement, n1ql.filter); // typeKey = typeValue
|
||||
|
||||
// To use generated parameters for literals
|
||||
// we need to figure out if we must use positional or named parameters
|
||||
// If we are using positional parameters, we need to start where
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user