Skip to content

Commit

Permalink
Optional sort in REST Data Panache
Browse files Browse the repository at this point in the history
  • Loading branch information
Gytis Trikleris committed May 17, 2021
1 parent c33d746 commit 5b516a3
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public interface DataAccessImplementor {
*/
ResultHandle findById(BytecodeCreator creator, ResultHandle id);

/**
* Find all entities.
*
* @param creator Bytecode creator that should be used for implementation.
* @param page Page instance that should be used in a query. Might be null if pagination is disabled.
* @return Entity list
*/
ResultHandle findAll(BytecodeCreator creator, ResultHandle page);

/**
* Find all entities.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
id);
}

/**
* Implements <code>Entity.findAll().page(page).list()</code>
*/
@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeStaticMethod(ofMethod(entityClassName, "findAll", PanacheQuery.class));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

/**
* Implements <code>Entity.findAll(sort).page(page).list()</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
getRepositoryInstance(creator), id);
}

/**
* Implements <code>repository.findAll().page(page).list()</code>
*/
@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeInterfaceMethod(
ofMethod(PanacheRepositoryBase.class, "findAll", PanacheQuery.class), getRepositoryInstance(creator));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query, page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

/**
* Implements <code>repository.findAll(sort).page(page).list()</code>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.hibernate.orm.rest.data.panache.deployment;

import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -8,6 +10,7 @@
import org.jboss.jandex.FieldInfo;
import org.jboss.logging.Logger;

import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
Expand Down Expand Up @@ -65,7 +68,13 @@ private void implementList(ClassCreator classCreator, DataAccessImplementor data
MethodCreator methodCreator = classCreator.getMethodCreator("list", List.class, Page.class, Sort.class);
ResultHandle page = methodCreator.getMethodParam(0);
ResultHandle sort = methodCreator.getMethodParam(1);
methodCreator.returnValue(dataAccessImplementor.findAll(methodCreator, page, sort));
ResultHandle columns = methodCreator.invokeVirtualMethod(ofMethod(Sort.class, "getColumns", List.class), sort);
ResultHandle isEmptySort = methodCreator.invokeInterfaceMethod(ofMethod(List.class, "isEmpty", boolean.class), columns);

BranchResult isEmptySortBranch = methodCreator.ifTrue(isEmptySort);
isEmptySortBranch.trueBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.trueBranch(), page));
isEmptySortBranch.falseBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.falseBranch(), page, sort));

methodCreator.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public interface DataAccessImplementor {
*/
ResultHandle findById(BytecodeCreator creator, ResultHandle id);

/**
* Find all entities.
*
* @param creator Bytecode creator that should be used for implementation.
* @param page Page instance that should be used in a query. Might be null if pagination is disabled.
* @return Entity list
*/
ResultHandle findAll(BytecodeCreator creator, ResultHandle page);

/**
* Find all entities.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
ofMethod(entityClassName, "findById", PanacheMongoEntityBase.class, Object.class), id);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeStaticMethod(ofMethod(entityClassName, "findAll", PanacheQuery.class));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page, ResultHandle sort) {
ResultHandle query = creator.invokeStaticMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
getRepositoryInstance(creator), id);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeInterfaceMethod(
ofMethod(PanacheMongoRepositoryBase.class, "findAll", PanacheQuery.class),
getRepositoryInstance(creator));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page, ResultHandle sort) {
ResultHandle query = creator.invokeInterfaceMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.quarkus.mongodb.rest.data.panache.deployment;

import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import org.jboss.jandex.FieldInfo;
import org.jboss.logging.Logger;

import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
Expand Down Expand Up @@ -60,7 +63,13 @@ private void implementList(ClassCreator classCreator, DataAccessImplementor data
MethodCreator methodCreator = classCreator.getMethodCreator("list", List.class, Page.class, Sort.class);
ResultHandle page = methodCreator.getMethodParam(0);
ResultHandle sort = methodCreator.getMethodParam(1);
methodCreator.returnValue(dataAccessImplementor.findAll(methodCreator, page, sort));
ResultHandle columns = methodCreator.invokeVirtualMethod(ofMethod(Sort.class, "getColumns", List.class), sort);
ResultHandle isEmptySort = methodCreator.invokeInterfaceMethod(ofMethod(List.class, "isEmpty", boolean.class), columns);

BranchResult isEmptySortBranch = methodCreator.ifTrue(isEmptySort);
isEmptySortBranch.trueBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.trueBranch(), page));
isEmptySortBranch.falseBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.falseBranch(), page, sort));

methodCreator.close();
}

Expand Down

0 comments on commit 5b516a3

Please sign in to comment.