DATAJPA-1519 - Final polishing.
Introduced dedicated JPA SpEL root object to be able to explicitly control the function names exposed.
This commit is contained in:
@@ -32,12 +32,12 @@ public class EscapeCharacter {
|
||||
|
||||
private static final List<String> TO_REPLACE = Arrays.asList("_", "%");
|
||||
|
||||
char value;
|
||||
char escapeCharacter;
|
||||
|
||||
/**
|
||||
* Escapes all special like characters ({@code _}, {@code %}) using the configured escape character.
|
||||
*
|
||||
* @param value May be {@literal null}.
|
||||
* @param value may be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public String escape(String value) {
|
||||
@@ -49,18 +49,9 @@ public class EscapeCharacter {
|
||||
String result = value;
|
||||
|
||||
for (String toReplace : TO_REPLACE) {
|
||||
result = result.replace(toReplace, value + toReplace);
|
||||
result = result.replace(toReplace, escapeCharacter + toReplace);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the underlying character available.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public char escapeCharacter() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,7 +291,7 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extend
|
||||
Expression<String> stringPath = getTypedPath(root, part);
|
||||
Expression<String> propertyExpression = upperIfIgnoreCase(stringPath);
|
||||
Expression<String> parameterExpression = upperIfIgnoreCase(provider.next(part, String.class).getExpression());
|
||||
Predicate like = builder.like(propertyExpression, parameterExpression, escape.getValue());
|
||||
Predicate like = builder.like(propertyExpression, parameterExpression, escape.getEscapeCharacter());
|
||||
return type.equals(NOT_LIKE) || type.equals(NOT_CONTAINING) ? like.not() : like;
|
||||
case TRUE:
|
||||
Expression<Boolean> truePath = getTypedPath(root, part);
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.data.jpa.repository.query.EscapeCharacter;
|
||||
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
|
||||
import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport;
|
||||
@@ -27,7 +29,7 @@ import org.springframework.data.repository.query.spi.EvaluationContextExtensionS
|
||||
*/
|
||||
public class JpaEvaluationContextExtension extends EvaluationContextExtensionSupport {
|
||||
|
||||
private final EscapeCharacter character;
|
||||
private final JpaRootObject root;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaEvaluationContextExtension} for the given escape character.
|
||||
@@ -35,7 +37,7 @@ public class JpaEvaluationContextExtension extends EvaluationContextExtensionSup
|
||||
* @param escapeCharacter the character to be used to escape parameters for LIKE expression.
|
||||
*/
|
||||
public JpaEvaluationContextExtension(char escapeCharacter) {
|
||||
this.character = EscapeCharacter.of(escapeCharacter);
|
||||
this.root = JpaRootObject.of(EscapeCharacter.of(escapeCharacter));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -53,6 +55,32 @@ public class JpaEvaluationContextExtension extends EvaluationContextExtensionSup
|
||||
*/
|
||||
@Override
|
||||
public Object getRootObject() {
|
||||
return character;
|
||||
return root;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public static class JpaRootObject {
|
||||
|
||||
private final EscapeCharacter character;
|
||||
|
||||
/**
|
||||
* Escapes the given source {@link String} for LIKE expressions.
|
||||
*
|
||||
* @param source can be {@literal null}.
|
||||
* @return
|
||||
* @see EscapeCharacter#escape(String)
|
||||
*/
|
||||
public String escape(String source) {
|
||||
return character.escape(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the escape character being used to escape special characters for LIKE expressions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public char escapeCharacter() {
|
||||
return character.getEscapeCharacter();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class JpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the escape character to be used to escape reserved characters for LIKE expressions.
|
||||
* Configures the escape character to be used to escape reserved characters in LIKE expressions.
|
||||
*
|
||||
* @param escapeCharacter
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user