Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Fix typos, move QueryTransformers.filterCountSelection to CountSelectionTokenStream.create.

Original Pull Request: #3553
  • Loading branch information
mp911de authored and christophstrobl committed Aug 9, 2024
1 parent 9d4bde3 commit b279ff0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import static org.springframework.data.jpa.repository.query.QueryTokens.TOKEN_CLOSE_PAREN;
import static org.springframework.data.jpa.repository.query.QueryTokens.TOKEN_COMMA;
import static org.springframework.data.jpa.repository.query.QueryTokens.TOKEN_COUNT_FUNC;
import static org.springframework.data.jpa.repository.query.QueryTokens.*;

import org.springframework.data.jpa.repository.query.QueryRenderer.QueryRendererBuilder;
import org.springframework.data.jpa.repository.query.QueryTransformers.CountSelectionTokenStream;
Expand Down Expand Up @@ -98,7 +96,7 @@ public QueryTokenStream visitSelect_clause(EqlParser.Select_clauseContext ctx) {
private QueryRendererBuilder getDistinctCountSelection(QueryTokenStream selectionListbuilder) {

QueryRendererBuilder nested = new QueryRendererBuilder();
CountSelectionTokenStream countSelection = QueryTransformers.filterCountSelection(selectionListbuilder);
CountSelectionTokenStream countSelection = CountSelectionTokenStream.create(selectionListbuilder);

if (countSelection.requiresPrimaryAlias()) {
// constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private QueryRendererBuilder visitSubQuerySelectClause(SelectClauseContext ctx,
private QueryRendererBuilder getDistinctCountSelection(QueryTokenStream selectionListbuilder) {

QueryRendererBuilder nested = new QueryRendererBuilder();
CountSelectionTokenStream countSelection = QueryTransformers.filterCountSelection(selectionListbuilder);
CountSelectionTokenStream countSelection = CountSelectionTokenStream.create(selectionListbuilder);

if (countSelection.requiresPrimaryAlias()) {
// constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public QueryRendererBuilder visitSelect_clause(JpqlParser.Select_clauseContext c
private QueryRendererBuilder getDistinctCountSelection(QueryTokenStream selectionListbuilder) {

QueryRendererBuilder nested = new QueryRendererBuilder();
CountSelectionTokenStream countSelection = QueryTransformers.filterCountSelection(selectionListbuilder);
CountSelectionTokenStream countSelection = CountSelectionTokenStream.create(selectionListbuilder);

if (countSelection.requiresPrimaryAlias()) {
// constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,46 @@
*/
class QueryTransformers {

static CountSelectionTokenStream filterCountSelection(QueryTokenStream selection) {
static class CountSelectionTokenStream implements QueryTokenStream {

List<QueryToken> target = new ArrayList<>(selection.size());
boolean skipNext = false;
boolean containsNew = false;
private final List<QueryToken> tokens;
private final boolean requiresPrimaryAlias;

for (QueryToken token : selection) {
CountSelectionTokenStream(List<QueryToken> tokens, boolean requiresPrimaryAlias) {
this.tokens = tokens;
this.requiresPrimaryAlias = requiresPrimaryAlias;
}

if (skipNext) {
skipNext = false;
continue;
}
static CountSelectionTokenStream create(QueryTokenStream selection) {

if (token.equals(TOKEN_AS)) {
skipNext = true;
continue;
}
List<QueryToken> target = new ArrayList<>(selection.size());
boolean skipNext = false;
boolean containsNew = false;

if (!token.equals(TOKEN_COMMA) && token.isExpression()) {
token = QueryTokens.token(token.value());
}
for (QueryToken token : selection) {

if (!containsNew && token.value().contains("new")) {
containsNew = true;
}
if (skipNext) {
skipNext = false;
continue;
}

target.add(token);
}
if (token.equals(TOKEN_AS)) {
skipNext = true;
continue;
}

return new CountSelectionTokenStream(target, containsNew);
}
if (!token.equals(TOKEN_COMMA) && token.isExpression()) {
token = QueryTokens.token(token.value());
}

static class CountSelectionTokenStream implements QueryTokenStream {
if (!containsNew && token.value().contains("new")) {
containsNew = true;
}

private final List<QueryToken> tokens;
private final boolean requiresPrimaryAlias;
target.add(token);
}

public CountSelectionTokenStream(List<QueryToken> tokens, boolean requiresPrimaryAlias) {
this.tokens = tokens;
this.requiresPrimaryAlias = requiresPrimaryAlias;
return new CountSelectionTokenStream(target, containsNew);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void applyCountToMoreComplexQuery() {
}

@Test
void applyCountToAlreadySorteQuery() {
void applyCountToAlreadySortedQuery() {

// given
var original = "SELECT e FROM Employee e where e.name = :name ORDER BY e.modified_date";
Expand Down

0 comments on commit b279ff0

Please sign in to comment.