Skip to content

Commit

Permalink
Merge pull request #2943 from tsegismont/pg-client-iterable
Browse files Browse the repository at this point in the history
PgRowSet is now an Iterable<Row> in Axle shim
  • Loading branch information
gsmet committed Jun 24, 2019
2 parents 2fad44b + 4f1ceb4 commit 8d22a0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
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

0 comments on commit 8d22a0f

Please sign in to comment.