Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/pagination' into feature…
Browse files Browse the repository at this point in the history
…/pagination

# Conflicts:
#	plume-db-querydsl/README.md
  • Loading branch information
amanteaux committed Sep 19, 2024
2 parents cdd22ae + 2c2a7c6 commit abe0723
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
32 changes: 16 additions & 16 deletions plume-db-querydsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ This module helps integrate [Querydsl SQL](https://github.com/querydsl/querydsl/
with [Plume Database](https://github.com/Coreoz/Plume/tree/master/plume-db).
It contains mainly:
- `TransactionManagerQuerydsl`: the main class of this module; it will
read the configuration, initialize the SQL connection pool
and provide helper methods to create Querydsl queries,
read the configuration, initialize the SQL connection pool
and provide helper methods to create Querydsl queries,
- The generic DAO `CrudDaoQuerydsl` for CRUD operations.

Querydsl queries can be created:
- **without a `Connection`**: that means that the query will be executed with a connection
from the SQL connection pool. The `Connection` object will be automaticely released in the pool
once the query is executed.
from the SQL connection pool. The `Connection` object will be automaticely released in the pool
once the query is executed.
- **with a `Connection`**: that means that the query will be executed on this supplied connection.
This mode is almost always used when a **transaction** is needed:
This mode is almost always used when a **transaction** is needed:
```java
transactionManager.execute(connection -> {
transactionManager.insert(QTable.table, connection).populate(bean).execute();
transactionManager.insert(QTable.table, connection).populate(bean).execute();
transactionManager.delete(QTable.table, connection).where(predicate).execute();
// the connection is set to autocommit=false and will be commited at the end of the lambda
// the connection is set to autocommit=false and will be commited at the end of the lambda
});
```

Expand All @@ -35,15 +35,15 @@ Installation
**Maven**:
```xml
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-querydsl</artifactId>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-querydsl</artifactId>
</dependency>
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-querydsl-codegen</artifactId>
<optional>true</optional>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-querydsl-codegen</artifactId>
<optional>true</optional>
</dependency>
<!-- do not forget to also include the database driver -->
<!-- do not forget to also include the database driver -->
```

**Guice**: `install(new GuiceQuerydslModule());`
Expand Down Expand Up @@ -71,7 +71,7 @@ The `SqlPaginatedQuery` class provides a robust and flexible mechanism for pagin

- `Page<U>`: A `Page` contains a list of results, total count of items, total number of pages, and a flag to indicate if there are more pages available.
- `Slice<U>`: A `Slice` contains a list of results and a flag to indicate if there are more items to be fetched, without calculating the total number of items or pages.

So using slices will be more efficient than using pages, though the impact will depend on the number of rows to count.
Under the hood:
- When fetching a page, Querydsl will attempt to execute the fetch request and the count request in the same SQL query, if it is not supported by the database, it will execute two queries.
Expand All @@ -83,7 +83,7 @@ Other features:

### Working with Pagination from a WebService:
First, you need to create a translation between the API sort key and a table column.
This can be done like this:
This can be done like this:

```java
@Getter
Expand Down Expand Up @@ -130,7 +130,7 @@ public Page<AdminUser> searchUsers(
}
```

Then apply the pagination from the API call with `SqlPaginatedQuery` :
Then apply the pagination from the API call with `SqlPaginatedQuery` :

```java
public Page<AdminUser> searchUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Slice<U> fetchSlice(
) {
List<U> slicedQueryResults = this.sqlQuery
.offset(Pages.offset(pageNumber, pageSize))
.limit(pageSize + 1)
.limit(pageSize + 1L)
.fetch();

boolean hasMore = slicedQueryResults.size() > pageSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.coreoz.plume.db.pagination;

import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.util.List;

public class SliceTest {

@Test
public void should_map_users() {
User user = new User(1L, "To fetch");
Slice<User> page = new Slice<>(
List.of(user),
true
);

Slice<String> userNames = page.map(User::name);

Assertions.assertThat(userNames.items().stream().findFirst()).isNotEmpty();
Assertions.assertThat(userNames.items().stream().findFirst().get()).contains("To fetch");
}

private record User(long id, String name) {
}
}

0 comments on commit abe0723

Please sign in to comment.