Skip to content

Commit 41b24c3

Browse files
committed
Extract HqlQueryRenderer out of HqlQueryTransformer.
Extract JpqlQueryRenderer out of JpqlQueryTransformer. Move all the "boilerplate" operations out of JpqlQueryTransformer and into JpqlQueryRenderer. This visitor simply re-renders the original query provided. JpqlQueryTransform now extends it and ONLY overrides the relevant methods needed to apply sorting, counting, and other things.
1 parent 1fb5580 commit 41b24c3

File tree

8 files changed

+3361
-2292
lines changed

8 files changed

+3361
-2292
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ delete_statement
5454
;
5555

5656
from_clause
57-
: FROM identification_variable_declaration (',' (identification_variable_declaration | collection_member_declaration))*
57+
: FROM identification_variable_declaration (',' identificationVariableDeclarationOrCollectionMemberDeclaration )*
58+
;
59+
60+
// This parser rule is needed to iterate over these two types from #from_clause
61+
identificationVariableDeclarationOrCollectionMemberDeclaration
62+
: identification_variable_declaration
63+
| collection_member_declaration
5864
;
5965

6066
identification_variable_declaration

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1207,12 +1207,15 @@ public List<JpaQueryParsingToken> visitSimplePath(HqlParser.SimplePathContext ct
12071207
List<JpaQueryParsingToken> tokens = new ArrayList<>();
12081208

12091209
tokens.addAll(visit(ctx.identifier()));
1210+
NOSPACE(tokens);
12101211

12111212
ctx.simplePathElement().forEach(simplePathElementContext -> {
12121213
tokens.addAll(visit(simplePathElementContext));
1214+
NOSPACE(tokens);
12131215
});
1216+
SPACE(tokens);
12141217

1215-
return NOSPACE_ALL_BUT_LAST_ELEMENT(tokens, true);
1218+
return tokens;
12161219
}
12171220

12181221
@Override

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public List<JpaQueryParsingToken> visitOrderedQuery(HqlParser.OrderedQueryContex
142142
NOSPACE(tokens);
143143
tokens.add(TOKEN_CLOSE_PAREN);
144144
}
145-
tokens.add(new JpaQueryParsingToken(order.isDescending() ? "desc" : "asc", false));
145+
tokens.add(order.isDescending() ? TOKEN_DESC : TOKEN_ASC);
146146
tokens.add(TOKEN_COMMA);
147147
});
148148
CLIP(tokens);
@@ -258,14 +258,6 @@ public List<JpaQueryParsingToken> visitFromRoot(HqlParser.FromRootContext ctx) {
258258
return tokens;
259259
}
260260

261-
@Override
262-
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {
263-
264-
this.hasConstructorExpression = true;
265-
266-
return super.visitInstantiation(ctx);
267-
}
268-
269261
@Override
270262
public List<JpaQueryParsingToken> visitAlias(HqlParser.AliasContext ctx) {
271263

@@ -335,4 +327,12 @@ public List<JpaQueryParsingToken> visitSelectClause(HqlParser.SelectClauseContex
335327

336328
return tokens;
337329
}
330+
331+
@Override
332+
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {
333+
334+
this.hasConstructorExpression = true;
335+
336+
return super.visitInstantiation(ctx);
337+
}
338338
}

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

-28
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,6 @@ static void NOSPACE(List<JpaQueryParsingToken> tokens) {
144144
}
145145
}
146146

147-
/**
148-
* Take a list of {@link JpaQueryParsingToken}s and convert them ALL to {@code space = false} (except possibly the
149-
* last one).
150-
*
151-
* @param tokens
152-
* @param spacelastElement
153-
*/
154-
static List<JpaQueryParsingToken> NOSPACE_ALL_BUT_LAST_ELEMENT(List<JpaQueryParsingToken> tokens,
155-
boolean spacelastElement) {
156-
157-
List<JpaQueryParsingToken> respacedTokens = tokens.stream() //
158-
.map(queryParsingToken -> {
159-
160-
if (queryParsingToken.space == true) {
161-
return new JpaQueryParsingToken(queryParsingToken.token, false);
162-
} else {
163-
return queryParsingToken;
164-
}
165-
}) //
166-
.collect(Collectors.toList());
167-
168-
if (spacelastElement) {
169-
SPACE(respacedTokens);
170-
}
171-
172-
return respacedTokens;
173-
}
174-
175147
/**
176148
* Drop the last entry from the list of {@link JpaQueryParsingToken}s.
177149
*/

0 commit comments

Comments
 (0)