Skip to content

Commit 1334008

Browse files
polyglot-kmp911de
authored andcommitted
Replace recursion in QueryRenderer.isSubquery(…) with loop.
Signed-off-by: KNU-K <[email protected]> Closes #4025
1 parent 73a1632 commit 1334008

File tree

3 files changed

+54
-42
lines changed

3 files changed

+54
-42
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/**
3333
* An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders an EQL query without making any changes.
3434
*
35+
* @author TaeHyun Kang(polyglot-k)
3536
* @author Greg Turnquist
3637
* @author Christoph Strobl
3738
* @author Mark Paluch
@@ -47,15 +48,16 @@ class EqlQueryRenderer extends EqlBaseVisitor<QueryTokenStream> {
4748
*/
4849
static boolean isSubquery(ParserRuleContext ctx) {
4950

50-
if (ctx instanceof EqlParser.SubqueryContext) {
51-
return true;
52-
} else if (ctx instanceof EqlParser.Update_statementContext) {
53-
return false;
54-
} else if (ctx instanceof EqlParser.Delete_statementContext) {
55-
return false;
56-
} else {
57-
return ctx.getParent() != null && isSubquery(ctx.getParent());
51+
while (ctx != null) {
52+
if (ctx instanceof EqlParser.SubqueryContext) {
53+
return true;
54+
}
55+
if (ctx instanceof EqlParser.Update_statementContext || ctx instanceof EqlParser.Delete_statementContext) {
56+
return false;
57+
}
58+
ctx = ctx.getParent();
5859
}
60+
return false;
5961
}
6062

6163
/**
@@ -65,11 +67,13 @@ static boolean isSubquery(ParserRuleContext ctx) {
6567
*/
6668
static boolean isSetQuery(ParserRuleContext ctx) {
6769

68-
if (ctx instanceof EqlParser.Set_fuctionContext) {
69-
return true;
70+
while (ctx != null) {
71+
if (ctx instanceof EqlParser.Set_fuctionContext) {
72+
return true;
73+
}
74+
ctx = ctx.getParent();
7075
}
71-
72-
return ctx.getParent() != null && isSetQuery(ctx.getParent());
76+
return false;
7377
}
7478

7579
@Override

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/**
3333
* An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders an HQL query without making any changes.
3434
*
35+
* @author TaeHyun Kang(polyglot-k)
3536
* @author Greg Turnquist
3637
* @author Christoph Strobl
3738
* @author Oscar Fanchin
@@ -48,19 +49,20 @@ class HqlQueryRenderer extends HqlBaseVisitor<QueryTokenStream> {
4849
*/
4950
static boolean isSubquery(ParserRuleContext ctx) {
5051

51-
if (ctx instanceof HqlParser.SubqueryContext || ctx instanceof HqlParser.CteContext) {
52-
return true;
53-
} else if (ctx instanceof HqlParser.SelectStatementContext) {
54-
return false;
55-
} else if (ctx instanceof HqlParser.InsertStatementContext) {
56-
return false;
57-
} else if (ctx instanceof HqlParser.DeleteStatementContext) {
58-
return false;
59-
} else if (ctx instanceof HqlParser.UpdateStatementContext) {
60-
return false;
61-
} else {
62-
return ctx.getParent() != null && isSubquery(ctx.getParent());
52+
while (ctx != null) {
53+
if (ctx instanceof HqlParser.SubqueryContext || ctx instanceof HqlParser.CteContext) {
54+
return true;
55+
}
56+
if (ctx instanceof HqlParser.SelectStatementContext ||
57+
ctx instanceof HqlParser.InsertStatementContext ||
58+
ctx instanceof HqlParser.DeleteStatementContext ||
59+
ctx instanceof HqlParser.UpdateStatementContext
60+
) {
61+
return false;
62+
}
63+
ctx = ctx.getParent();
6364
}
65+
return false;
6466
}
6567

6668
/**
@@ -70,14 +72,16 @@ static boolean isSubquery(ParserRuleContext ctx) {
7072
*/
7173
static boolean isSetQuery(ParserRuleContext ctx) {
7274

73-
if (ctx instanceof HqlParser.OrderedQueryContext
74-
&& ctx.getParent() instanceof HqlParser.QueryExpressionContext qec) {
75-
if (qec.orderedQuery().indexOf(ctx) != 0) {
76-
return true;
75+
while (ctx != null) {
76+
ParserRuleContext parent = ctx.getParent();
77+
if (ctx instanceof HqlParser.OrderedQueryContext && parent instanceof HqlParser.QueryExpressionContext qec) {
78+
if (qec.orderedQuery().indexOf(ctx) != 0) {
79+
return true;
80+
}
7781
}
82+
ctx = parent;
7883
}
79-
80-
return ctx.getParent() != null && isSetQuery(ctx.getParent());
84+
return false;
8185
}
8286

8387
@Override

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/**
3333
* An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that renders a JPQL query without making any changes.
3434
*
35+
* @author TaeHyun Kang(polyglot-k)
3536
* @author Greg Turnquist
3637
* @author Christoph Strobl
3738
* @author Mark Paluch
@@ -47,15 +48,16 @@ class JpqlQueryRenderer extends JpqlBaseVisitor<QueryTokenStream> {
4748
*/
4849
static boolean isSubquery(ParserRuleContext ctx) {
4950

50-
if (ctx instanceof JpqlParser.SubqueryContext) {
51-
return true;
52-
} else if (ctx instanceof JpqlParser.Update_statementContext) {
53-
return false;
54-
} else if (ctx instanceof JpqlParser.Delete_statementContext) {
55-
return false;
56-
} else {
57-
return ctx.getParent() != null && isSubquery(ctx.getParent());
51+
while (ctx != null) {
52+
if (ctx instanceof JpqlParser.SubqueryContext) {
53+
return true;
54+
}
55+
if (ctx instanceof JpqlParser.Update_statementContext || ctx instanceof JpqlParser.Delete_statementContext) {
56+
return false;
57+
}
58+
ctx = ctx.getParent();
5859
}
60+
return false;
5961
}
6062

6163
/**
@@ -65,11 +67,13 @@ static boolean isSubquery(ParserRuleContext ctx) {
6567
*/
6668
static boolean isSetQuery(ParserRuleContext ctx) {
6769

68-
if (ctx instanceof JpqlParser.Set_fuctionContext) {
69-
return true;
70+
while (ctx != null) {
71+
if (ctx instanceof JpqlParser.Set_fuctionContext) {
72+
return true;
73+
}
74+
ctx = ctx.getParent();
7075
}
71-
72-
return ctx.getParent() != null && isSetQuery(ctx.getParent());
76+
return false;
7377
}
7478

7579
@Override

0 commit comments

Comments
 (0)