Skip to content
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

other types of comparisons for parameter based automatic query #892

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions api/src/main/java/jakarta/data/Limit.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
* For example,</p>
*
* <pre>
* Product[] findByNameLike(String namePattern, Limit limit, Sort&lt;?&gt;... sorts);
* &#64;Find
* Product[] named(&#64;By(_Product.NAME) &#64;Is(LIKE_IGNORE_CASE) String namePattern,
* Limit limit,
* Sort&lt;Product&gt;... sorts);
*
* ...
* mostExpensive50 = products.findByNameLike(pattern, Limit.of(50), Sort.desc("price"));
* mostExpensive50 = products.named(pattern, Limit.of(50), Sort.desc("price"));
* ...
* secondMostExpensive50 = products.findByNameLike(pattern, Limit.range(51, 100), Sort.desc("price"));
* secondMostExpensive50 = products.named(pattern, Limit.range(51, 100), Sort.desc("price"));
* </pre>
*
* <p>A repository method may not be declared with:
Expand Down
17 changes: 10 additions & 7 deletions api/src/main/java/jakarta/data/page/CursoredPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,26 @@
* query parameters) of type {@link PageRequest}, for example:</p>
*
* <pre>
* &#64;OrderBy("lastName")
* &#64;OrderBy("firstName")
* &#64;OrderBy("id")
* CursoredPage&lt;Employee&gt; findByHoursWorkedGreaterThan(int hours, PageRequest pageRequest);
* &#64;Find
* &#64;OrderBy(_Employee.LASTNAME)
* &#64;OrderBy(_Employee.FIRSTNAME)
* &#64;OrderBy(_Employee.ID)
* CursoredPage&lt;Employee&gt; withOvertime(
* &#64;By(_Employee.HOURSWORKED) &#64;Is(GREATER_THAN) int fullTimeHours,
* PageRequest pageRequest);
* </pre>
*
* <p>In initial page may be requested using an offset-based page request:</p>
*
* <pre>
* page = employees.findByHoursWorkedGreaterThan(1500, PageRequest.ofSize(50));
* page = employees.withOvertime(40, PageRequest.ofSize(50));
* </pre>
*
* <p>The next page may be requested relative to the end of the current page,
* as follows:</p>
*
* <pre>
* page = employees.findByHoursWorkedGreaterThan(1500, page.nextPageRequest());
* page = employees.withOvertime(40, page.nextPageRequest());
* </pre>
*
* <p>Here, the instance of {@link PageRequest} returned by
Expand All @@ -92,7 +95,7 @@
* PageRequest.ofPage(5)
* .size(50)
* .afterCursor(Cursor.forKey(emp.lastName, emp.firstName, emp.id));
* page = employees.findByHoursWorkedGreaterThan(1500, pageRequest);
* page = employees.withOvertime(40, pageRequest);
* </pre>
*
* <p>By making the query for the next page relative to observed values,
Expand Down
11 changes: 8 additions & 3 deletions api/src/main/java/jakarta/data/page/PageRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@
* example:</p>
*
* <pre>
* &#64;Find
* &#64;OrderBy("age")
* &#64;OrderBy("ssn")
* Person[] findByAgeBetween(int minAge, int maxAge, PageRequest pageRequest);
* Person[] agedBetween(&#64;By("age") &#64;Is(GREATER_THAN_EQ) int minAge,
* &#64;By("age") &#64;Is(LESS_THAN_EQ) int maxAge,
* PageRequest pageRequest);
* </pre>
*
* <p>This method might be called as follows:</p>
*
* <pre>
* var page = people.findByAgeBetween(35, 59,
* var page = people.agedBetween(
* 35, 59,
* PageRequest.ofSize(100));
* var results = page.content();
* ...
* while (page.hasNext()) {
* page = people.findByAgeBetween(35, 59,
* page = people.agedBetween(
* 35, 59,
* page.nextPageRequest().withoutTotal());
* results = page.content();
* ...
Expand Down
5 changes: 4 additions & 1 deletion api/src/main/java/jakarta/data/repository/By.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
* to the unique identifier field or property.
* </ul>
* <p>Arguments to the annotated parameter are compared to values of the
* mapped persistent field.</p>
* mapped persistent field. The equality comparison is used by default.<p>
*
* <p>For other types of basic comparisons, include the {@link Is} annotation.</p>
*
* <p>The field name may be a compound name like {@code address.city}.</p>
*
* <p>For example, for a {@code Person} entity with attributes {@code ssn},
Expand Down
103 changes: 103 additions & 0 deletions api/src/main/java/jakarta/data/repository/Is.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* 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
*
* http://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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package jakarta.data.repository;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* <p>Annotates a parameter of a repository {@link Find} or {@link Delete} method,
* indicating how a persistent field is compared against the parameter's value.
* The {@link By} annotation is used on the same parameter to identify the
* persistent field.</p>
*
* <p>For example,</p>
*
* <pre>
* &#64;Repository
* public interface Products extends CrudRepository&lt;Product, Long&gt; {
*
* // Find all Product entities where the price field is less than a maximum value.
* &#64;Find
* List&lt;Product&gt; pricedBelow(&#64;By(_Product.PRICE) &#64;Is(LESS_THAN) float max);
*
* // Find a page of Product entities where the name field matches a pattern, ignoring case.
* &#64;Find
* Page&lt;Product&gt; search(&#64;By(_Product.NAME) &#64;Is(LIKE_IGNORE_CASE) String pattern,
* PageRequest pagination,
* Order&lt;Product&gt; order);
*
* // Remove Product entities with any of the unique identifiers listed.
* &#64;Delete
* void remove(&#64;By(ID) &#64;Is(IN) List&lt;Long&gt; productIds);
* }
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Is {
// TODO add JavaDoc with examples to these
String EQUAL = "EQUAL";
Copy link
Contributor

@otaviojava otaviojava Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the power of flexibility of String; however, a typo can be a disaster; plus, the enum can show the available options by IDE.

In this case, I still prefer the enum; if a vendor wants to externalize this behavior, it can easily create its annotation.

I like the Effective Java item 34 in this case, thus, Enum instead of constant.

Copy link
Contributor Author

@njr-11 njr-11 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to make it more agreeable to Gavin. My own preference was for enum which I do still prefer, but I don't think it is a big deal one way or the other and would be happy with either. If we did leave it this way, I'm not at all worried about typos because the user doesn't write these, they use the constants we define in the spec (or that a vendor defines in their API). And yes, having a vendor extend behavior with an annotation of their seems perfectly reasonable to me, but I don't think Gavin is going to be on board with that and he was the one who brought up not being extensible as a drawback. In any case, if the two of you can agree on one way or the other (enum or String constant), I'll switch it to that because I really am fine with either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would vote to keep an enum or reuse the By annotation to give the provider more flexibility.

#857 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, I put the enum back for now. I was only anticipating that Gavin would prefer it with String constants to enable vendor extensions without an annotation, but I'm not actually sure of that. This is also now updated with the shorter ANY_CASE, and I added in the full list of possible enum constants, which I realize might need to be pared down later to gain agreement on getting this in.

String GREATER_THAN = "GREATER_THAN";
String GREATER_THAN_EQ = "GREATER_THAN_EQ";
String IGNORE_CASE = "IGNORE_CASE";
String IN = "IN";
String LESS_THAN = "LESS_THAN";
String LESS_THAN_EQ = "LESS_THAN_EQ";
String LIKE = "LIKE";
String LIKE_IGNORE_CASE = "LIKE_IGNORE_CASE";
String NOT = "NOT";
String NOT_IGNORE_CASE = "NOT_IGNORE_CASE";
String NOT_IN = "NOT_IN";
String NOT_LIKE = "NOT_LIKE";
String NOT_LIKE_IGNORE_CASE = "NOT_LIKE_IGNORE_CASE";

/**
* <p>The type of comparison operation to use when comparing a persistent
* field against a value that is supplied to a repository method.
* For portable applications, the comparison operation must be one of the
* constants defined within this class. Jakarta Data providers might choose
* to provide their own constants as non-portable extensions.</p>
*
* <p>The following example compares the year a person was born against
* a minimum and maximum year that are supplied as parameters to a repository
* method:</p>
*
* <pre>
* &#64;Find
* &#64;OrderBy(_Person.YEAR_BORN)
* List&lt;Person&gt; bornWithin(&#64;By(_Person.YEAR_BORN) &#64;Is(TREATER_THAN_EQ) float minYear,
* &#64;By(_Person.YEAR_BORN) &#64;Is(LESS_THAN_EQ) float maxYear);
* </pre>
*
* <p>The default comparison operation is the {@linkplain #EQUAL equality}
* comparison.</p>
*
* <p>For concise code, it can be convenient for a repository interface to
* statically import one or more constants from this class. For example:</p>
*
* <pre>
* import static jakarta.data.repository.Is.*;
* </pre>
*
* @return the type of comparison operation.
*/
String value() default EQUAL;
}
8 changes: 5 additions & 3 deletions api/src/main/java/jakarta/data/repository/OrderBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
* <p>The default sort order is ascending. The {@code descending} member can be
* used to specify the sort direction.</p>
* <pre>
* &#64;OrderBy(value = "price", descending = true)
* {@code Stream<Product>} findByPriceLessThanEqual(double maxPrice);
* &#64;Find
* &#64;OrderBy(value = _Product.PRICE, descending = true)
* {@code Stream<Product>} pricedBelow(&#64;By(_Product.PRICE) &#64;Is(LESS_THAN) double maxPrice);
* </pre>
*
* <p>A repository method with an {@code @OrderBy} annotation must not
Expand Down Expand Up @@ -115,8 +116,9 @@
* <p>For example,</p>
*
* <pre>
* &#64;Find
* &#64;OrderBy("age")
* Stream&lt;Person&gt; findByLastName(String lastName);
* Stream&lt;Person&gt; withLastName(&#64;By("lastName") &#64;Is(IGNORE_CASE) String surname);
* </pre>
*
* @return entity attribute name.
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/jakarta/data/repository/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
* &#64;Repository
* public interface Products extends DataRepository&lt;Product, Long&gt; {
*
* &#64;Find
* &#64;OrderBy("price")
* List&lt;Product&gt; findByNameLike(String namePattern);
* List&lt;Product&gt; named(&#64;By("name") &#64;Is(LIKE_IGNORE_CASE) String namePattern);
*
* &#64;Query("UPDATE Product SET price = price - (price * ?1) WHERE price * ?1 &lt;= ?2")
* int putOnSale(float rateOfDiscount, float maxDiscount);
Expand All @@ -52,7 +53,7 @@
* Products products;
*
* ...
* found = products.findByNameLike("%Printer%");
* found = products.named("%Printer%");
* numUpdated = products.putOnSale(0.15f, 20.0f);
* </pre>
*
Expand Down
Loading