Skip to content

Commit

Permalink
CursoredPage not supported with findAll and PredicateSpecification (#…
Browse files Browse the repository at this point in the history
…3007)

* CursoredPage not supported with findAll and PredicateSpecification
  • Loading branch information
radovanradic authored Jul 4, 2024
1 parent 732aa6b commit 6cb04c8
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,4 @@ class H2CursoredPaginationSpec extends AbstractCursoredPageSpec {
return br
}

@Override
void init() {
pr.deleteAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
package io.micronaut.data.jdbc.mysql

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class MysqlCursoredPaginationSpec extends AbstractCursoredPageSpec implements MySQLTestPropertyProvider {

@Shared @AutoCleanup ApplicationContext context

@Memoized
@Override
PersonRepository getPersonRepository() {
Expand All @@ -39,9 +34,4 @@ class MysqlCursoredPaginationSpec extends AbstractCursoredPageSpec implements My
return context.getBean(MySqlBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
package io.micronaut.data.jdbc.oraclexe

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class OracleXECursoredPaginationSpec extends AbstractCursoredPageSpec implements OracleTestPropertyProvider {

@Shared @AutoCleanup ApplicationContext context

@Override
@Memoized
PersonRepository getPersonRepository() {
Expand All @@ -39,9 +34,4 @@ class OracleXECursoredPaginationSpec extends AbstractCursoredPageSpec implements
return context.getBean(OracleXEBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
package io.micronaut.data.jdbc.postgres

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Ignore
import spock.lang.Shared

@Ignore("Causes error: 'FATAL: sorry, too many clients already'")
class PostgresCursoredPaginationSpec extends AbstractCursoredPageSpec implements PostgresTestPropertyProvider {
@Shared @AutoCleanup ApplicationContext context

@Memoized
@Override
Expand All @@ -40,8 +34,4 @@ class PostgresCursoredPaginationSpec extends AbstractCursoredPageSpec implements
return context.getBean(PostgresBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(getProperties())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
*/
package io.micronaut.data.jdbc.sqlserver

import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class SqlServerCursoredPaginationSpec extends AbstractCursoredPageSpec implements MSSQLTestPropertyProvider {

@Shared @AutoCleanup ApplicationContext context

@Override
PersonRepository getPersonRepository() {
return context.getBean(MSSQLPersonRepository)
Expand All @@ -36,8 +31,4 @@ class SqlServerCursoredPaginationSpec extends AbstractCursoredPageSpec implement
return context.getBean(MSBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@
package io.micronaut.data.r2dbc.h2

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.*
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import io.micronaut.data.tck.tests.AbstractRepositorySpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class H2CursoredPaginationSpec extends AbstractCursoredPageSpec implements H2TestPropertyProvider {

@Shared
@AutoCleanup
ApplicationContext context

@Memoized
@Override
PersonRepository getPersonRepository() {
Expand All @@ -40,9 +32,4 @@ class H2CursoredPaginationSpec extends AbstractCursoredPageSpec implements H2Tes
BookRepository getBookRepository() {
return context.getBean(H2BookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
package io.micronaut.data.r2dbc.mysql

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class MySqlCursoredPaginationSpec extends AbstractCursoredPageSpec implements MySqlTestPropertyProvider {

@Shared @AutoCleanup ApplicationContext context

@Memoized
@Override
PersonRepository getPersonRepository() {
Expand All @@ -39,9 +34,4 @@ class MySqlCursoredPaginationSpec extends AbstractCursoredPageSpec implements My
return context.getBean(MySqlBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
package io.micronaut.data.r2dbc.oraclexe

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Shared

class OracleXECursoredPaginationSpec extends AbstractCursoredPageSpec implements OracleXETestPropertyProvider {

@Shared @AutoCleanup ApplicationContext context

@Override
@Memoized
PersonRepository getPersonRepository() {
Expand All @@ -39,9 +34,4 @@ class OracleXECursoredPaginationSpec extends AbstractCursoredPageSpec implements
return context.getBean(OracleXEBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(properties)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
package io.micronaut.data.r2dbc.postgres

import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.data.tck.repositories.BookRepository
import io.micronaut.data.tck.repositories.PersonRepository
import io.micronaut.data.tck.tests.AbstractCursoredPageSpec
import spock.lang.AutoCleanup
import spock.lang.Ignore
import spock.lang.Shared

//@Ignore("Causes error: 'FATAL: sorry, too many clients already'")
class PostgresCursoredPaginationSpec extends AbstractCursoredPageSpec implements PostgresTestPropertyProvider {
@Shared @AutoCleanup ApplicationContext context

@Memoized
@Override
Expand All @@ -40,8 +34,4 @@ class PostgresCursoredPaginationSpec extends AbstractCursoredPageSpec implements
return context.getBean(PostgresBookRepository)
}

@Override
void init() {
context = ApplicationContext.run(getProperties())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
@Internal
public abstract class AbstractSpecificationInterceptor<T, R> extends AbstractQueryInterceptor<T, R> {

protected static final String PREPARED_QUERY_KEY = "PREPARED_QUERY";

protected final CriteriaRepositoryOperations criteriaRepositoryOperations;
private final Map<RepositoryMethodKey, QueryBuilder> sqlQueryBuilderForRepositories = new ConcurrentHashMap<>();
private final Map<RepositoryMethodKey, Set<JoinPath>> methodsJoinPaths = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -150,7 +152,9 @@ protected final Iterable<?> findAll(RepositoryMethodKey methodKey, MethodInvocat
}
return criteriaRepositoryOperations.findAll(query);
}
return operations.findAll(preparedQueryForCriteria(methodKey, context, type, methodJoinPaths));
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, type, methodJoinPaths);
context.setAttribute(PREPARED_QUERY_KEY, preparedQuery);
return operations.findAll(preparedQuery);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.data.intercept.RepositoryMethodKey;
import io.micronaut.data.model.CursoredPage;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.operations.RepositoryOperations;
import io.micronaut.data.runtime.operations.internal.sql.DefaultSqlPreparedQuery;

import java.util.List;

Expand Down Expand Up @@ -68,7 +71,18 @@ public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<O
count = count(methodKey, context);
}

Page page = Page.of(resultList, getPageable(context), count);
Page page;
if (pageable.getMode() == Pageable.Mode.OFFSET) {
page = Page.of(resultList, pageable, count);
} else {
PreparedQuery preparedQuery = (PreparedQuery) context.getAttribute(PREPARED_QUERY_KEY).orElse(null);
if (preparedQuery instanceof DefaultSqlPreparedQuery<?, ?> sqlPreparedQuery) {
List<Pageable.Cursor> cursors = sqlPreparedQuery.createCursors(resultList, pageable);
page = CursoredPage.of(resultList, pageable, cursors, count);
} else {
throw new UnsupportedOperationException("Only offset pageable mode is supported by this query implementation");
}
}
Class<Object> rt = context.getReturnType().getType();
if (rt.isInstance(page)) {
return page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.Pageable.Mode;
import io.micronaut.data.model.query.JoinPath;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.operations.RepositoryOperations;
import io.micronaut.data.operations.reactive.ReactiveCapableRepository;
import io.micronaut.data.operations.reactive.ReactiveCriteriaCapableRepository;
Expand Down Expand Up @@ -84,7 +85,9 @@ protected final Publisher<Object> findAllReactive(RepositoryMethodKey methodKey,
}
return reactiveCriteriaOperations.findAll(criteriaQuery);
}
return reactiveOperations.findAll(preparedQueryForCriteria(methodKey, context, type, methodJoinPaths));
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, type, methodJoinPaths);
context.setAttribute(PREPARED_QUERY_KEY, preparedQuery);
return (Publisher<Object>) reactiveOperations.findAll(preparedQuery);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.data.intercept.RepositoryMethodKey;
import io.micronaut.data.model.CursoredPage;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.operations.RepositoryOperations;
import io.micronaut.data.runtime.operations.internal.sql.DefaultSqlPreparedQuery;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
* Runtime implementation of {@code Publisher<Page> find(Specification, Pageable)}.
*
Expand Down Expand Up @@ -61,12 +66,26 @@ public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<O
.collectList()
.flatMap(
list -> pageable.requestTotal()
? Mono.from(countReactive(methodKey, context)).map(count -> Page.of(list, getPageable(context), count))
: Mono.just(Page.of(list, getPageable(context), null))
? Mono.from(countReactive(methodKey, context)).map(count -> getPage(list, pageable, count, context))
: Mono.just(getPage(list, pageable, null, context))
);
}
return Publishers.convertPublisher(conversionService, result, context.getReturnType().getType());

}

private Page getPage(List<Object> list, Pageable pageable, Long count, MethodInvocationContext<Object, Object> context) {
Page page;
if (pageable.getMode() == Pageable.Mode.OFFSET) {
page = Page.of(list, pageable, count);
} else {
PreparedQuery preparedQuery = (PreparedQuery) context.getAttribute(PREPARED_QUERY_KEY).orElse(null);
if (preparedQuery instanceof DefaultSqlPreparedQuery<?, ?> sqlPreparedQuery) {
List<Pageable.Cursor> cursors = sqlPreparedQuery.createCursors(list, pageable);
page = CursoredPage.of(list, pageable, cursors, count);
} else {
throw new UnsupportedOperationException("Only offset pageable mode is supported by this query implementation");
}
}
return page;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
import io.micronaut.data.annotation.Query;
import io.micronaut.data.intercept.RepositoryMethodKey;
import io.micronaut.data.intercept.reactive.FindPageReactiveInterceptor;
import io.micronaut.data.model.CursoredPage;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.operations.RepositoryOperations;
import io.micronaut.data.runtime.operations.internal.sql.DefaultSqlPreparedQuery;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

import java.util.List;

/**
* Default implementation of {@link FindPageReactiveInterceptor}.
*
Expand All @@ -52,8 +57,19 @@ public Publisher<?> interceptPublisher(RepositoryMethodKey methodKey, MethodInvo
return Flux.from(reactiveOperations.findOne(countQuery))
.flatMap(total -> {
Flux<Object> resultList = Flux.from(reactiveOperations.findAll(preparedQuery));
return resultList.collectList().map(list ->
Page.of(list, preparedQuery.getPageable(), total.longValue())
return resultList.collectList().map(list -> {
Pageable pageable = preparedQuery.getPageable();
Page page;
if (pageable.getMode() == Pageable.Mode.OFFSET) {
page = Page.of(list, pageable, total.longValue());
} else if (preparedQuery instanceof DefaultSqlPreparedQuery<?, ?> sqlPreparedQuery) {
List<Pageable.Cursor> cursors = sqlPreparedQuery.createCursors(list, pageable);
page = CursoredPage.of(list, pageable, cursors, total.longValue());
} else {
throw new UnsupportedOperationException("Only offset pageable mode is supported by this query implementation");
}
return page;
}
);
});
}
Expand Down
Loading

0 comments on commit 6cb04c8

Please sign in to comment.