Skip to content

Commit

Permalink
Extract JpqlQueryRenderer out of JpqlQueryTransformer.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gregturn committed Mar 11, 2023
1 parent 1fb5580 commit 4e02b10
Show file tree
Hide file tree
Showing 8 changed files with 3,361 additions and 2,292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ delete_statement
;

from_clause
: FROM identification_variable_declaration (',' (identification_variable_declaration | collection_member_declaration))*
: FROM identification_variable_declaration (',' identificationVariableDeclarationOrCollectionMemberDeclaration )*
;

// This parser rule is needed to iterate over these two types from #from_clause
identificationVariableDeclarationOrCollectionMemberDeclaration
: identification_variable_declaration
| collection_member_declaration
;

identification_variable_declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,12 +1207,15 @@ public List<JpaQueryParsingToken> visitSimplePath(HqlParser.SimplePathContext ct
List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.addAll(visit(ctx.identifier()));
NOSPACE(tokens);

ctx.simplePathElement().forEach(simplePathElementContext -> {
tokens.addAll(visit(simplePathElementContext));
NOSPACE(tokens);
});
SPACE(tokens);

return NOSPACE_ALL_BUT_LAST_ELEMENT(tokens, true);
return tokens;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public List<JpaQueryParsingToken> visitOrderedQuery(HqlParser.OrderedQueryContex
NOSPACE(tokens);
tokens.add(TOKEN_CLOSE_PAREN);
}
tokens.add(new JpaQueryParsingToken(order.isDescending() ? "desc" : "asc", false));
tokens.add(order.isDescending() ? TOKEN_DESC : TOKEN_ASC);
tokens.add(TOKEN_COMMA);
});
CLIP(tokens);
Expand Down Expand Up @@ -258,14 +258,6 @@ public List<JpaQueryParsingToken> visitFromRoot(HqlParser.FromRootContext ctx) {
return tokens;
}

@Override
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {

this.hasConstructorExpression = true;

return super.visitInstantiation(ctx);
}

@Override
public List<JpaQueryParsingToken> visitAlias(HqlParser.AliasContext ctx) {

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

return tokens;
}

@Override
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {

this.hasConstructorExpression = true;

return super.visitInstantiation(ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,6 @@ static void NOSPACE(List<JpaQueryParsingToken> tokens) {
}
}

/**
* Take a list of {@link JpaQueryParsingToken}s and convert them ALL to {@code space = false} (except possibly the
* last one).
*
* @param tokens
* @param spacelastElement
*/
static List<JpaQueryParsingToken> NOSPACE_ALL_BUT_LAST_ELEMENT(List<JpaQueryParsingToken> tokens,
boolean spacelastElement) {

List<JpaQueryParsingToken> respacedTokens = tokens.stream() //
.map(queryParsingToken -> {

if (queryParsingToken.space == true) {
return new JpaQueryParsingToken(queryParsingToken.token, false);
} else {
return queryParsingToken;
}
}) //
.collect(Collectors.toList());

if (spacelastElement) {
SPACE(respacedTokens);
}

return respacedTokens;
}

/**
* Drop the last entry from the list of {@link JpaQueryParsingToken}s.
*/
Expand Down
Loading

0 comments on commit 4e02b10

Please sign in to comment.