Skip to content

Fix parsing entity names in JPQL query with package names that contain reserved words #3457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ identification_variable
;

constructor_name
: state_field_path_expression
: entity_name
;

literal
Expand Down Expand Up @@ -720,7 +720,7 @@ collection_value_field
;

entity_name
: identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
: reserved_word ('.' reserved_word)* // Hibernate sometimes expands the entity name to FQDN when using named queries
;

result_variable
Expand Down Expand Up @@ -748,6 +748,90 @@ character_valued_input_parameter
| input_parameter
;

reserved_word
: IDENTIFICATION_VARIABLE
| f=(ABS
|ALL
|AND
|ANY
|AS
|ASC
|AVG
|BETWEEN
|BOTH
|BY
|CASE
|CEILING
|COALESCE
|CONCAT
|COUNT
|CURRENT_DATE
|CURRENT_TIME
|CURRENT_TIMESTAMP
|DATE
|DATETIME
|DELETE
|DESC
|DISTINCT
|END
|ELSE
|EMPTY
|ENTRY
|ESCAPE
|EXISTS
|EXP
|EXTRACT
|FALSE
|FETCH
|FLOOR
|FUNCTION
|IN
|INDEX
|INNER
|IS
|KEY
|LEFT
|LENGTH
|LIKE
|LN
|LOCAL
|LOCATE
|LOWER
|MAX
|MEMBER
|MIN
|MOD
|NEW
|NOT
|NULL
|NULLIF
|OBJECT
|OF
|ON
|OR
|ORDER
|OUTER
|POWER
|ROUND
|SELECT
|SET
|SIGN
|SIZE
|SOME
|SQRT
|SUBSTRING
|SUM
|THEN
|TIME
|TRAILING
|TREAT
|TRIM
|TRUE
|TYPE
|UPDATE
|UPPER
|VALUE)
;
/*
Lexer rules
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ reservedWord
| EXCEPT
| EXCLUDE
| EXISTS
| EXP
| EXTRACT
| FETCH
| FILTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ identification_variable
;

constructor_name
: state_field_path_expression
: entity_name
;

literal
Expand Down Expand Up @@ -696,7 +696,7 @@ collection_value_field
;

entity_name
: identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
: reserved_word ('.' reserved_word)* // Hibernate sometimes expands the entity name to FQDN when using named queries
;

result_variable
Expand Down Expand Up @@ -724,6 +724,90 @@ character_valued_input_parameter
| input_parameter
;

reserved_word
: IDENTIFICATION_VARIABLE
| f=(ABS
|ALL
|AND
|ANY
|AS
|ASC
|AVG
|BETWEEN
|BOTH
|BY
|CASE
|CEILING
|COALESCE
|CONCAT
|COUNT
|CURRENT_DATE
|CURRENT_TIME
|CURRENT_TIMESTAMP
|DATE
|DATETIME
|DELETE
|DESC
|DISTINCT
|END
|ELSE
|EMPTY
|ENTRY
|ESCAPE
|EXISTS
|EXP
|EXTRACT
|FALSE
|FETCH
|FLOOR
|FUNCTION
|IN
|INDEX
|INNER
|IS
|KEY
|LEFT
|LENGTH
|LIKE
|LN
|LOCAL
|LOCATE
|LOWER
|MAX
|MEMBER
|MIN
|MOD
|NEW
|NOT
|NULL
|NULLIF
|OBJECT
|OF
|ON
|OR
|ORDER
|OUTER
|POWER
|ROUND
|SELECT
|SET
|SIGN
|SIZE
|SOME
|SQRT
|SUBSTRING
|SUM
|THEN
|TIME
|TRAILING
|TREAT
|TRIM
|TRUE
|TYPE
|UPDATE
|UPPER
|VALUE)
;
/*
Lexer rules
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ public List<JpaQueryParsingToken> visitConstructor_name(EqlParser.Constructor_na

List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.addAll(visit(ctx.state_field_path_expression()));
tokens.addAll(visit(ctx.entity_name()));
NOSPACE(tokens);

return tokens;
Expand Down Expand Up @@ -2492,8 +2492,8 @@ public List<JpaQueryParsingToken> visitEntity_name(EqlParser.Entity_nameContext

List<JpaQueryParsingToken> tokens = new ArrayList<>();

ctx.identification_variable().forEach(identificationVariableContext -> {
tokens.addAll(visit(identificationVariableContext));
ctx.reserved_word().forEach(identificationVariableContext -> {
tokens.addAll(visitReserved_word(identificationVariableContext));
NOSPACE(tokens);
tokens.add(TOKEN_DOT);
});
Expand Down Expand Up @@ -2543,4 +2543,15 @@ public List<JpaQueryParsingToken> visitCharacter_valued_input_parameter(
return List.of();
}
}

@Override
public List<JpaQueryParsingToken> visitReserved_word(EqlParser.Reserved_wordContext ctx) {
if (ctx.IDENTIFICATION_VARIABLE() != null) {
return List.of(new JpaQueryParsingToken(ctx.IDENTIFICATION_VARIABLE()));
} else if (ctx.f != null) {
return List.of(new JpaQueryParsingToken(ctx.f));
} else {
return List.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.data.jpa.repository.query.JpqlParser.Reserved_wordContext;

/**
* An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders a JPQL query without making any changes.
*
Expand Down Expand Up @@ -2143,7 +2145,7 @@ public List<JpaQueryParsingToken> visitConstructor_name(JpqlParser.Constructor_n

List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.addAll(visit(ctx.state_field_path_expression()));
tokens.addAll(visit(ctx.entity_name()));
NOSPACE(tokens);

return tokens;
Expand Down Expand Up @@ -2296,8 +2298,8 @@ public List<JpaQueryParsingToken> visitEntity_name(JpqlParser.Entity_nameContext

List<JpaQueryParsingToken> tokens = new ArrayList<>();

ctx.identification_variable().forEach(identificationVariableContext -> {
tokens.addAll(visit(identificationVariableContext));
ctx.reserved_word().forEach(ctx2 -> {
tokens.addAll(visitReserved_word(ctx2));
NOSPACE(tokens);
tokens.add(TOKEN_DOT);
});
Expand Down Expand Up @@ -2347,4 +2349,15 @@ public List<JpaQueryParsingToken> visitCharacter_valued_input_parameter(
return List.of();
}
}

@Override
public List<JpaQueryParsingToken> visitReserved_word(Reserved_wordContext ctx) {
if (ctx.IDENTIFICATION_VARIABLE() != null) {
return List.of(new JpaQueryParsingToken(ctx.IDENTIFICATION_VARIABLE()));
} else if (ctx.f != null) {
return List.of(new JpaQueryParsingToken(ctx.f));
} else {
return List.of();
}
}
}
Loading