DATAJPA-461 - Improve binding detection.

Improved the regular expression to detect binding detection to safely find as-is bindings.
This commit is contained in:
Oliver Gierke
2014-02-11 18:14:31 +01:00
parent cd26a36a7b
commit 5d1f41ed62
2 changed files with 26 additions and 8 deletions

View File

@@ -153,9 +153,9 @@ class StringQuery {
}
StringBuilder builder = new StringBuilder();
builder.append("(?<=(");
builder.append("(");
builder.append(StringUtils.collectionToDelimitedString(keywords, "|")); // keywords
builder.append("))");
builder.append(")?");
builder.append("(?: )?"); // some whitespace
builder.append("(");
builder.append("%?(\\?(\\d+))%?"); // position parameter
@@ -184,8 +184,9 @@ class StringQuery {
String parameterIndexString = matcher.group(4);
String parameterName = parameterIndexString != null ? null : matcher.group(6);
Integer parameterIndex = parameterIndexString == null ? null : Integer.valueOf(parameterIndexString);
String typeSource = matcher.group(1);
switch (ParameterBindingType.of(matcher.group(1))) {
switch (ParameterBindingType.of(typeSource)) {
case LIKE:
@@ -246,7 +247,7 @@ class StringQuery {
// Trailing whitespace is intentional to reflect that the keywords must be used with at least one whitespace
// character, while = does not.
LIKE("like "), IN("in "), AS_IS("=");
LIKE("like "), IN("in "), AS_IS(null);
private final String keyword;
@@ -268,18 +269,22 @@ class StringQuery {
* Return the appropriate {@link ParameterBindingType} for the given {@link String}. Returns {@keyword
* #AS_IS} in case no other {@link ParameterBindingType} could be found.
*
* @param parameterBindingKindName
* @param typeSource
* @return
*/
static ParameterBindingType of(String parameterBindingKindName) {
static ParameterBindingType of(String typeSource) {
if (!StringUtils.hasText(typeSource)) {
return AS_IS;
}
for (ParameterBindingType type : values()) {
if (type.name().equalsIgnoreCase(parameterBindingKindName.trim())) {
if (type.name().equalsIgnoreCase(typeSource.trim())) {
return type;
}
}
return AS_IS;
throw new IllegalArgumentException(String.format("Unsupported parameter binding type %s!", typeSource));
}
}
}

View File

@@ -181,6 +181,19 @@ public class StringQueryUnitTests {
new StringQuery("select u from User u where u.firstname like %?1 and u.lastname like ?1%");
}
/**
* @see DATAJPA-461
*/
@Test
public void treatsGreaterThanBindingAsSimpleBinding() {
StringQuery query = new StringQuery("select u from User u where u.createdDate > ?1");
List<ParameterBinding> bindings = query.getParameterBindings();
assertThat(bindings, hasSize(1));
assertPositionalBinding(ParameterBinding.class, 1, bindings.get(0));
}
private void assertPositionalBinding(Class<? extends ParameterBinding> bindingType, Integer position,
ParameterBinding expectedBinding) {