diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java index 230da53fed..2a19abc3c5 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,6 +32,7 @@ /** * An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders an EQL query without making any changes. * + * @author TaeHyun Kang(polyglot-k) * @author Greg Turnquist * @author Christoph Strobl * @author Mark Paluch @@ -47,15 +48,16 @@ class EqlQueryRenderer extends EqlBaseVisitor { */ static boolean isSubquery(ParserRuleContext ctx) { - if (ctx instanceof EqlParser.SubqueryContext) { - return true; - } else if (ctx instanceof EqlParser.Update_statementContext) { - return false; - } else if (ctx instanceof EqlParser.Delete_statementContext) { - return false; - } else { - return ctx.getParent() != null && isSubquery(ctx.getParent()); + while (ctx != null) { + if (ctx instanceof EqlParser.SubqueryContext) { + return true; + } + if (ctx instanceof EqlParser.Update_statementContext || ctx instanceof EqlParser.Delete_statementContext) { + return false; + } + ctx = ctx.getParent(); } + return false; } /** @@ -65,11 +67,13 @@ static boolean isSubquery(ParserRuleContext ctx) { */ static boolean isSetQuery(ParserRuleContext ctx) { - if (ctx instanceof EqlParser.Set_fuctionContext) { - return true; + while (ctx != null) { + if (ctx instanceof EqlParser.Set_fuctionContext) { + return true; + } + ctx = ctx.getParent(); } - - return ctx.getParent() != null && isSetQuery(ctx.getParent()); + return false; } @Override diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryRenderer.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryRenderer.java index 15522f0263..e93f34e584 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryRenderer.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryRenderer.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,6 +32,7 @@ /** * An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders an HQL query without making any changes. * + * @author TaeHyun Kang(polyglot-k) * @author Greg Turnquist * @author Christoph Strobl * @author Oscar Fanchin @@ -48,19 +49,20 @@ class HqlQueryRenderer extends HqlBaseVisitor { */ static boolean isSubquery(ParserRuleContext ctx) { - if (ctx instanceof HqlParser.SubqueryContext || ctx instanceof HqlParser.CteContext) { - return true; - } else if (ctx instanceof HqlParser.SelectStatementContext) { - return false; - } else if (ctx instanceof HqlParser.InsertStatementContext) { - return false; - } else if (ctx instanceof HqlParser.DeleteStatementContext) { - return false; - } else if (ctx instanceof HqlParser.UpdateStatementContext) { - return false; - } else { - return ctx.getParent() != null && isSubquery(ctx.getParent()); + while (ctx != null) { + if (ctx instanceof HqlParser.SubqueryContext || ctx instanceof HqlParser.CteContext) { + return true; + } + if (ctx instanceof HqlParser.SelectStatementContext || + ctx instanceof HqlParser.InsertStatementContext || + ctx instanceof HqlParser.DeleteStatementContext || + ctx instanceof HqlParser.UpdateStatementContext + ) { + return false; + } + ctx = ctx.getParent(); } + return false; } /** @@ -70,14 +72,16 @@ static boolean isSubquery(ParserRuleContext ctx) { */ static boolean isSetQuery(ParserRuleContext ctx) { - if (ctx instanceof HqlParser.OrderedQueryContext - && ctx.getParent() instanceof HqlParser.QueryExpressionContext qec) { - if (qec.orderedQuery().indexOf(ctx) != 0) { - return true; + while (ctx != null) { + ParserRuleContext parent = ctx.getParent(); + if (ctx instanceof HqlParser.OrderedQueryContext && parent instanceof HqlParser.QueryExpressionContext qec) { + if (qec.orderedQuery().indexOf(ctx) != 0) { + return true; + } } + ctx = parent; } - - return ctx.getParent() != null && isSetQuery(ctx.getParent()); + return false; } @Override diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java index 3e3c39fa19..e768260fc5 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,6 +32,7 @@ /** * An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders a JPQL query without making any changes. * + * @author TaeHyun Kang(polyglot-k) * @author Greg Turnquist * @author Christoph Strobl * @author Mark Paluch @@ -47,15 +48,16 @@ class JpqlQueryRenderer extends JpqlBaseVisitor { */ static boolean isSubquery(ParserRuleContext ctx) { - if (ctx instanceof JpqlParser.SubqueryContext) { - return true; - } else if (ctx instanceof JpqlParser.Update_statementContext) { - return false; - } else if (ctx instanceof JpqlParser.Delete_statementContext) { - return false; - } else { - return ctx.getParent() != null && isSubquery(ctx.getParent()); + while (ctx != null) { + if (ctx instanceof JpqlParser.SubqueryContext) { + return true; + } + if (ctx instanceof JpqlParser.Update_statementContext || ctx instanceof JpqlParser.Delete_statementContext) { + return false; + } + ctx = ctx.getParent(); } + return false; } /** @@ -65,11 +67,13 @@ static boolean isSubquery(ParserRuleContext ctx) { */ static boolean isSetQuery(ParserRuleContext ctx) { - if (ctx instanceof JpqlParser.Set_fuctionContext) { - return true; + while (ctx != null) { + if (ctx instanceof JpqlParser.Set_fuctionContext) { + return true; + } + ctx = ctx.getParent(); } - - return ctx.getParent() != null && isSetQuery(ctx.getParent()); + return false; } @Override