Skip to content

Commit

Permalink
Ensure thatsingle-parameter DOTs work with JPQL queries.
Browse files Browse the repository at this point in the history
Closes #1869.
  • Loading branch information
gregturn committed Jun 13, 2022
1 parent b57eb85 commit 8924156
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public abstract class QueryUtils {

private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)",
CASE_INSENSITIVE);
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)", CASE_INSENSITIVE);

private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
CASE_INSENSITIVE);
Expand Down Expand Up @@ -590,8 +590,9 @@ public static String createCountQueryFor(String originalQuery, @Nullable String

String variable = matcher.matches() ? matcher.group(VARIABLE_NAME_GROUP_INDEX) : null;
boolean useVariable = StringUtils.hasText(variable) //
&& !variable.startsWith(" new") //
&& !variable.startsWith("count(") //
&& !variable.startsWith("new") // select [new com.example.User...
&& !variable.startsWith(" new") // select distinct[ new com.example.User...
&& !variable.startsWith("count(") // select [count(...
&& !variable.contains(",");

String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ void createsCountQueryForJoins() {
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
}

@Test // GH-1869
void createsCountQueryForJoinsWithTwoArgs() {

assertCountQuery("select distinct new User(u.name, u.age) from User u left outer join u.roles r WHERE r = ?",
"select count(distinct u) from User u left outer join u.roles r WHERE r = ?");
}

@Test // GH-1869
void createsCountQueryForDtoWithOneArg() {

assertCountQuery(
"SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = ?",
"select count(u) from User u where u.firstname = ?");
}

@Test // GH-1869
void createsCountQueryForDtoWithTwoArgs() {

assertCountQuery(
"SELECT new org.springframework.data.jpa.repository.sample.NameOnlyDto(u.firstname, u.lastname) from User u where u.firstname = ?",
"select count(u) from User u where u.firstname = ?");
}

@Test
void createsCountQueryForQueriesWithSubSelects() {

Expand Down Expand Up @@ -400,8 +423,8 @@ void doesNotContainStaticClauseInExistsQuery() {
@Test // DATAJPA-1363
void discoversAliasWithComplexFunction() {

assertThat(QueryUtils
.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
assertThat(
QueryUtils.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
.contains("myAlias");
}

Expand Down

0 comments on commit 8924156

Please sign in to comment.