implement support for requestTotals() in processor

also fix up some newline handling
This commit is contained in:
Gavin King 2024-03-09 17:38:31 +01:00
parent 7773e28b2c
commit c3bbd193c8
4 changed files with 28 additions and 33 deletions

View File

@ -209,14 +209,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
void chainSessionEnd(boolean isUpdate, StringBuilder declaration) {
if ( isReactiveSession() ) {
declaration.append("\n\t})");
declaration.append("\t})");
// here we're checking for a boxed void and not Uni<Void> because the returnType has already
// been checked, and is ununi-ed
if ( isUpdate && returnTypeName != null && returnTypeName.equals(BOXED_VOID) ) {
declaration.append(".replaceWithVoid();");
declaration.append(".replaceWithVoid();\n");
}
else {
declaration.append(";");
declaration.append(";\n");
}
}
}
@ -317,7 +317,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
abstract boolean singleResult();
static void closingBrace(StringBuilder declaration) {
declaration.append("\n}");
declaration.append("}");
}
void unwrapQuery(StringBuilder declaration, boolean unwrapped) {
@ -391,9 +391,9 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
"\t\t\t\t\t\t.sortBy(pageRequest.sorts())\n" +
"\t\t\t\t\t\t.size(pageRequest.size())\n" +
"\t\t\t\t\t\t.page(pageRequest.page() + 1);\n" +
"\t\treturn new CursoredPageRecord<>( _results.getResultList(), _cursors, _totalResults, pageRequest,\n" +
"\t\treturn new CursoredPageRecord<>(_results.getResultList(), _cursors, _totalResults, pageRequest,\n" +
"\t\t\t\t_results.isLastPage() ? null : _page.afterKey(_results.getNextPage().getKey().toArray()),\n" +
"\t\t\t\t_results.isFirstPage() ? null : _page.beforeKey(_results.getPreviousPage().getKey().toArray()) );\n";
"\t\t\t\t_results.isFirstPage() ? null : _page.beforeKey(_results.getPreviousPage().getKey().toArray()));";
static final String MAKE_KEYED_PAGE
= "\tvar _unkeyedPage =\n" +
@ -404,16 +404,16 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
"\t\t\t\t\t.map(_cursor -> {\n" +
"\t\t\t\t\t\t@SuppressWarnings(\"unchecked\")\n" +
"\t\t\t\t\t\tvar _elements = (List<Comparable<?>>) _cursor.elements();\n" +
"\t\t\t\t\t\treturn switch ( pageRequest.mode() ) {\n" +
"\t\t\t\t\t\treturn switch (pageRequest.mode()) {\n" +
"\t\t\t\t\t\t\tcase CURSOR_NEXT -> _unkeyedPage.withKey(_elements, KEY_OF_LAST_ON_PREVIOUS_PAGE);\n" +
"\t\t\t\t\t\t\tcase CURSOR_PREVIOUS -> _unkeyedPage.withKey(_elements, KEY_OF_FIRST_ON_NEXT_PAGE);\n" +
"\t\t\t\t\t\t\tdefault -> _unkeyedPage;\n" +
"\t\t\t\t\t\t};\n" +
"\t\t\t\t\t}).orElse(_unkeyedPage);\n";
"\t\t\t\t\t}).orElse(_unkeyedPage);";
void createQuery(StringBuilder declaration) {}
void setParameters(StringBuilder declaration, List<String> paramTypes) {}
void setParameters(StringBuilder declaration, List<String> paramTypes, String indent) {}
void tryReturn(StringBuilder declaration, List<String> paramTypes, @Nullable String containerType) {
if ( isJakartaCursoredPage(containerType) ) {
@ -465,12 +465,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
private void totalResults(StringBuilder declaration, List<String> paramTypes) {
declaration
.append("\tlong _totalResults = ");
.append("\tlong _totalResults = \n\t\t\t\t")
.append(parameterName(JD_PAGE_REQUEST, paramTypes, paramNames))
.append(".requestTotal()\n\t\t\t\t\t\t? ");
createQuery( declaration );
setParameters( declaration, paramTypes );
setParameters( declaration, paramTypes, "\t\t\t\t\t");
unwrapQuery( declaration, !isUsingEntityManager() );
declaration
.append("\t\t\t.getResultCount();\n");
.append("\t\t\t\t\t\t\t\t.getResultCount()\n\t\t\t\t\t\t: -1;\n");
}
void collectOrdering(StringBuilder declaration, List<String> paramTypes) {
@ -679,6 +681,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
}
}
}
declaration.append('\n');
}
private static String parameterName(String paramType, List<String> paramTypes, List<String> paramNames) {

View File

@ -85,11 +85,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
declaration
.append("\t\t\t.load(")
.append(paramName)
.append(");");
if (dataRepository) {
declaration
.append("\n");
}
.append(");\n");
}
private void findWithNoFetchProfiles(StringBuilder declaration) {
@ -98,11 +94,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
.append(annotationMetaEntity.importType(entity))
.append(".class, ")
.append(paramName)
.append(");");
if (dataRepository) {
declaration
.append("\n");
}
.append(");\n");
}
private static void nullCheck(StringBuilder declaration, String parameterName) {

View File

@ -82,7 +82,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
}
}
declaration
.append("\t\t\t.load();");
.append("\t\t\t.load();\n");
}
private void findReactively(StringBuilder declaration) {
@ -130,7 +130,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
if (composite) {
declaration.append("\n\t\t\t)\n\t");
}
declaration.append(");");
declaration.append(");\n");
}
private boolean isComposite() {

View File

@ -89,7 +89,7 @@ public class QueryMethod extends AbstractQueryMethod {
tryReturn( declaration, paramTypes, containerType );
castResult( declaration, returnType );
createQuery( declaration );
setParameters( declaration, paramTypes );
setParameters( declaration, paramTypes, "");
handlePageParameters( declaration, paramTypes, containerType );
boolean unwrapped = specialNeeds( declaration );
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
@ -127,7 +127,8 @@ public class QueryMethod extends AbstractQueryMethod {
&& isUsingEntityManager() ) {
// EntityManager.createNativeQuery() does not return TypedQuery,
// so we need to cast to the entity type
declaration.append("(")
declaration
.append("(")
.append(returnType)
.append(") ");
}
@ -139,10 +140,11 @@ public class QueryMethod extends AbstractQueryMethod {
.append("\t\t\t")
.append(".executeUpdate()");
if ( "boolean".equals(returnTypeName) ) {
declaration.append(" > 0");
declaration
.append(" > 0");
}
declaration
.append(';');
.append(";\n");
}
else {
final boolean mustUnwrap =
@ -150,23 +152,21 @@ public class QueryMethod extends AbstractQueryMethod {
|| isNative && returnTypeName != null;
executeSelect( declaration, paramTypes, containerType, unwrapped, mustUnwrap );
}
if ( dataRepository ) {
declaration
.append('\n');
}
}
@Override
void setParameters(StringBuilder declaration, List<String> paramTypes) {
void setParameters(StringBuilder declaration, List<String> paramTypes, String indent) {
for ( int i = 0; i < paramNames.size(); i++ ) {
final String paramName = paramNames.get(i);
final String paramType = paramTypes.get(i);
if ( !isSpecialParam(paramType) ) {
final int ordinal = i+1;
if ( queryString.contains(":" + paramName) ) {
declaration.append(indent);
setNamedParameter( declaration, paramName );
}
else if ( queryString.contains("?" + ordinal) ) {
declaration.append(indent);
setOrdinalParameter( declaration, ordinal, paramName );
}
}