Properly handle reserved words as entity names.

In JPQL and HQL, we need to properly handle reserved words that crop up as entity names (which is legal).

See #2982.
This commit is contained in:
Greg L. Turnquist
2023-05-26 14:35:36 -05:00
parent ad7b28063a
commit 0f8279bbfa
5 changed files with 43 additions and 12 deletions

View File

@@ -713,6 +713,7 @@ reservedWord
| FETCH
| FILTER
| FIRST
| FLOOR
| FOLLOWING
| FOR
| FORMAT

View File

@@ -596,12 +596,13 @@ trim_character
identification_variable
: IDENTIFICATION_VARIABLE
| ORDER // Gap in the spec requires supporting 'Order' as an entity name
| COUNT // Gap in the spec requires supporting 'count' as a possible name
| KEY // Gap in the sepc requires supported 'key' as a possible name
| LEFT
| f=(COUNT
| INNER
| KEY
| LEFT
| ORDER
| OUTER
| FLOOR)
;
constructor_name
@@ -682,8 +683,7 @@ collection_value_field
;
entity_name
: identification_variable
| identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
: identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
;
result_variable

View File

@@ -2118,12 +2118,8 @@ class JpqlQueryRenderer extends JpqlBaseVisitor<List<JpaQueryParsingToken>> {
if (ctx.IDENTIFICATION_VARIABLE() != null) {
return List.of(new JpaQueryParsingToken(ctx.IDENTIFICATION_VARIABLE()));
} else if (ctx.COUNT() != null) {
return List.of(new JpaQueryParsingToken(ctx.COUNT()));
} else if (ctx.ORDER() != null) {
return List.of(new JpaQueryParsingToken(ctx.ORDER()));
} else if (ctx.KEY() != null) {
return List.of(new JpaQueryParsingToken(ctx.KEY()));
} else if (ctx.f != null) {
return List.of(new JpaQueryParsingToken(ctx.f));
} else {
return List.of();
}

View File

@@ -1499,4 +1499,21 @@ class HqlQueryRendererTests {
select sr from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId
""");
}
@Test // GH-2982
void floorShouldBeValidEntityName() {
assertQuery("""
SELECT f
FROM Floor f
WHERE f.name = :name
""");
assertQuery("""
SELECT r
FROM Room r
JOIN r.floor f
WHERE f.name = :name
""");
}
}

View File

@@ -914,4 +914,21 @@ class JpqlQueryRendererTests {
WHERE l.product.name = ?1
""");
}
@Test // GH-2982
void floorShouldBeValidEntityName() {
assertQuery("""
SELECT f
FROM Floor f
WHERE f.name = :name
""");
assertQuery("""
SELECT r
FROM Room r
JOIN r.floor f
WHERE f.name = :name
""");
}
}