-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Coreoz/feature/pagination
implement PaginatedQueries for QueryDsl
- Loading branch information
Showing
15 changed files
with
686 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e-db-querydsl/src/main/java/com/coreoz/plume/db/querydsl/dagger/DaggerQuerydslModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...-db-querydsl/src/main/java/com/coreoz/plume/db/querydsl/pagination/SqlPaginatedQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.coreoz.plume.db.querydsl.pagination; | ||
|
||
import com.coreoz.plume.db.pagination.Page; | ||
import com.coreoz.plume.db.pagination.Pages; | ||
import com.coreoz.plume.db.pagination.Slice; | ||
import com.querydsl.core.QueryResults; | ||
import com.querydsl.core.types.Expression; | ||
import com.querydsl.core.types.Order; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import com.querydsl.sql.SQLQuery; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.List; | ||
|
||
/** | ||
* Paginated query implementation with Querydsl | ||
* <br> | ||
* @param <U> The type of elements contained in the request. | ||
* <br> | ||
* Usage example: | ||
* <code><pre> | ||
* public Page<User> fetchUsers() { | ||
* return SqlPaginatedQuery | ||
* .fromQuery( | ||
* this.transactionManagerQuerydsl.selectQuery() | ||
* .select(QUser.user) | ||
* .from(QUser.user) | ||
* ) | ||
* .withSort(QUser.user.name, Order.DESC) | ||
* .fetchPage(1, 10); | ||
* } | ||
* </pre></code> | ||
*/ | ||
public class SqlPaginatedQuery<U> { | ||
|
||
private final SQLQuery<U> sqlQuery; | ||
|
||
private SqlPaginatedQuery(SQLQuery<U> sqlQuery) { | ||
this.sqlQuery = sqlQuery; | ||
} | ||
|
||
public static <U> SqlPaginatedQuery<U> fromQuery(SQLQuery<U> sqlQuery) { | ||
return new SqlPaginatedQuery<>(sqlQuery); | ||
} | ||
|
||
@Nonnull | ||
public <E extends Comparable<E>> SqlPaginatedQuery<U> withSort( | ||
@Nonnull Expression<E> expression, | ||
@Nonnull Order sortDirection | ||
) { | ||
return new SqlPaginatedQuery<>( | ||
sqlQuery | ||
.orderBy( | ||
new OrderSpecifier<>( | ||
sortDirection, | ||
expression | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Fetches a page of the SQL query provided | ||
* @param pageNumber the number of the page queried (must be >= 1) | ||
* @param pageSize the size of the page queried (must be >= 1) | ||
* @return the corresponding page | ||
*/ | ||
@Nonnull | ||
public Page<U> fetchPage( | ||
int pageNumber, | ||
int pageSize | ||
) { | ||
QueryResults<U> paginatedQueryResults = this.sqlQuery | ||
.offset(Pages.offset(pageNumber, pageSize)) | ||
.limit(pageSize) | ||
.fetchResults(); | ||
|
||
return new Page<>( | ||
paginatedQueryResults.getResults(), | ||
paginatedQueryResults.getTotal(), | ||
Pages.pageCount(pageSize, paginatedQueryResults.getTotal()), | ||
pageNumber, | ||
Pages.hasMore(pageNumber, pageSize, paginatedQueryResults.getTotal()) | ||
); | ||
} | ||
|
||
/** | ||
* Fetches a slice of the SQL query provided | ||
* @param pageNumber the number of the page queried (must be >= 1) | ||
* @param pageSize the size of the page queried (must be >= 1) | ||
* @return the corresponding slice | ||
*/ | ||
@Nonnull | ||
public Slice<U> fetchSlice( | ||
int pageNumber, | ||
int pageSize | ||
) { | ||
List<U> slicedQueryResults = this.sqlQuery | ||
.offset(Pages.offset(pageNumber, pageSize)) | ||
.limit(pageSize + 1L) | ||
.fetch(); | ||
|
||
boolean hasMore = slicedQueryResults.size() > pageSize; | ||
|
||
// Trim the results to the required size (if needed) | ||
List<U> items = hasMore ? slicedQueryResults.subList(0, pageSize) : slicedQueryResults; | ||
|
||
return new Slice<>( | ||
items, | ||
hasMore | ||
); | ||
} | ||
} |
Oops, something went wrong.