Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Introduce QueryInformation to provide query introspection results. Refine generics.

See #3726
Original pull request: #3730
  • Loading branch information
mp911de committed Jan 13, 2025
1 parent 643a3a9 commit f2d1a35
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class EqlCountQueryTransformer extends EqlQueryRenderer {
private final @Nullable String countProjection;
private final @Nullable String primaryFromAlias;

EqlCountQueryTransformer(@Nullable String countProjection, @Nullable String primaryFromAlias) {

EqlCountQueryTransformer(@Nullable String countProjection, QueryInformation queryInformation) {
this.countProjection = countProjection;
this.primaryFromAlias = primaryFromAlias;
this.primaryFromAlias = queryInformation.getAlias();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

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

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -31,7 +31,7 @@
* @author Christoph Strobl
*/
@SuppressWarnings("UnreachableCode")
class EqlQueryIntrospector extends EqlBaseVisitor<Void> implements ParsedQueryIntrospector {
class EqlQueryIntrospector extends EqlBaseVisitor<Void> implements ParsedQueryIntrospector<QueryInformation> {

private final EqlQueryRenderer renderer = new EqlQueryRenderer();

Expand All @@ -41,18 +41,9 @@ class EqlQueryIntrospector extends EqlBaseVisitor<Void> implements ParsedQueryIn
private boolean hasConstructorExpression = false;

@Override
public String getAlias() {
return primaryFromAlias;
}

@Override
public List<QueryToken> getProjection() {
return projection == null ? Collections.emptyList() : projection;
}

@Override
public boolean hasConstructorExpression() {
return hasConstructorExpression;
public QueryInformation getParsedQueryInformation() {
return new QueryInformation(primaryFromAlias, projection == null ? Collections.emptyList() : projection,
hasConstructorExpression);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ class EqlSortedQueryTransformer extends EqlQueryRenderer {
private final @Nullable String primaryFromAlias;
private final @Nullable DtoProjectionTransformerDelegate dtoDelegate;

EqlSortedQueryTransformer(Sort sort, @Nullable String primaryFromAlias, @Nullable ReturnedType returnedType) {
EqlSortedQueryTransformer(Sort sort, QueryInformation queryInformation, @Nullable ReturnedType returnedType) {

Assert.notNull(sort, "Sort must not be null");
Assert.notNull(queryInformation, "ParsedHqlQueryInformation must not be null");

this.sort = sort;
this.primaryFromAlias = primaryFromAlias;
this.primaryFromAlias = queryInformation.getAlias();
this.dtoDelegate = returnedType == null ? null : new DtoProjectionTransformerDelegate(returnedType);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.query;

import java.util.List;

import org.springframework.lang.Nullable;

/**
* Hibernate-specific query details capturing common table expression details.
*
* @author Mark Paluch
* @since 3.5
*/
class HibernateQueryInformation extends QueryInformation {

private final boolean hasCte;

public HibernateQueryInformation(@Nullable String alias, List<QueryToken> projection,
boolean hasConstructorExpression, boolean hasCte) {
super(alias, projection, hasConstructorExpression);
this.hasCte = hasCte;
}

public boolean hasCte() {
return hasCte;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ class HqlCountQueryTransformer extends HqlQueryRenderer {

private final @Nullable String countProjection;
private final @Nullable String primaryFromAlias;
private boolean containsCTE = false;
private final boolean containsCTE;

HqlCountQueryTransformer(@Nullable String countProjection, @Nullable String primaryFromAlias) {
HqlCountQueryTransformer(@Nullable String countProjection, HibernateQueryInformation queryInformation) {
this.countProjection = countProjection;
this.primaryFromAlias = primaryFromAlias;
this.primaryFromAlias = queryInformation.getAlias();
this.containsCTE = queryInformation.hasCte();
}

@Override
Expand All @@ -67,12 +68,6 @@ public QueryRendererBuilder visitOrderedQuery(HqlParser.OrderedQueryContext ctx)
return builder;
}

@Override
public QueryTokenStream visitCte(HqlParser.CteContext ctx) {
this.containsCTE = true;
return super.visitCte(ctx);
}

@Override
public QueryRendererBuilder visitFromQuery(HqlParser.FromQueryContext ctx) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,20 @@
* @author Mark Paluch
*/
@SuppressWarnings({ "UnreachableCode", "ConstantValue" })
class HqlQueryIntrospector extends HqlBaseVisitor<Void> implements ParsedQueryIntrospector {
class HqlQueryIntrospector extends HqlBaseVisitor<Void> implements ParsedQueryIntrospector<HibernateQueryInformation> {

private final HqlQueryRenderer renderer = new HqlQueryRenderer();

private @Nullable String primaryFromAlias = null;
private @Nullable List<QueryToken> projection;
private boolean projectionProcessed;
private boolean hasConstructorExpression = false;
private boolean hasCte = false;

@Override
public String getAlias() {
return primaryFromAlias;
}

@Override
public List<QueryToken> getProjection() {
return projection == null ? Collections.emptyList() : projection;
}

@Override
public boolean hasConstructorExpression() {
return hasConstructorExpression;
public HibernateQueryInformation getParsedQueryInformation() {
return new HibernateQueryInformation(primaryFromAlias, projection == null ? Collections.emptyList() : projection,
hasConstructorExpression, hasCte);
}

@Override
Expand All @@ -65,6 +57,12 @@ public Void visitSelectClause(HqlParser.SelectClauseContext ctx) {
return super.visitSelectClause(ctx);
}

@Override
public Void visitCte(HqlParser.CteContext ctx) {
this.hasCte = true;
return super.visitCte(ctx);
}

@Override
public Void visitFromRoot(HqlParser.FromRootContext ctx) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ class HqlSortedQueryTransformer extends HqlQueryRenderer {
private final @Nullable String primaryFromAlias;
private final @Nullable DtoProjectionTransformerDelegate dtoDelegate;

HqlSortedQueryTransformer(Sort sort, @Nullable String primaryFromAlias) {
HqlSortedQueryTransformer(Sort sort, HibernateQueryInformation queryInformation,
@Nullable ReturnedType returnedType) {

Assert.notNull(sort, "Sort must not be null");
Assert.notNull(queryInformation, "ParsedHqlQueryInformation must not be null");

this.sort = sort;
this.primaryFromAlias = primaryFromAlias;
this.dtoDelegate = null;
}

HqlSortedQueryTransformer(Sort sort, @Nullable String primaryFromAlias, @Nullable ReturnedType returnedType) {

Assert.notNull(sort, "Sort must not be null");

this.sort = sort;
this.primaryFromAlias = primaryFromAlias;
this.primaryFromAlias = queryInformation.getAlias();
this.dtoDelegate = returnedType == null ? null : new DtoProjectionTransformerDelegate(returnedType);
}

Expand Down
Loading

0 comments on commit f2d1a35

Please sign in to comment.