Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PgRowSet is now an Iterable<Row> in Axle shim #2943

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs/src/main/asciidoc/reactive-postgres-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ CompletionStage<PgRowSet> rowSet = client.query("SELECT id, name FROM fruits ORD
----

When the operation completes, we will get a `PgRowSet` that has all the rows buffered in memory.
It can be traversed with an `Iterator`:
As an `java.lang.Iterable<Row>`, it can be traversed with a _for-each_ loop:

[source,java]
----
CompletionStage<List<Fruit>> fruits = rowSet.thenApply(pgRowSet -> {
List<Fruit> list = new ArrayList<>(pgRowSet.size());
PgIterator pgIterator = pgRowSet.iterator();
while (pgIterator.hasNext()) {
list.add(from(pgIterator.next()));
for (Row row : pgRowSet) {
list.add(from(row));
}
return list;
});
Expand All @@ -234,9 +233,8 @@ Putting it all together, the `Fruit.findAll` method looks like:
public static CompletionStage<List<Fruit>> findAll(PgPool client) {
return client.query("SELECT id, name FROM fruits ORDER BY name ASC").thenApply(pgRowSet -> {
List<Fruit> list = new ArrayList<>(pgRowSet.size());
PgIterator pgIterator = pgRowSet.iterator();
while (pgIterator.hasNext()) {
list.add(from(pgIterator.next()));
for (Row row : pgRowSet) {
list.add(from(row));
}
return list;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.reactiverse.axle.pgclient.PgIterator;
import io.reactiverse.axle.pgclient.PgPool;
import io.reactiverse.axle.pgclient.Row;
import io.vertx.core.json.JsonArray;
Expand All @@ -35,16 +34,13 @@ void setupDb() {
@GET
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<JsonArray> listFruits() {
return client.query("SELECT * FROM fruits")
.thenApply(pgRowSet -> {
JsonArray jsonArray = new JsonArray();
PgIterator iterator = pgRowSet.iterator();
while (iterator.hasNext()) {
Row row = iterator.next();
jsonArray.add(toJson(row));
}
return jsonArray;
});
return client.query("SELECT * FROM fruits").thenApply(pgRowSet -> {
JsonArray jsonArray = new JsonArray();
for (Row row : pgRowSet) {
jsonArray.add(toJson(row));
}
return jsonArray;
});
}

private JsonObject toJson(Row row) {
Expand Down