Skip to content

DATACOUCH-583 - Nested properties in queries not working. #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public String export() { // used only by tests
*/
private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonValue parameters,
CouchbaseConverter converter) {
String fieldName = maybeQuote(key);
String fieldName = maybeBackTic(key);
int valueLen = value == null ? 0 : value.length;
Object[] v = new Object[valueLen + 2];
v[0] = fieldName;
Expand Down Expand Up @@ -445,8 +445,8 @@ private void addAsArray(JsonArray posValues, Object o, CouchbaseConverter conver
posValues.add(ja);
}

private String maybeQuote(String value) {
if (value == null || (value.startsWith("\"") && value.endsWith("\""))) {
private String maybeBackTic(String value) {
if (value == null || (value.startsWith("`") && value.endsWith("`"))) {
return value;
} else {
return "`" + value + "`";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Iterator;

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

static Converter<? super CouchbasePersistentProperty, String> cvtr = (
source) -> new StringBuilder(source.getName().length() + 2).append('`').append(source.getName()).append('`')
.toString();

@Override
protected QueryCriteria and(final Part part, final QueryCriteria base, final Iterator<Object> iterator) {
if (base == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.springframework.data.couchbase.domain;

import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.data.couchbase.repository.Query;
import org.springframework.data.couchbase.repository.ScanConsistency;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

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

void deleteAll();

}
@ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS)
List<Person> findByAddressStreet(String street);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.domain.PersonRepository;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.util.Capabilities;
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
Expand Down Expand Up @@ -82,6 +85,24 @@ void shouldSaveAndFindAll() {
}
}

@Autowired PersonRepository personRepository;

@Test
void nestedFind() {
Person person = null;
try {
person = new Person(1, "first", "last");
Address address = new Address();
address.setStreet("Maple");
person.setAddress(address);
personRepository.save(person);
List<Person> persons = personRepository.findByAddressStreet("Maple");
assertEquals(1, persons.size());
} finally {
personRepository.deleteById(person.getId().toString());
}
}

// "1\" or name=name or name=\"1")
@Test
void findByInjection() {
Expand Down