diff --git a/data-jdbc/src/test/java/io/micronaut/data/jdbc/oraclexe/OracleXEBookRepository.java b/data-jdbc/src/test/java/io/micronaut/data/jdbc/oraclexe/OracleXEBookRepository.java index abe675c3ca3..83be0b5b6f2 100644 --- a/data-jdbc/src/test/java/io/micronaut/data/jdbc/oraclexe/OracleXEBookRepository.java +++ b/data-jdbc/src/test/java/io/micronaut/data/jdbc/oraclexe/OracleXEBookRepository.java @@ -36,6 +36,9 @@ public OracleXEBookRepository(OracleXEAuthorRepository authorRepository) { super(authorRepository); } + @Query(value = "SELECT book_.* FROM book book_ ORDER BY book_.title ASC OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY") + public abstract List findBooks(int limit, int offset); + @Override @Query(value = "select * from book b where b.title = any (:arg0)", nativeQuery = true) public abstract List listNativeBooksWithTitleAnyCollection(@Nullable Collection arg0); diff --git a/data-jdbc/src/test/java/io/micronaut/data/jdbc/sqlserver/MSBookRepository.java b/data-jdbc/src/test/java/io/micronaut/data/jdbc/sqlserver/MSBookRepository.java index b1f57dcafc7..a877c349a8a 100644 --- a/data-jdbc/src/test/java/io/micronaut/data/jdbc/sqlserver/MSBookRepository.java +++ b/data-jdbc/src/test/java/io/micronaut/data/jdbc/sqlserver/MSBookRepository.java @@ -15,13 +15,21 @@ */ package io.micronaut.data.jdbc.sqlserver; +import io.micronaut.data.annotation.Query; import io.micronaut.data.jdbc.annotation.JdbcRepository; import io.micronaut.data.model.query.builder.sql.Dialect; +import io.micronaut.data.tck.entities.Book; import io.micronaut.data.tck.repositories.BookRepository; +import java.util.List; + @JdbcRepository(dialect = Dialect.SQL_SERVER) public abstract class MSBookRepository extends BookRepository { public MSBookRepository(MSAuthorRepository authorRepository) { super(authorRepository); } + + @Query(value = "SELECT book_.* FROM book book_ ORDER BY book_.title ASC OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY") + public abstract List findBooks(int limit, int offset); + } diff --git a/data-model/src/main/java/io/micronaut/data/model/query/builder/sql/SqlQueryBuilder2.java b/data-model/src/main/java/io/micronaut/data/model/query/builder/sql/SqlQueryBuilder2.java index fca050a7d6a..fe70c7ed604 100644 --- a/data-model/src/main/java/io/micronaut/data/model/query/builder/sql/SqlQueryBuilder2.java +++ b/data-model/src/main/java/io/micronaut/data/model/query/builder/sql/SqlQueryBuilder2.java @@ -1094,7 +1094,7 @@ public QueryResult buildPagination(@NonNull Pageable pageable) { case SQL_SERVER: // SQL server requires OFFSET always if (from == 0) { - builder.append("OFFSET ").append(0).append(" ROWS "); + builder.append("OFFSET 0 ROWS "); } // intentional fall through case ANSI: diff --git a/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/oraclexe/OracleXEBookRepository.java b/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/oraclexe/OracleXEBookRepository.java index 48d0959c5a4..5b7f9bff6c1 100644 --- a/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/oraclexe/OracleXEBookRepository.java +++ b/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/oraclexe/OracleXEBookRepository.java @@ -35,6 +35,9 @@ public OracleXEBookRepository(OracleXEAuthorRepository authorRepository) { super(authorRepository); } + @Query(value = "SELECT book_.* FROM book book_ ORDER BY book_.title ASC OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY") + public abstract List findBooks(int limit, int offset); + @Override @Query(value = "select * from book b where b.title = any (:arg0)", nativeQuery = true) public abstract List listNativeBooksWithTitleAnyCollection(@Nullable Collection arg0); diff --git a/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/sqlserver/MSBookRepository.java b/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/sqlserver/MSBookRepository.java index 6b5af793bc7..d19804f661d 100644 --- a/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/sqlserver/MSBookRepository.java +++ b/data-r2dbc/src/test/java/io/micronaut/data/r2dbc/sqlserver/MSBookRepository.java @@ -15,13 +15,20 @@ */ package io.micronaut.data.r2dbc.sqlserver; +import io.micronaut.data.annotation.Query; import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.r2dbc.annotation.R2dbcRepository; +import io.micronaut.data.tck.entities.Book; import io.micronaut.data.tck.repositories.BookRepository; +import java.util.List; + @R2dbcRepository(dialect = Dialect.SQL_SERVER) public abstract class MSBookRepository extends BookRepository { public MSBookRepository(MSAuthorRepository authorRepository) { super(authorRepository); } + + @Query(value = "SELECT book_.* FROM book book_ ORDER BY book_.title ASC OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY") + public abstract List findBooks(int limit, int offset); } diff --git a/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy b/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy index 9469c629338..d91526bb3ae 100644 --- a/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy +++ b/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy @@ -204,6 +204,23 @@ abstract class AbstractRepositorySpec extends Specification { return false } + void "test query with limit and offset" () { + given: + saveSampleBooks() + when: + def allBooks = bookRepository.findAll(Pageable.UNPAGED.order("title", Sort.Order.Direction.ASC)) + then: + allBooks.totalSize == 6 + when: + def books = bookRepository.findBooks(2, 2) + then: + books.collect { it.id } == allBooks.toList().subList(2, 4).collect { it.id } + when: + books = bookRepository.findBooks(Integer.MAX_VALUE, 0) + then: + books.collect { it.id } == allBooks.toList().collect { it.id } + } + void "test save and retrieve basic types"() { when: "we save a new book" def book = basicTypeRepository.save(new BasicTypes()) diff --git a/data-tck/src/main/java/io/micronaut/data/tck/repositories/BookRepository.java b/data-tck/src/main/java/io/micronaut/data/tck/repositories/BookRepository.java index bdb66567337..4c210c441e5 100644 --- a/data-tck/src/main/java/io/micronaut/data/tck/repositories/BookRepository.java +++ b/data-tck/src/main/java/io/micronaut/data/tck/repositories/BookRepository.java @@ -46,6 +46,9 @@ public abstract class BookRepository implements PageableRepository, @Override public abstract @NonNull Book save(@NonNull Book book); + @Query(value = "SELECT book_.* FROM book book_ ORDER BY book_.title ASC LIMIT :limit OFFSET :offset") + public abstract List findBooks(int limit, int offset); + @Join(value = "author", alias = "auth") public abstract Book queryByTitle(String title);