Skip to content

Commit e244440

Browse files
committed
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")
1 parent d2456bb commit e244440

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

src/main/java/org/springframework/data/couchbase/core/query/StringQuery.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
import org.springframework.data.couchbase.repository.query.StringBasedN1qlQueryParser;
2222

2323
/**
24-
* @author Michael Reiche
25-
*
2624
* Query created from the string in @Query annotation in the repository interface.
27-
*
28-
* @Query("#{#n1ql.selectEntity} where firstname = $1 and lastname = $2")
29-
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
30-
*
31-
* It must include the SELECT ... FROM ... preferably via the #n1ql expression
32-
* In addition to any predicates in the string, a predicate for the domainType (class)
33-
* will be added.
25+
*
26+
* <pre>
27+
* &#64;Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
28+
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
29+
* </pre>
30+
*
31+
* It must include the SELECT ... FROM ... preferably via the #n1ql expression, in addition to any predicates required,
32+
* including the n1ql.filter (for _class = className)
33+
*
34+
* @author Michael Reiche
3435
*/
3536
public class StringQuery extends Query {
3637

@@ -41,22 +42,19 @@ public StringQuery(String n1qlString) {
4142
}
4243

4344
/**
44-
* inlineN1qlQuery (Query Annotation)
45-
* append the string query to the provided StringBuilder.
46-
* To be used along with the other append*() methods to construct the N1QL statement
47-
* @param sb - StringBuilder
45+
* inlineN1qlQuery (Query Annotation) append the string query to the provided StringBuilder. To be used along with the
46+
* other append*() methods to construct the N1QL statement
47+
*
48+
* @param sb - StringBuilder
4849
*/
4950
private void appendInlineN1qlStatement(final StringBuilder sb) {
5051
sb.append(inlineN1qlQuery);
5152
}
5253

5354
@Override
5455
public String toN1qlString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
55-
StringBasedN1qlQueryParser.N1qlSpelValues n1ql = getN1qlSpelValues(template, domainClass, isCount);
5656
final StringBuilder statement = new StringBuilder();
5757
appendInlineN1qlStatement(statement); // apply the string statement
58-
appendWhereString(statement, n1ql.filter); // typeKey = typeValue
59-
6058
// To use generated parameters for literals
6159
// we need to figure out if we must use positional or named parameters
6260
// If we are using positional parameters, we need to start where

src/test/java/org/springframework/data/couchbase/domain/AirlineRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@Repository
2727
public interface AirlineRepository extends PagingAndSortingRepository<Airline, String> {
2828

29-
@Query("#{#n1ql.selectEntity} where (name = $1)")
29+
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (name = $1)")
3030
List<User> getByName(@Param("airline_name")String airlineName);
3131

3232
}

src/test/java/org/springframework/data/couchbase/domain/UserRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public interface UserRepository extends PagingAndSortingRepository<User, String>
3636

3737
List<User> findByFirstnameAndLastname(String firstname, String lastname);
3838

39-
@Query("#{#n1ql.selectEntity} where firstname = $1 and lastname = $2")
39+
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
4040
List<User> getByFirstnameAndLastname(String firstname, String lastname);
4141

42-
@Query("#{#n1ql.selectEntity} where (firstname = $first or lastname = $last)")
43-
List<User> getByFirstnameOrLastname(@Param("first")String firstname, @Param("last")String lastname);
42+
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (firstname = $first or lastname = $last)")
43+
List<User> getByFirstnameOrLastname(@Param("first") String firstname, @Param("last") String lastname);
4444
}

src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorMockedTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void createsQueryCorrectly() throws Exception {
8787

8888
Query query = creator.createQuery();
8989
assertEquals(
90-
"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\"",
90+
"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",
9191
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
9292
}
9393

@@ -105,7 +105,7 @@ void createsQueryCorrectly2() throws Exception {
105105

106106
Query query = creator.createQuery();
107107
assertEquals(
108-
"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\"",
108+
"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)",
109109
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
110110
}
111111

src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorTests.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests {
6565
CouchbaseTemplate couchbaseTemplate;
6666
static NamedQueries namedQueries = new PropertiesBasedNamedQueries(new Properties());
6767

68-
6968
@BeforeEach
7069
public void beforeEach() {
7170
context = new CouchbaseMappingContext();
@@ -74,7 +73,6 @@ public void beforeEach() {
7473
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
7574
}
7675

77-
7876
@Test
7977
@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
8078
void findUsingStringNq1l() throws Exception {
@@ -86,24 +84,24 @@ void findUsingStringNq1l() throws Exception {
8684
Method method = AirlineRepository.class.getMethod(input, String.class);
8785

8886
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
89-
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
87+
new DefaultRepositoryMetadata(AirlineRepository.class), new SpelAwareProxyProjectionFactory(),
9088
converter.getMappingContext());
9189

92-
93-
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(
94-
getAccessor(getParameters(method), "Continental"), queryMethod, converter, config().bucketname(),
95-
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
90+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Continental"),
91+
queryMethod, converter, config().bucketname(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
9692

9793
Query query = creator.createQuery();
98-
System.out.println(query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
94+
System.out.println(query.toN1qlString(couchbaseTemplate.reactive(), Airline.class, false));
9995

100-
try { Thread.sleep(3000); } catch (Exception e){}
101-
ExecutableFindByQueryOperation.ExecutableFindByQuery q = (ExecutableFindByQueryOperation.ExecutableFindByQuery) couchbaseTemplate.findByQuery(
102-
Airline.class).matching(query);
96+
try {
97+
Thread.sleep(3000);
98+
} catch (Exception e) {}
99+
ExecutableFindByQueryOperation.ExecutableFindByQuery q = (ExecutableFindByQueryOperation.ExecutableFindByQuery) couchbaseTemplate
100+
.findByQuery(Airline.class).matching(query);
103101

104102
Optional<Airline> al = q.one();
105103
assertEquals(airline.toString(), al.get().toString());
106-
}catch(Exception e){
104+
} catch (Exception e) {
107105
e.printStackTrace();
108106
throw e;
109107
} finally {

0 commit comments

Comments
 (0)