Handle subqueries when used inside HQL INSERT statements.

Potentialy subqueries can crop up inside INSERT statements. We need to handle this as well.

NOTE: INSERT statements aren't defined in JPQL, so this problem doesn't overlap with standard JPA queries.

See #2977
This commit is contained in:
Greg L. Turnquist
2023-05-24 14:36:46 -05:00
parent 6d56a2525c
commit d6f368dcfb
2 changed files with 15 additions and 0 deletions

View File

@@ -96,6 +96,8 @@ class HqlQueryTransformer extends HqlQueryRenderer {
return true;
} else if (ctx instanceof HqlParser.SelectStatementContext) {
return false;
} else if (ctx instanceof HqlParser.InsertStatementContext) {
return false;
} else {
return isSubquery(ctx.getParent());
}

View File

@@ -973,6 +973,19 @@ class HqlQueryTransformerTests {
.isEqualTo("FROM Story WHERE enabled = true order by created desc");
}
@Test // GH-2977
void isSubqueryThrowsException() {
String query = """
insert into MyEntity (id, col)
select max(id), col
from MyEntityStaging
group by col
""";
assertThat(createQueryFor(query, Sort.unsorted())).isEqualToIgnoringWhitespace(query);
}
private void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
}