Skip to content

Commit

Permalink
Resolve handling of ESCAPE clause with LIKE queries on Hibernate.
Browse files Browse the repository at this point in the history
The HQL parser has to handle parameters in addition to character values in order to support SpEL.

Closes #2954
Original Pull Request: #2956.
  • Loading branch information
gregturn authored and mp911de committed May 31, 2023
1 parent e0cfe41 commit 38ced42
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ dealingWithNullExpression

// https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#hql-like-predicate
stringPatternMatching
: expression NOT? (LIKE | ILIKE) expression (ESCAPE character)?
: expression NOT? (LIKE | ILIKE) expression (ESCAPE expression)?
;

// https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#hql-elements-indices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ public List<JpaQueryParsingToken> visitStringPatternMatching(HqlParser.StringPat
if (ctx.ESCAPE() != null) {

tokens.add(new JpaQueryParsingToken(ctx.ESCAPE()));
tokens.addAll(visit(ctx.character()));
tokens.addAll(visit(ctx.expression(2)));
}

return tokens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,12 @@ public List<JpaQueryParsingToken> visitLike_expression(JpqlParser.Like_expressio
tokens.add(new JpaQueryParsingToken(ctx.LIKE()));
tokens.addAll(visit(ctx.pattern_value()));

if (ctx.ESCAPE() != null) {

tokens.add(new JpaQueryParsingToken(ctx.ESCAPE()));
tokens.addAll(visit(ctx.escape_character()));
}

return tokens;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@ void executesNotInQueryCorrectly() {}
@Disabled
@Override
void executesInKeywordForPageCorrectly() {}

@Disabled("Can't get ESCAPE clause working with EclipseLink. See #2955")
@Override
void escapingInLikeSpels() {
super.escapingInLikeSpels();
}

@Disabled("Can't get ESCAPE clause working with EclipseLink. See #2955")
@Override
void escapingInLikeSpelsInThePresenceOfEscapeCharacters() {
super.escapingInLikeSpelsInThePresenceOfEscapeCharacters();
}

@Disabled("Can't get ESCAPE clause working with EclipseLink. See #2955")
@Override
void escapingInLikeSpelsInThePresenceOfEscapedWildcards() {
super.escapingInLikeSpelsInThePresenceOfEscapedWildcards();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -235,7 +234,6 @@ void parametersForContainsGetProperlyEscaped() {
.isEmpty();
}

@Disabled("Can't get ESCAPE clause working with Hibernate")
@Test // DATAJPA-1519
void escapingInLikeSpels() {

Expand All @@ -246,7 +244,6 @@ void escapingInLikeSpels() {
assertThat(userRepository.findContainingEscaped("att_")).containsExactly(extra);
}

@Disabled("Can't get ESCAPE clause working with Hibernate")
@Test // DATAJPA-1522
void escapingInLikeSpelsInThePresenceOfEscapeCharacters() {

Expand All @@ -256,7 +253,6 @@ void escapingInLikeSpelsInThePresenceOfEscapeCharacters() {
assertThat(userRepository.findContainingEscaped("att\\x")).containsExactly(withEscapeCharacter);
}

@Disabled("Can't get ESCAPE clause working with Hibernate")
@Test // DATAJPA-1522
void escapingInLikeSpelsInThePresenceOfEscapedWildcards() {

Expand Down Expand Up @@ -288,8 +284,7 @@ void executesQueryWithProjectionContainingReferenceToPluralAttribute() {

List<RolesAndFirstname> rolesAndFirstnameBy = userRepository.findRolesAndFirstnameBy();

assertThat(rolesAndFirstnameBy)
.isNotNull();
assertThat(rolesAndFirstnameBy).isNotNull();

for (RolesAndFirstname rolesAndFirstname : rolesAndFirstnameBy) {
assertThat(rolesAndFirstname.getFirstname()).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntity
List<NameOnlyDto> findByNamedQueryWithConstructorExpression();

// DATAJPA-1519
@Query("select u from User u where u.lastname like '%?#{escape([0])}%' escape ?#{escapeCharacter()}")
@Query("select u from User u where u.lastname like %?#{escape([0])}% escape ?#{escapeCharacter()}")
List<User> findContainingEscaped(String namePart);

// DATAJPA-1303
Expand Down

0 comments on commit 38ced42

Please sign in to comment.