From 4d1f401f345a93401fa13e8a177d45567b5043e6 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 9 Jan 2025 11:37:41 +0100 Subject: [PATCH] Polishing. Refine JOIN and function keyword rendering. See #3692 --- .../query/HqlCountQueryTransformer.java | 1 + .../repository/query/HqlQueryRenderer.java | 19 ++++++++++--------- .../jpa/repository/query/QueryTokens.java | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlCountQueryTransformer.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlCountQueryTransformer.java index 662776019c..c5467460fc 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlCountQueryTransformer.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlCountQueryTransformer.java @@ -155,6 +155,7 @@ public QueryRendererBuilder visitJoin(HqlParser.JoinContext ctx) { QueryRendererBuilder builder = QueryRenderer.builder(); + builder.append(TOKEN_SPACE); builder.appendExpression(visit(ctx.joinType())); builder.append(QueryTokens.expression(ctx.JOIN())); 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 b4cc3f5386..90aede7d7f 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 @@ -336,8 +336,8 @@ public QueryTokenStream visitEntityWithJoins(HqlParser.EntityWithJoinsContext ct QueryRendererBuilder builder = QueryRenderer.builder(); - builder.appendExpression(visit(ctx.fromRoot())); - builder.appendInline(QueryTokenStream.concat(ctx.joinSpecifier(), this::visit, TOKEN_SPACE)); + builder.appendInline(visit(ctx.fromRoot())); + builder.appendInline(QueryTokenStream.concat(ctx.joinSpecifier(), this::visit, EMPTY_TOKEN)); return builder; } @@ -396,6 +396,7 @@ public QueryTokenStream visitJoin(HqlParser.JoinContext ctx) { QueryRendererBuilder builder = QueryRenderer.builder(); + builder.append(TOKEN_SPACE); builder.append(visit(ctx.joinType())); builder.append(QueryTokens.expression(ctx.JOIN())); @@ -753,7 +754,7 @@ public QueryTokenStream visitOffsetClause(HqlParser.OffsetClauseContext ctx) { QueryRendererBuilder builder = QueryRenderer.builder(); builder.append(QueryTokens.expression(ctx.OFFSET())); - builder.append(visit(ctx.parameterOrIntegerLiteral())); + builder.appendExpression(visit(ctx.parameterOrIntegerLiteral())); if (ctx.ROW() != null) { builder.append(QueryTokens.expression(ctx.ROW())); @@ -3384,7 +3385,7 @@ public QueryTokenStream visitEveryFunction(HqlParser.EveryFunctionContext ctx) { builder.append(TOKEN_CLOSE_PAREN); } else { - builder.appendExpression(visit(ctx.collectionQuantifier())); + builder.append(visit(ctx.collectionQuantifier())); builder.append(TOKEN_OPEN_PAREN); builder.appendInline(visit(ctx.simplePath())); @@ -3419,7 +3420,7 @@ public QueryTokenStream visitAnyFunction(HqlParser.AnyFunctionContext ctx) { builder.append(TOKEN_CLOSE_PAREN); } else { - builder.appendExpression(visit(ctx.collectionQuantifier())); + builder.append(visit(ctx.collectionQuantifier())); builder.append(TOKEN_OPEN_PAREN); builder.appendInline(visit(ctx.simplePath())); @@ -3808,9 +3809,9 @@ public QueryTokenStream visitInList(HqlParser.InListContext ctx) { if (ctx.simplePath() != null) { if (ctx.ELEMENTS() != null) { - builder.append(QueryTokens.expression(ctx.ELEMENTS())); + builder.append(QueryTokens.token(ctx.ELEMENTS())); } else if (ctx.INDICES() != null) { - builder.append(QueryTokens.expression(ctx.INDICES())); + builder.append(QueryTokens.token(ctx.INDICES())); } builder.append(TOKEN_OPEN_PAREN); @@ -3843,9 +3844,9 @@ public QueryTokenStream visitExistsExpression(HqlParser.ExistsExpressionContext builder.append(QueryTokens.expression(ctx.EXISTS())); if (ctx.ELEMENTS() != null) { - builder.append(QueryTokens.expression(ctx.ELEMENTS())); + builder.append(QueryTokens.token(ctx.ELEMENTS())); } else if (ctx.INDICES() != null) { - builder.append(QueryTokens.expression(ctx.INDICES())); + builder.append(QueryTokens.token(ctx.INDICES())); } builder.append(TOKEN_OPEN_PAREN); diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryTokens.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryTokens.java index 33ff1bc5ed..0a60c39acd 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryTokens.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryTokens.java @@ -31,6 +31,7 @@ class QueryTokens { /** * Commonly use tokens. */ + static final QueryToken EMPTY_TOKEN = token(""); static final QueryToken TOKEN_COMMA = token(", "); static final QueryToken TOKEN_SPACE = token(" "); static final QueryToken TOKEN_DOT = token(".");