Skip to content

Commit d383d08

Browse files
committed
DATACOUCH-583 - Nested properties in queries not working. (#270)
The complete property path was being quoted instead of the individual components. ie. `address.street` instead of `address`.`street` Also fixed another issue - changed the maybeQuote() method for property names to correctly look for back-tics instead of double quotes and changed the name accordingly. Co-authored-by: mikereiche <[email protected]>
1 parent e4068b9 commit d383d08

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public String export() { // used only by tests
343343
*/
344344
private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonValue parameters,
345345
CouchbaseConverter converter) {
346-
String fieldName = maybeQuote(key);
346+
String fieldName = maybeBackTic(key);
347347
int valueLen = value == null ? 0 : value.length;
348348
Object[] v = new Object[valueLen + 2];
349349
v[0] = fieldName;
@@ -445,8 +445,8 @@ private void addAsArray(JsonArray posValues, Object o, CouchbaseConverter conver
445445
posValues.add(ja);
446446
}
447447

448-
private String maybeQuote(String value) {
449-
if (value == null || (value.startsWith("\"") && value.endsWith("\""))) {
448+
private String maybeBackTic(String value) {
449+
if (value == null || (value.startsWith("`") && value.endsWith("`"))) {
450450
return value;
451451
} else {
452452
return "`" + value + "`";

src/main/java/org/springframework/data/couchbase/repository/query/N1qlQueryCreator.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Iterator;
2121

22+
import org.springframework.core.convert.converter.Converter;
2223
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
2324
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
2425
import org.springframework.data.couchbase.core.query.Query;
@@ -56,9 +57,13 @@ public N1qlQueryCreator(final PartTree tree, final ParameterAccessor accessor, Q
5657
protected QueryCriteria create(final Part part, final Iterator<Object> iterator) {
5758
PersistentPropertyPath<CouchbasePersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
5859
CouchbasePersistentProperty property = path.getLeafProperty();
59-
return from(part, property, where(path.toDotPath()), iterator);
60+
return from(part, property, where(path.toDotPath(cvtr)), iterator);
6061
}
6162

63+
static Converter<? super CouchbasePersistentProperty, String> cvtr = (
64+
source) -> new StringBuilder(source.getName().length() + 2).append('`').append(source.getName()).append('`')
65+
.toString();
66+
6267
@Override
6368
protected QueryCriteria and(final Part part, final QueryCriteria base, final Iterator<Object> iterator) {
6469
if (base == null) {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.couchbase.domain;
1717

18+
import com.couchbase.client.java.query.QueryScanConsistency;
1819
import org.springframework.data.couchbase.repository.Query;
20+
import org.springframework.data.couchbase.repository.ScanConsistency;
1921
import org.springframework.data.repository.CrudRepository;
2022
import org.springframework.data.repository.query.Param;
2123

@@ -105,4 +107,6 @@ public interface PersonRepository extends CrudRepository<Person, String> {
105107

106108
void deleteAll();
107109

108-
}
110+
@ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS)
111+
List<Person> findByAddressStreet(String street);
112+
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.data.couchbase.CouchbaseClientFactory;
3535
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
36+
import org.springframework.data.couchbase.domain.Address;
3637
import org.springframework.data.couchbase.domain.Airport;
3738
import org.springframework.data.couchbase.domain.AirportRepository;
39+
import org.springframework.data.couchbase.domain.Person;
40+
import org.springframework.data.couchbase.domain.PersonRepository;
3841
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
3942
import org.springframework.data.couchbase.util.Capabilities;
4043
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
@@ -82,6 +85,24 @@ void shouldSaveAndFindAll() {
8285
}
8386
}
8487

88+
@Autowired PersonRepository personRepository;
89+
90+
@Test
91+
void nestedFind() {
92+
Person person = null;
93+
try {
94+
person = new Person(1, "first", "last");
95+
Address address = new Address();
96+
address.setStreet("Maple");
97+
person.setAddress(address);
98+
personRepository.save(person);
99+
List<Person> persons = personRepository.findByAddressStreet("Maple");
100+
assertEquals(1, persons.size());
101+
} finally {
102+
personRepository.deleteById(person.getId().toString());
103+
}
104+
}
105+
85106
// "1\" or name=name or name=\"1")
86107
@Test
87108
void findByInjection() {

0 commit comments

Comments
 (0)