Skip to content

Commit

Permalink
Merge pull request #536 from eclipse/select-query
Browse files Browse the repository at this point in the history
Enhance query with pagination at Repository
  • Loading branch information
otaviojava authored Aug 6, 2024
2 parents da4ef8c + caa7d53 commit 2a2362d
Show file tree
Hide file tree
Showing 36 changed files with 950 additions and 473 deletions.
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

0 comments on commit 2a2362d

Please sign in to comment.