Skip to content

Commit d7a4b28

Browse files
committed
Add example with export; rename methods
1 parent 2e0d57d commit d7a4b28

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

vertx-pg-client/src/main/java/examples/PgClientExamples.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import io.vertx.core.Future;
2121
import io.vertx.core.Vertx;
22+
import io.vertx.core.buffer.Buffer;
23+
import io.vertx.core.buffer.impl.BufferImpl;
2224
import io.vertx.core.json.JsonObject;
2325
import io.vertx.core.net.PemTrustOptions;
2426
import io.vertx.docgen.Source;
@@ -733,14 +735,26 @@ public void batchReturning(SqlClient client) {
733735
});
734736
}
735737

736-
public void copyFromStdinReturning(Vertx vertx, PgConnection client) {
737-
client.copyFrom(
738-
"COPY my_table FROM STDIN (FORMAT csv, HEADER)",
739-
vertx.fileSystem().readFile("path/to/file")
740-
).execute().onSuccess(res -> {
741-
Long rowsWritten = res.iterator().next().getLong("rowsWritten");
742-
System.out.println("rows affected: " + rowsWritten);
743-
});
738+
public void importDataToDb(Vertx vertx, PgConnection client) {
739+
vertx.fileSystem().readFile("path/to/file")
740+
.flatMap(bufferAsyncResult -> {
741+
return client.copyFrom(
742+
"COPY my_table FROM STDIN (FORMAT csv, HEADER)",
743+
bufferAsyncResult
744+
).execute();
745+
}).onSuccess(result -> {
746+
Long rowsWritten = result.iterator().next().getLong("rowsWritten");
747+
System.out.println("rows written: " + rowsWritten);
748+
});
749+
}
750+
751+
public void exportDataFromDb(Vertx vertx, PgConnection client) {
752+
Buffer buffer = new BufferImpl();
753+
String path = "path/to/file";
754+
client.copyTo("COPY my_table TO STDOUT (FORMAT csv, HEADER)", buffer)
755+
.andThen(res -> {
756+
vertx.fileSystem().writeFile("path/to/file.csv", buffer);
757+
}).onSuccess(res -> System.out.println("Data exported to " + path));
744758
}
745759

746760
public void pgBouncer(PgConnectOptions connectOptions) {

vertx-pg-client/src/main/java/io/vertx/pgclient/PgConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static Future<PgConnection> connect(Vertx vertx, String connectionUri) {
108108
* @param from byte stream data will be fetched from
109109
* @return result set with single field {@code rowsWritten}
110110
*/
111-
Query<RowSet<Row>> copyFrom(String sql, Future<Buffer> from);
111+
Query<RowSet<Row>> copyFrom(String sql, Buffer from);
112112

113113
/**
114114
* Exports data from a database with decoding.

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public PgConnection noticeHandler(Handler<PgNotice> handler) {
117117
}
118118

119119
@Override
120-
public Query<RowSet<Row>> copyFrom(String sql, Future<Buffer> from) {
120+
public Query<RowSet<Row>> copyFrom(String sql, Buffer from) {
121121
return null;
122122
}
123123

vertx-pg-client/src/test/java/io/vertx/pgclient/PgConnectionCopyTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import org.junit.Test;
66

77
public class PgConnectionCopyTest extends PgConnectionTestBase {
8+
public PgConnectionCopyTest() {
9+
connector = (handler) -> PgConnection.connect(vertx, options).onComplete(ar -> {
10+
handler.handle(ar.map(p -> p));
11+
});
12+
}
13+
814
@Test
915
public void testCopyToRows(TestContext ctx) {
1016
Async async = ctx.async();

0 commit comments

Comments
 (0)