Skip to content

Commit

Permalink
Merge pull request #3119 from micronaut-projects/lo
Browse files Browse the repository at this point in the history
Test different combinations of limit and offset
  • Loading branch information
dstepanov authored Sep 11, 2024
2 parents a05fe25 + 9185991 commit 0539e29
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book> findBooks(int limit, int offset);

@Override
@Query(value = "select * from book b where b.title = any (:arg0)", nativeQuery = true)
public abstract List<Book> listNativeBooksWithTitleAnyCollection(@Nullable Collection<String> arg0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book> findBooks(int limit, int offset);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book> findBooks(int limit, int offset);

@Override
@Query(value = "select * from book b where b.title = any (:arg0)", nativeQuery = true)
public abstract List<Book> listNativeBooksWithTitleAnyCollection(@Nullable Collection<String> arg0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book> findBooks(int limit, int offset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public abstract class BookRepository implements PageableRepository<Book, Long>,
@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<Book> findBooks(int limit, int offset);

@Join(value = "author", alias = "auth")
public abstract Book queryByTitle(String title);

Expand Down

0 comments on commit 0539e29

Please sign in to comment.