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:
@@ -34,30 +34,20 @@ 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
|
||||
*/
|
||||
@Nullable
|
||||
public String escape(String value) {
|
||||
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return TO_REPLACE.stream() //
|
||||
.reduce(value, (it, character) -> it.replace(character, this.value + character));
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the underlying character available.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public char escapeCharacter() {
|
||||
return value;
|
||||
return value == null //
|
||||
? null //
|
||||
: TO_REPLACE.stream() //
|
||||
.reduce(value, (it, character) -> it.replace(character, this.escapeCharacter + character));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.spel.spi.EvaluationContextExtension;
|
||||
|
||||
@@ -26,7 +28,7 @@ import org.springframework.data.spel.spi.EvaluationContextExtension;
|
||||
*/
|
||||
public class JpaEvaluationContextExtension implements EvaluationContextExtension {
|
||||
|
||||
private final EscapeCharacter character;
|
||||
private final JpaRootObject root;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaEvaluationContextExtension} for the given escape character.
|
||||
@@ -34,7 +36,7 @@ public class JpaEvaluationContextExtension implements EvaluationContextExtension
|
||||
* @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));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -52,6 +54,32 @@ public class JpaEvaluationContextExtension implements EvaluationContextExtension
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user