DATAMONGO-1565 - Polishing.
Formatting in ExpressionEvaluatingParameterBinder and StringBasedMongoQueryUnitTests. Turned Placeholder into value object.
This commit is contained in:
@@ -15,8 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.repository.query;
|
package org.springframework.data.mongodb.repository.query;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Value;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@@ -144,19 +143,24 @@ class ExpressionEvaluatingParameterBinder {
|
|||||||
while (quotationMark != '\'' && quotationMark != '"') {
|
while (quotationMark != '\'' && quotationMark != '"') {
|
||||||
|
|
||||||
quotationMarkIndex--;
|
quotationMarkIndex--;
|
||||||
|
|
||||||
if (quotationMarkIndex < 0) {
|
if (quotationMarkIndex < 0) {
|
||||||
throw new IllegalArgumentException("Could not find opening quotes for quoted parameter");
|
throw new IllegalArgumentException("Could not find opening quotes for quoted parameter");
|
||||||
}
|
}
|
||||||
|
|
||||||
quotationMark = buffer.charAt(quotationMarkIndex);
|
quotationMark = buffer.charAt(quotationMarkIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valueForBinding.startsWith("{")) { // remove quotation char before the complex object string
|
if (valueForBinding.startsWith("{")) { // remove quotation char before the complex object string
|
||||||
|
|
||||||
buffer.deleteCharAt(quotationMarkIndex);
|
buffer.deleteCharAt(quotationMarkIndex);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (quotationMark == '\'') {
|
if (quotationMark == '\'') {
|
||||||
buffer.replace(quotationMarkIndex, quotationMarkIndex + 1, "\"");
|
buffer.replace(quotationMarkIndex, quotationMarkIndex + 1, "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.append("\"");
|
buffer.append("\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -220,7 +224,9 @@ class ExpressionEvaluatingParameterBinder {
|
|||||||
private Pattern createReplacementPattern(List<ParameterBinding> bindings) {
|
private Pattern createReplacementPattern(List<ParameterBinding> bindings) {
|
||||||
|
|
||||||
StringBuilder regex = new StringBuilder();
|
StringBuilder regex = new StringBuilder();
|
||||||
|
|
||||||
for (ParameterBinding binding : bindings) {
|
for (ParameterBinding binding : bindings) {
|
||||||
|
|
||||||
regex.append("|");
|
regex.append("|");
|
||||||
regex.append(Pattern.quote(binding.getParameter()));
|
regex.append(Pattern.quote(binding.getParameter()));
|
||||||
regex.append("['\"]?"); // potential quotation char (as in { foo : '?0' }).
|
regex.append("['\"]?"); // potential quotation char (as in { foo : '?0' }).
|
||||||
@@ -238,10 +244,9 @@ class ExpressionEvaluatingParameterBinder {
|
|||||||
*/
|
*/
|
||||||
private Placeholder extractPlaceholder(String groupName) {
|
private Placeholder extractPlaceholder(String groupName) {
|
||||||
|
|
||||||
if (!groupName.endsWith("'") && !groupName.endsWith("\"")) {
|
return !groupName.endsWith("'") && !groupName.endsWith("\"") ? //
|
||||||
return new Placeholder(groupName, false);
|
Placeholder.of(groupName, false) : //
|
||||||
}
|
Placeholder.of(groupName.substring(0, groupName.length() - 1), true);
|
||||||
return new Placeholder(groupName.substring(0, groupName.length() - 1), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -311,9 +316,11 @@ class ExpressionEvaluatingParameterBinder {
|
|||||||
private static Map<Placeholder, ParameterBinding> mapBindings(List<ParameterBinding> bindings) {
|
private static Map<Placeholder, ParameterBinding> mapBindings(List<ParameterBinding> bindings) {
|
||||||
|
|
||||||
Map<Placeholder, ParameterBinding> map = new LinkedHashMap<Placeholder, ParameterBinding>(bindings.size(), 1);
|
Map<Placeholder, ParameterBinding> map = new LinkedHashMap<Placeholder, ParameterBinding>(bindings.size(), 1);
|
||||||
|
|
||||||
for (ParameterBinding binding : bindings) {
|
for (ParameterBinding binding : bindings) {
|
||||||
map.put(new Placeholder(binding.getParameter(), binding.isQuoted()), binding);
|
map.put(Placeholder.of(binding.getParameter(), binding.isQuoted()), binding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,8 +331,7 @@ class ExpressionEvaluatingParameterBinder {
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @since 1.9
|
* @since 1.9
|
||||||
*/
|
*/
|
||||||
@Data
|
@Value(staticConstructor = "of")
|
||||||
@RequiredArgsConstructor
|
|
||||||
static class Placeholder {
|
static class Placeholder {
|
||||||
|
|
||||||
private final String parameter;
|
private final String parameter;
|
||||||
|
|||||||
@@ -365,6 +365,17 @@ public class StringBasedMongoQueryUnitTests {
|
|||||||
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1454
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldSupportExistsProjection() throws Exception {
|
||||||
|
|
||||||
|
StringBasedMongoQuery mongoQuery = createQueryForMethod("existsByLastname", String.class);
|
||||||
|
|
||||||
|
assertThat(mongoQuery.isExistsQuery(), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-1565
|
* @see DATAMONGO-1565
|
||||||
*/
|
*/
|
||||||
@@ -385,17 +396,6 @@ public class StringBasedMongoQueryUnitTests {
|
|||||||
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @see DATAMONGO-1454
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void shouldSupportExistsProjection() throws Exception {
|
|
||||||
|
|
||||||
StringBasedMongoQuery mongoQuery = createQueryForMethod("existsByLastname", String.class);
|
|
||||||
|
|
||||||
assertThat(mongoQuery.isExistsQuery(), is(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-1565
|
* @see DATAMONGO-1565
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user