Skip to content

Commit

Permalink
Post-Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Dec 12, 2024
1 parent 0ee6c66 commit debdeba
Show file tree
Hide file tree
Showing 34 changed files with 324 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ OR TREAT(p AS SmallProject).name LIKE 'Persist%'
OR p.description LIKE "cost overrun"
""";

query = DeclaredQuery.of(s, false);
enhancer = QueryEnhancerFactory.forQuery(query);
query = DeclaredQuery.ofJpql(s);
enhancer = QueryEnhancerFactory.forQuery(query).create(query);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void doSetup() throws IOException {
select SOME_COLUMN from SOME_OTHER_TABLE where REPORTING_DATE = :REPORTING_DATE
union select SOME_COLUMN from SOME_OTHER_OTHER_TABLE""";

enhancer = new JSqlParserQueryEnhancer(DeclaredQuery.of(s, true));
enhancer = new JSqlParserQueryEnhancer(DeclaredQuery.ofNative(s));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {

private final IntrospectedQuery query;
private final EntityQuery query;
private final Lazy<IntrospectedQuery> countQuery;
private final ValueExpressionDelegate valueExpressionDelegate;
private final QueryParameterSetter.QueryMetadataCache metadataCache = new QueryParameterSetter.QueryMetadataCache();
Expand All @@ -65,33 +65,27 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
* @param em must not be {@literal null}.
* @param queryString must not be {@literal null}.
* @param countQueryString must not be {@literal null}.
* @param queryRewriter must not be {@literal null}.
* @param valueExpressionDelegate must not be {@literal null}.
* @param queryConfiguration must not be {@literal null}.
*/
public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, String queryString,
@Nullable String countQueryString, QueryRewriter queryRewriter,
ValueExpressionDelegate valueExpressionDelegate, JpaQueryConfiguration queryConfiguration) {
@Nullable String countQueryString, JpaQueryConfiguration queryConfiguration) {

super(method, em);

Assert.hasText(queryString, "Query string must not be null or empty");
Assert.notNull(valueExpressionDelegate, "ValueExpressionDelegate must not be null");
Assert.notNull(queryRewriter, "QueryRewriter must not be null");
Assert.notNull(queryConfiguration, "JpaQueryConfiguration must not be null");

this.valueExpressionDelegate = valueExpressionDelegate;
this.valueExpressionDelegate = queryConfiguration.getValueExpressionDelegate();
this.valueExpressionContextProvider = valueExpressionDelegate.createValueContextProvider(method.getParameters());
this.query = ExpressionBasedStringQuery.create(queryString, method, queryConfiguration);

this.countQuery = Lazy.of(() -> {

if (StringUtils.hasText(countQueryString)) {

return ExpressionBasedStringQuery.create(countQueryString, method, queryConfiguration);

return ExpressionBasedStringQuery.create(countQueryString, method, queryConfiguration);
}

return query.deriveCountQuery(method.getCountQueryProjection());
return this.query.deriveCountQuery(method.getCountQueryProjection());
});

this.countParameterBinder = Lazy.of(() -> {
Expand All @@ -107,7 +101,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
this.querySortRewriter = NoOpQuerySortRewriter.INSTANCE;
}

Assert.isTrue(method.isNativeQuery() || !query.usesJdbcStyleParameters(),
Assert.isTrue(method.isNativeQuery() || !this.query.usesJdbcStyleParameters(),
"JDBC style parameters (?) are not supported for JPA queries");
}

Expand Down Expand Up @@ -162,7 +156,7 @@ protected Query doCreateCountQuery(JpaParametersParameterAccessor accessor) {
/**
* @return the query
*/
public IntrospectedQuery getQuery() {
public EntityQuery getQuery() {
return query;
}

Expand Down Expand Up @@ -210,16 +204,14 @@ protected String potentiallyRewriteQuery(String originalQuery, Sort sort, @Nulla
}

String applySorting(CachableQuery cachableQuery) {

return QueryEnhancerFactory.forQuery(cachableQuery.getDeclaredQuery()).applySorting(cachableQuery.getSort(),
cachableQuery.getAlias());
return cachableQuery.getDeclaredQuery().applySorting(cachableQuery.getSort());
}

/**
* Query Sort Rewriter interface.
*/
interface QuerySortRewriter {
String getSorted(IntrospectedQuery query, Sort sort);
String getSorted(EntityQuery query, Sort sort);
}

/**
Expand All @@ -228,7 +220,7 @@ interface QuerySortRewriter {
enum NoOpQuerySortRewriter implements QuerySortRewriter {
INSTANCE;

public String getSorted(IntrospectedQuery query, Sort sort) {
public String getSorted(EntityQuery query, Sort sort) {

if (sort.isSorted()) {
throw new UnsupportedOperationException("NoOpQueryCache does not support sorting");
Expand All @@ -247,7 +239,7 @@ class CachingQuerySortRewriter implements QuerySortRewriter {
AbstractStringBasedJpaQuery.this::applySorting);

@Override
public String getSorted(IntrospectedQuery query, Sort sort) {
public String getSorted(EntityQuery query, Sort sort) {

if (sort.isUnsorted()) {
return query.getQueryString();
Expand All @@ -266,30 +258,25 @@ public String getSorted(IntrospectedQuery query, Sort sort) {
*/
static class CachableQuery {

private final IntrospectedQuery introspectedQuery;
private final EntityQuery introspectedQuery;
private final String queryString;
private final Sort sort;

CachableQuery(IntrospectedQuery query, Sort sort) {
CachableQuery(EntityQuery query, Sort sort) {

this.introspectedQuery = query;
this.queryString = query.getQueryString();
this.sort = sort;
}

IntrospectedQuery getDeclaredQuery() {
EntityQuery getDeclaredQuery() {
return introspectedQuery;
}

Sort getSort() {
return sort;
}

@Nullable
String getAlias() {
return introspectedQuery.getAlias();
}

@Override
public boolean equals(Object o) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface DeclaredQuery {
* @param query the JPQL query string.
* @return
*/
static DeclaredQuery jpql(String query) {
static DeclaredQuery ofJpql(String query) {
return new DefaultDeclaredQuery(query, false);
}

Expand All @@ -38,7 +38,7 @@ static DeclaredQuery jpql(String query) {
* @param query the native query string.
* @return
*/
static DeclaredQuery nativeQuery(String query) {
static DeclaredQuery ofNative(String query) {
return new DefaultDeclaredQuery(query, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class DefaultDeclaredQuery implements DeclaredQuery {
this.nativeQuery = nativeQuery;
}

@Override
public String getQueryString() {
return query;
}

@Override
public boolean isNativeQuery() {
return nativeQuery;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;

/**
Expand All @@ -26,12 +27,12 @@
* @author Jens Schauder
* @since 2.0.3
*/
class EmptyIntrospectedQuery implements IntrospectedQuery {
class EmptyIntrospectedQuery implements EntityQuery {

/**
* An implementation implementing the NULL-Object pattern for situations where there is no query.
*/
static final IntrospectedQuery EMPTY_QUERY = new EmptyIntrospectedQuery();
static final EntityQuery EMPTY_QUERY = new EmptyIntrospectedQuery();

@Override
public boolean hasNamedParameter() {
Expand All @@ -48,11 +49,6 @@ public boolean isNativeQuery() {
return false;
}

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

@Override
public boolean hasConstructorExpression() {
return false;
Expand All @@ -73,6 +69,11 @@ public IntrospectedQuery deriveCountQuery(@Nullable String countQueryProjection)
return EMPTY_QUERY;
}

@Override
public String applySorting(Sort sort) {
return "";
}

@Override
public boolean usesJdbcStyleParameters() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2018-2024 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 org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

/**
* A wrapper for a String representation of a query offering information about the query.
*
* @author Jens Schauder
* @author Diego Krupitza
* @since 2.0.3
*/
interface EntityQuery extends IntrospectedQuery {

/**
* Creates a DeclaredQuery for a JPQL query.
*
* @param query the JPQL query string.
* @return
*/
static EntityQuery introspectJpql(String query, QueryEnhancerFactory queryEnhancer) {
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY
: new StringQuery(query, false, queryEnhancer);
}

/**
* Creates a DeclaredQuery for a JPQL query.
*
* @param query the JPQL query string.
* @return
*/
static EntityQuery introspectJpql(String query, QueryEnhancerSelector selector) {
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY : new StringQuery(query, false, selector);
}

/**
* Creates a DeclaredQuery for a native query.
*
* @param query the native query string.
* @return
*/
static EntityQuery introspectNativeQuery(String query, QueryEnhancerFactory queryEnhancer) {
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY
: new StringQuery(query, true, queryEnhancer);
}

/**
* Creates a DeclaredQuery for a native query.
*
* @param query the native query string.
* @return
*/
static EntityQuery introspectNativeQuery(String query, QueryEnhancerSelector selector) {
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY : new StringQuery(query, true, selector);
}

/**
* Returns whether the query is using a constructor expression.
*
* @since 1.10
*/
boolean hasConstructorExpression();

/**
* Returns whether the query uses the default projection, i.e. returns the main alias defined for the query.
*/
boolean isDefaultProjection();

/**
* Creates a new {@literal DeclaredQuery} representing a count query, i.e. a query returning the number of rows to be
* expected from the original query, either derived from the query wrapped by this instance or from the information
* passed as arguments.
*
* @param countQueryProjection an optional return type for the query.
* @return a new {@literal DeclaredQuery} instance.
*/
IntrospectedQuery deriveCountQuery(@Nullable String countQueryProjection);

String applySorting(Sort sort);

/**
* @return whether paging is implemented in the query itself, e.g. using SpEL expressions.
* @since 2.0.6
*/
default boolean usesPaging() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,6 @@ class ExpressionBasedStringQuery extends StringQuery {
selector);
}

/**
* Creates an {@link ExpressionBasedStringQuery} from a given {@link DeclaredQuery}.
*
* @param query the original query. Must not be {@literal null}.
* @param metadata the {@link JpaEntityMetadata} for the given entity. Must not be {@literal null}.
* @param parser Parser for resolving SpEL expressions. Must not be {@literal null}.
* @param nativeQuery is a given query native or not
* @return A query supporting SpEL expressions.
*/
static ExpressionBasedStringQuery from(DeclaredQuery query, JpaEntityMetadata<?> metadata,
ValueExpressionParser parser, boolean nativeQuery) {
return new ExpressionBasedStringQuery(query.getQueryString(), metadata, parser, nativeQuery);
}

/**
* @param query, the query expression potentially containing a SpEL expression. Must not be {@literal null}.
* @param metadata the {@link JpaEntityMetadata} for the given entity. Must not be {@literal null}.
Expand Down Expand Up @@ -125,8 +111,9 @@ private static boolean containsExpression(String query) {
return query.contains(ENTITY_NAME_VARIABLE_EXPRESSION);
}

public static IntrospectedQuery create(String query, JpaQueryMethod method, JpaQueryConfiguration queryContext) {
return new ExpressionBasedStringQuery(query, method.getEntityInformation(), queryContext.getParser(),
public static EntityQuery create(String query, JpaQueryMethod method, JpaQueryConfiguration queryContext) {
return new ExpressionBasedStringQuery(query, method.getEntityInformation(),
queryContext.getValueExpressionDelegate().getValueExpressionParser(),
method.isNativeQuery(), queryContext.getSelector());
}

Expand Down
Loading

0 comments on commit debdeba

Please sign in to comment.