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

Enhance query with pagination at Repository #536

Merged
merged 24 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
baff61a
docs: enhance documentation to entities
otaviojava Aug 4, 2024
26645c8
feat: organize methods to the select in the structure template
otaviojava Aug 4, 2024
767487d
feat: implement query
otaviojava Aug 4, 2024
f379edd
test: create test to select field
otaviojava Aug 4, 2024
b6481df
feat: update select field to return function instead of change the st…
otaviojava Aug 4, 2024
cd55872
test: update select field mapper scenario
otaviojava Aug 4, 2024
b84cfa3
feat: start the implementation with prepare statement
otaviojava Aug 4, 2024
cf389a9
feat: create scenario to single list entities
otaviojava Aug 4, 2024
7134dd8
test: create scenario to list result
otaviojava Aug 4, 2024
bfc8bc5
feat: update API using new nameclature to each new event in the mapper
otaviojava Aug 4, 2024
b853f49
feat: implement observer with new events
otaviojava Aug 4, 2024
004a75e
style: fix style
otaviojava Aug 5, 2024
45ba869
feat: include dynamice query method return
otaviojava Aug 6, 2024
415b18b
feat: fix builder
otaviojava Aug 6, 2024
601cc23
test: fix dynamic query return test
otaviojava Aug 6, 2024
235f1e8
feat: include the page condition when execute query
otaviojava Aug 6, 2024
967e0d5
style: remove unsed imports
otaviojava Aug 6, 2024
a316e53
feat: include pagination at pagerequest in query
otaviojava Aug 6, 2024
a6af931
docs: update documentation of changelog
otaviojava Aug 6, 2024
6726207
feat: put the diamon effect
otaviojava Aug 6, 2024
21982d0
feat: generate warning to dynamic return
otaviojava Aug 6, 2024
268cc88
refactoring the builder and removing with
otaviojava Aug 6, 2024
d55de09
refactoring remove the with ath builder
otaviojava Aug 6, 2024
caa7d53
feat: enhance and update has next
otaviojava Aug 6, 2024
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: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
- Include the `First` keyword in the method by query in the Repository
- Include the `Null`, `NotNull` and `countAll` keywords in the method by query in the Repository
- Include condition to is NUll and is Not Null in the query
- Include pagination with Query annotation

=== Fixed

- Fix the `Orderby` annotation in the Repository
- Make the JDQL return the correct type when the select is by field

== [1.1.1] - 2023-05-25

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,32 @@ default String fireEntity(String entity) {
* @return the result of processing the field name
* @throws NullPointerException when either entity or field is null
*/
default String fireField(String entity, String field) {
default String fireSelectField(String entity, String field) {
return field;
}

/**
* Fires an event for each sort property in the mapper process.
*
* @param entity the entity name
* @param field the field name
* @return the result of processing the sort property
* @throws NullPointerException when either entity or field is null
*/
default String fireSortProperty(String entity, String field) {
return field;
}


/**
* Fires an event for each condition field in the mapper process.
*
* @param entity the entity name
* @param field the field name
* @return the result of processing the condition field
* @throws NullPointerException when either entity or field is null
*/
default String fireConditionField(String entity, String field) {
return field;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static CriteriaCondition getCondition(QueryCondition condition, Params parameter
}

private static String getName(QueryCondition condition, CommunicationObserverParser observer, String entity) {
return observer.fireField(entity, condition.name());
return observer.fireConditionField(entity, condition.name());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private DeleteQuery getQuery(String query, Params params, CommunicationObserverP
private DeleteQuery getQuery(Params params, CommunicationObserverParser observer, org.eclipse.jnosql.communication.query.DeleteQuery deleteQuery) {
String columnFamily = observer.fireEntity(deleteQuery.entity());
List<String> columns = deleteQuery.fields().stream()
.map(f -> observer.fireField(columnFamily, f))
.map(f -> observer.fireSelectField(columnFamily, f))
.collect(Collectors.toList());
CriteriaCondition condition = deleteQuery.where().map(c -> Conditions.getCondition(c, params, observer, columnFamily))
.orElse(null);
Expand All @@ -83,7 +83,7 @@ private DeleteQuery getQuery(String query, CommunicationObserverParser observer)

String columnFamily = observer.fireEntity(deleteQuery.entity());
List<String> columns = deleteQuery.fields().stream()
.map(f -> observer.fireField(columnFamily, f))
.map(f -> observer.fireSelectField(columnFamily, f))
.collect(Collectors.toList());
Params params = Params.newParams();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private SelectQuery query(String query, String entity, CommunicationObserverPars
var limit = selectQuery.limit();
var skip = selectQuery.skip();
var columns = selectQuery.fields().stream()
.map(f -> observer.fireField(entityName, f))
.map(f -> observer.fireSelectField(entityName, f))
.collect(Collectors.toList());
List<Sort<?>> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, entityName))
.collect(toList());
Expand All @@ -93,7 +93,7 @@ private SelectQuery query(Params params, org.eclipse.jnosql.communication.query.
long limit = selectQuery.limit();
long skip = selectQuery.skip();
List<String> columns = selectQuery.fields().stream()
.map(f -> observer.fireField(entity, f))
.map(f -> observer.fireSelectField(entity, f))
.collect(Collectors.toList());

List<Sort<?>> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, entity)).collect(toList());
Expand All @@ -107,7 +107,7 @@ private SelectQuery query(Params params, org.eclipse.jnosql.communication.query.
}

private Sort<?> toSort(Sort<?> sort, CommunicationObserverParser observer, String entity) {
return Sort.of(observer.fireField(entity, sort.property()),
return Sort.of(observer.fireSortProperty(entity, sort.property()),
sort.isAscending()? Direction.ASC: Direction.DESC, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private UpdateQuery getQuery(Params params, CommunicationObserverParser observer

List<Element> set = new ArrayList<>();
for (UpdateItem updateItem : updateQuery.set()) {
var field = observer.fireField(entity, updateItem.name());
var field = observer.fireSelectField(entity, updateItem.name());
var value = Values.get(updateItem.value(), params);
set.add(Element.of(field, value));
}
Expand All @@ -91,7 +91,7 @@ private UpdateQuery getQuery(String query, CommunicationObserverParser observer)
List<Element> set = new ArrayList<>();

for (UpdateItem updateItem : updateQuery.set()) {
var field = observer.fireField(entity, updateItem.name());
var field = observer.fireSelectField(entity, updateItem.name());
var value = Values.get(updateItem.value(), params);
set.add(Element.of(field, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public int numberOfElements() {

@Override
public boolean hasNext() {
return true;
return hasContent();
}

@Override
public boolean hasPrevious() {
return true;
return hasContent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.eclipse.jnosql.mapping.core.repository;



import jakarta.data.page.PageRequest;
import org.eclipse.jnosql.mapping.PreparedStatement;

import java.lang.reflect.Method;
Expand All @@ -25,20 +25,23 @@
/**
* This instance has the information to run the JNoSQL native query at {@link jakarta.data.repository.CrudRepository}
*/
public final class DynamicQueryMethodReturn implements MethodDynamicExecutable {
public final class DynamicQueryMethodReturn<T> implements MethodDynamicExecutable {


private final Method method;
private final Object[] args;
private final Class<?> typeClass;
private final Function<String, PreparedStatement> prepareConverter;
private final PageRequest pageRequest;

private DynamicQueryMethodReturn(Method method, Object[] args, Class<?> typeClass,
Function<String, PreparedStatement> prepareConverter) {
Function<String, PreparedStatement> prepareConverter,
PageRequest pageRequest) {
this.method = method;
this.args = args;
this.typeClass = typeClass;
this.prepareConverter = prepareConverter;
this.pageRequest = pageRequest;
}

Method method() {
Expand All @@ -57,57 +60,66 @@ Function<String, PreparedStatement> prepareConverter() {
return prepareConverter;
}

PageRequest pageRequest() {
return pageRequest;
}

boolean hasPagination() {
return pageRequest != null;
}

public static DynamicQueryMethodReturnBuilder builder() {
return new DynamicQueryMethodReturnBuilder();
public static <T> DynamicQueryMethodReturnBuilder<T> builder() {
return new DynamicQueryMethodReturnBuilder<>();
}

@Override
public Object execute() {
return DynamicReturnConverter.INSTANCE.convert(this);
}

public static final class DynamicQueryMethodReturnBuilder {
public static final class DynamicQueryMethodReturnBuilder<T> {

private Method method;

private Object[] args;

private Class<?> typeClass;

private Function<String, PreparedStatement> prepareConverter;
private PageRequest pageRequest;

private DynamicQueryMethodReturnBuilder() {
}

public DynamicQueryMethodReturnBuilder withMethod(Method method) {
public DynamicQueryMethodReturnBuilder<T> method(Method method) {
this.method = method;
return this;
}

public DynamicQueryMethodReturnBuilder withArgs(Object[] args) {
public DynamicQueryMethodReturnBuilder<T> args(Object[] args) {
if(args != null) {
this.args = args.clone();
}
return this;
}

public DynamicQueryMethodReturnBuilder withTypeClass(Class<?> typeClass) {
public DynamicQueryMethodReturnBuilder<T> typeClass(Class<?> typeClass) {
this.typeClass = typeClass;
return this;
}

public DynamicQueryMethodReturnBuilder withPrepareConverter(Function<String, PreparedStatement> prepareConverter) {
public DynamicQueryMethodReturnBuilder<T> prepareConverter(Function<String, PreparedStatement> prepareConverter) {
this.prepareConverter = prepareConverter;
return this;
}

public DynamicQueryMethodReturn build() {
public DynamicQueryMethodReturnBuilder<T> pageRequest(PageRequest pageRequest) {
this.pageRequest = pageRequest;
return this;
}

public DynamicQueryMethodReturn<T> build() {
Objects.requireNonNull(method, "method is required");
Objects.requireNonNull(typeClass, "typeClass is required");
Objects.requireNonNull(prepareConverter, "prepareConverter is required");

return new DynamicQueryMethodReturn(method, args, typeClass, prepareConverter);
return new DynamicQueryMethodReturn<>(method, args, typeClass, prepareConverter, pageRequest);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public static <T> DefaultDynamicReturnBuilder<T> builder() {
* A builder of {@link DynamicReturn}
* @param <T> the type
*/
@SuppressWarnings("rawtypes")
public static final class DefaultDynamicReturnBuilder<T> {

private Class<?> classSource;
Expand All @@ -251,7 +252,7 @@ private DefaultDynamicReturnBuilder() {
* @param classSource set the classSource
* @return the instance
*/
public DefaultDynamicReturnBuilder withClassSource(Class<?> classSource) {
public DefaultDynamicReturnBuilder classSource(Class<?> classSource) {
this.classSource = classSource;
return this;
}
Expand All @@ -260,7 +261,7 @@ public DefaultDynamicReturnBuilder withClassSource(Class<?> classSource) {
* @param methodSource the method source
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withMethodSource(Method methodSource) {
public DefaultDynamicReturnBuilder methodSource(Method methodSource) {
this.methodSource = methodSource;
return this;
}
Expand All @@ -269,7 +270,7 @@ public DefaultDynamicReturnBuilder withMethodSource(Method methodSource) {
* @param singleResult the singleResult source
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withSingleResult(Supplier<Optional<T>> singleResult) {
public DefaultDynamicReturnBuilder singleResult(Supplier<Optional<T>> singleResult) {
this.singleResult = singleResult;
return this;
}
Expand All @@ -278,7 +279,7 @@ public DefaultDynamicReturnBuilder withSingleResult(Supplier<Optional<T>> single
* @param result the list
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withResult(Supplier<Stream<T>> result) {
public DefaultDynamicReturnBuilder result(Supplier<Stream<T>> result) {
this.result = result;
return this;
}
Expand All @@ -287,7 +288,7 @@ public DefaultDynamicReturnBuilder withResult(Supplier<Stream<T>> result) {
* @param pageRequest the pagination
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withPagination(PageRequest pageRequest) {
public DefaultDynamicReturnBuilder pagination(PageRequest pageRequest) {
this.pageRequest = pageRequest;
return this;
}
Expand All @@ -296,7 +297,7 @@ public DefaultDynamicReturnBuilder withPagination(PageRequest pageRequest) {
* @param singleResultPagination the single result pagination
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withSingleResultPagination(Function<PageRequest, Optional<T>> singleResultPagination) {
public DefaultDynamicReturnBuilder singleResultPagination(Function<PageRequest, Optional<T>> singleResultPagination) {
this.singleResultPagination = singleResultPagination;
return this;
}
Expand All @@ -305,7 +306,7 @@ public DefaultDynamicReturnBuilder withSingleResultPagination(Function<PageReque
* @param listPagination the list pagination
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withStreamPagination(Function<PageRequest, Stream<T>> listPagination) {
public DefaultDynamicReturnBuilder streamPagination(Function<PageRequest, Stream<T>> listPagination) {
this.streamPagination = listPagination;
return this;
}
Expand All @@ -314,7 +315,7 @@ public DefaultDynamicReturnBuilder withStreamPagination(Function<PageRequest, St
* @param page the page
* @return the builder instance
*/
public DefaultDynamicReturnBuilder withPage(Function<PageRequest, Page<T>> page) {
public DefaultDynamicReturnBuilder page(Function<PageRequest, Page<T>> page) {
this.page = page;
return this;
}
Expand All @@ -325,7 +326,8 @@ public DefaultDynamicReturnBuilder withPage(Function<PageRequest, Page<T>> page)
* @return a new instance
* @throws NullPointerException when there is null attributes
*/
public DynamicReturn build() {
@SuppressWarnings({"rawtypes", "unchecked"})
public DynamicReturn<T> build() {
requireNonNull(classSource, "the class Source is required");
requireNonNull(methodSource, "the method Source is required");
requireNonNull(singleResult, "the single result supplier is required");
Expand Down
Loading