Skip to content

Commit e101ce4

Browse files
committed
Extend reserved word usage also to JPQL parser.
Closes #3832
1 parent 2ff51be commit e101ce4

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Jpql.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,18 +681,22 @@ subtype
681681

682682
collection_valued_field
683683
: identification_variable
684+
| reserved_word
684685
;
685686

686687
single_valued_object_field
687688
: identification_variable
689+
| reserved_word
688690
;
689691

690692
state_field
691693
: identification_variable
694+
| reserved_word
692695
;
693696

694697
collection_value_field
695698
: identification_variable
699+
| reserved_word
696700
;
697701

698702
entity_name

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ public List<JpaQueryParsingToken> visitLiteral(JpqlParser.LiteralContext ctx) {
21642164
tokens.add(new JpaQueryParsingToken(ctx.INTLITERAL()));
21652165
} else if (ctx.FLOATLITERAL() != null) {
21662166
tokens.add(new JpaQueryParsingToken(ctx.FLOATLITERAL()));
2167-
} else if(ctx.LONGLITERAL() != null) {
2167+
} else if (ctx.LONGLITERAL() != null) {
21682168
tokens.add(new JpaQueryParsingToken(ctx.LONGLITERAL()));
21692169
} else if (ctx.boolean_literal() != null) {
21702170
tokens.addAll(visit(ctx.boolean_literal()));
@@ -2226,7 +2226,7 @@ public List<JpaQueryParsingToken> visitNumeric_literal(JpqlParser.Numeric_litera
22262226
return List.of(new JpaQueryParsingToken(ctx.INTLITERAL()));
22272227
} else if (ctx.FLOATLITERAL() != null) {
22282228
return List.of(new JpaQueryParsingToken(ctx.FLOATLITERAL()));
2229-
} else if(ctx.LONGLITERAL() != null) {
2229+
} else if (ctx.LONGLITERAL() != null) {
22302230
return List.of(new JpaQueryParsingToken(ctx.LONGLITERAL()));
22312231
} else {
22322232
return List.of();
@@ -2275,21 +2275,36 @@ public List<JpaQueryParsingToken> visitSubtype(JpqlParser.SubtypeContext ctx) {
22752275

22762276
@Override
22772277
public List<JpaQueryParsingToken> visitCollection_valued_field(JpqlParser.Collection_valued_fieldContext ctx) {
2278+
if (ctx.reserved_word() != null) {
2279+
return visit(ctx.reserved_word());
2280+
}
22782281
return visit(ctx.identification_variable());
22792282
}
22802283

22812284
@Override
22822285
public List<JpaQueryParsingToken> visitSingle_valued_object_field(JpqlParser.Single_valued_object_fieldContext ctx) {
2286+
2287+
if (ctx.reserved_word() != null) {
2288+
return visit(ctx.reserved_word());
2289+
}
22832290
return visit(ctx.identification_variable());
22842291
}
22852292

22862293
@Override
22872294
public List<JpaQueryParsingToken> visitState_field(JpqlParser.State_fieldContext ctx) {
2295+
2296+
if (ctx.reserved_word() != null) {
2297+
return visit(ctx.reserved_word());
2298+
}
22882299
return visit(ctx.identification_variable());
22892300
}
22902301

22912302
@Override
22922303
public List<JpaQueryParsingToken> visitCollection_value_field(JpqlParser.Collection_value_fieldContext ctx) {
2304+
2305+
if (ctx.reserved_word() != null) {
2306+
return visit(ctx.reserved_word());
2307+
}
22932308
return visit(ctx.identification_variable());
22942309
}
22952310

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlQueryRendererTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,13 @@ void lateralShouldBeAValidParameter() {
10311031
assertQuery("select e from Employee e where e.lateral = :_lateral");
10321032
assertQuery("select te from TestEntity te where te.lateral = :lateral");
10331033
}
1034+
1035+
@Test
1036+
void reservedWordsShouldWork() {
1037+
1038+
assertQuery("select ie from ItemExample ie left join ie.object io where io.externalId = :externalId");
1039+
assertQuery("select ie.object from ItemExample ie left join ie.object io where io.externalId = :externalId");
1040+
assertQuery("select ie from ItemExample ie left join ie.object io where io.object = :externalId");
1041+
assertQuery("select ie from ItemExample ie where ie.status = com.app.domain.object.Status.UP");
1042+
}
10341043
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,4 +1674,13 @@ void entityNameWithPackageContainingReservedWord(String reservedWord) {
16741674
String source = "select new com.company.%s.thing.stuff.ClassName(e.id) from Experience e".formatted(reservedWord);
16751675
assertQuery(source);
16761676
}
1677+
1678+
@Test
1679+
void reservedWordsShouldWork() {
1680+
1681+
assertQuery("select ie from ItemExample ie left join ie.object io where io.externalId = :externalId");
1682+
assertQuery("select ie.object from ItemExample ie left join ie.object io where io.externalId = :externalId");
1683+
assertQuery("select ie from ItemExample ie left join ie.object io where io.object = :externalId");
1684+
assertQuery("select ie from ItemExample ie where ie.status = com.app.domain.object.Status.UP");
1685+
}
16771686
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/JpqlQueryRendererTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,4 +1033,13 @@ void entityNameWithPackageContainingReservedWord(String reservedWord) {
10331033
assertQuery(source);
10341034
}
10351035

1036+
@Test
1037+
void reservedWordsShouldWork() {
1038+
1039+
assertQuery("select ie from ItemExample ie left join ie.object io where io.externalId = :externalId");
1040+
assertQuery("select ie.object from ItemExample ie left join ie.object io where io.externalId = :externalId");
1041+
assertQuery("select ie from ItemExample ie left join ie.object io where io.object = :externalId");
1042+
assertQuery("select ie from ItemExample ie where ie.status = com.app.domain.object.Status.UP");
1043+
}
1044+
10361045
}

0 commit comments

Comments
 (0)