Ensure that single-parameter DTOs work with JPQL queries.
Closes #1869.
This commit is contained in:
@@ -114,8 +114,8 @@ public abstract class QueryUtils {
|
|||||||
|
|
||||||
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
|
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 = 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]*\\)",
|
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
|
||||||
CASE_INSENSITIVE);
|
.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,
|
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
|
||||||
CASE_INSENSITIVE);
|
CASE_INSENSITIVE);
|
||||||
@@ -590,8 +590,9 @@ public abstract class QueryUtils {
|
|||||||
|
|
||||||
String variable = matcher.matches() ? matcher.group(VARIABLE_NAME_GROUP_INDEX) : null;
|
String variable = matcher.matches() ? matcher.group(VARIABLE_NAME_GROUP_INDEX) : null;
|
||||||
boolean useVariable = StringUtils.hasText(variable) //
|
boolean useVariable = StringUtils.hasText(variable) //
|
||||||
&& !variable.startsWith(" new") //
|
&& !variable.startsWith("new") // select [new com.example.User...
|
||||||
&& !variable.startsWith("count(") //
|
&& !variable.startsWith(" new") // select distinct[ new com.example.User...
|
||||||
|
&& !variable.startsWith("count(") // select [count(...
|
||||||
&& !variable.contains(",");
|
&& !variable.contains(",");
|
||||||
|
|
||||||
String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))
|
String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|||||||
@@ -84,6 +84,29 @@ class DefaultQueryUtilsUnitTests {
|
|||||||
"select count(distinct u) 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 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
|
@Test
|
||||||
void createsCountQueryForQueriesWithSubSelects() {
|
void createsCountQueryForQueriesWithSubSelects() {
|
||||||
|
|
||||||
@@ -400,8 +423,8 @@ class DefaultQueryUtilsUnitTests {
|
|||||||
@Test // DATAJPA-1363
|
@Test // DATAJPA-1363
|
||||||
void discoversAliasWithComplexFunction() {
|
void discoversAliasWithComplexFunction() {
|
||||||
|
|
||||||
assertThat(QueryUtils
|
assertThat(
|
||||||
.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
|
QueryUtils.getFunctionAliases("select new MyDto(sum(case when myEntity.prop3=0 then 1 else 0 end) as myAlias")) //
|
||||||
.contains("myAlias");
|
.contains("myAlias");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user