Skip to content

DATACMNS-875 - Add support for exists projection in repository query derivation. #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.0.BUILD-SNAPSHOT</version>
<version>1.13.0.DATACMNS-875-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2016 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.
Expand Down Expand Up @@ -36,6 +36,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
public class PartTree implements Iterable<OrPart> {

Expand All @@ -52,9 +53,10 @@ public class PartTree implements Iterable<OrPart> {
private static final String KEYWORD_TEMPLATE = "(%s)(?=(\\p{Lu}|\\P{InBASIC_LATIN}))";
private static final String QUERY_PATTERN = "find|read|get|query|stream";
private static final String COUNT_PATTERN = "count";
private static final String EXISTS_PATTERN = "exists";
private static final String DELETE_PATTERN = "delete|remove";
private static final Pattern PREFIX_TEMPLATE = Pattern.compile( //
"^(" + QUERY_PATTERN + "|" + COUNT_PATTERN + "|" + DELETE_PATTERN + ")((\\p{Lu}.*?))??By");
"^(" + QUERY_PATTERN + "|" + COUNT_PATTERN + "|" + EXISTS_PATTERN + "|" + DELETE_PATTERN + ")((\\p{Lu}.*?))??By");

/**
* The subject, for example "findDistinctUserByNameOrderByAge" would have the subject "DistinctUser".
Expand Down Expand Up @@ -125,6 +127,16 @@ public Boolean isCountProjection() {
return subject.isCountProjection();
}

/**
* Returns whether an exists projection shall be applied.
*
* @return
* @since 1.13
*/
public Boolean isExistsProjection() {
return subject.isExistsProjection();
}

/**
* return true if the created {@link PartTree} is meant to be used for delete operation.
*
Expand Down Expand Up @@ -262,20 +274,23 @@ private static class Subject {

private static final String DISTINCT = "Distinct";
private static final Pattern COUNT_BY_TEMPLATE = Pattern.compile("^count(\\p{Lu}.*?)??By");
private static final Pattern EXISTS_BY_TEMPLATE = Pattern.compile("^(" + EXISTS_PATTERN + ")(\\p{Lu}.*?)??By");
private static final Pattern DELETE_BY_TEMPLATE = Pattern.compile("^(" + DELETE_PATTERN + ")(\\p{Lu}.*?)??By");
private static final String LIMITING_QUERY_PATTERN = "(First|Top)(\\d*)?";
private static final Pattern LIMITED_QUERY_TEMPLATE = Pattern.compile("^(" + QUERY_PATTERN + ")(" + DISTINCT + ")?"
+ LIMITING_QUERY_PATTERN + "(\\p{Lu}.*?)??By");

private final boolean distinct;
private final boolean count;
private final boolean exists;
private final boolean delete;
private final Integer maxResults;

public Subject(String subject) {

this.distinct = subject == null ? false : subject.contains(DISTINCT);
this.count = matches(subject, COUNT_BY_TEMPLATE);
this.exists = matches(subject, EXISTS_BY_TEMPLATE);
this.delete = matches(subject, DELETE_BY_TEMPLATE);
this.maxResults = returnMaxResultsIfFirstKSubjectOrNull(subject);
}
Expand Down Expand Up @@ -314,6 +329,16 @@ public boolean isCountProjection() {
return count;
}

/**
* Returns {@literal true} if {@link Subject} matches {@link #EXISTS_BY_TEMPLATE}.
*
* @return
* @since 1.13
*/
public boolean isExistsProjection() {
return exists;
}

public boolean isDistinct() {
return distinct;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2016 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
Expand Down Expand Up @@ -44,10 +44,11 @@
* @author Thomas Darimont
* @author Martin Baumgartner
* @author Christoph Strobl
* @author Mark Paluch
*/
public class PartTreeUnitTests {

private String[] PREFIXES = { "find", "read", "get", "query", "stream", "count", "delete", "remove" };
private String[] PREFIXES = { "find", "read", "get", "query", "stream", "count", "delete", "remove", "exists" };

@Test(expected = IllegalArgumentException.class)
public void rejectsNullSource() throws Exception {
Expand Down Expand Up @@ -436,6 +437,16 @@ public void identifiesSimpleCountByCorrectly() {
assertThat(tree.isCountProjection(), is(true));
}

/**
* @see DATACMNS-875
*/
@Test
public void identifiesSimpleExistsByCorrectly() {

PartTree tree = new PartTree("existsByLastname", User.class);
assertThat(tree.isExistsProjection(), is(true));
}

/**
* @see DATACMNS-399
*/
Expand Down Expand Up @@ -652,6 +663,16 @@ public void shouldNotSupportLimitingCountQueries() {
assertLimiting("countTop10DistinctUsersByLastname", User.class, false, null, true);
}

/**
* @see DATACMNS-875
*/
@Test
public void shouldNotSupportLimitingExistQueries() {

assertLimiting("existsFirst10DistinctUsersByLastname", User.class, false, null, true);
assertLimiting("existsTop10DistinctUsersByLastname", User.class, false, null, true);
}

/**
* @see DATACMNS-581
*/
Expand Down