Skip to content
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
143 changes: 77 additions & 66 deletions vertx-db2-client/src/main/java/examples/DB2ClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void gettingStarted() {
// A simple query
client
.query("SELECT * FROM users WHERE id='julien'")
.execute(ar -> {
.execute()
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> result = ar.result();
System.out.println("Got " + result.size() + " rows ");
Expand Down Expand Up @@ -86,9 +87,10 @@ public void configureFromDataObject(Vertx vertx) {
// Create the pool from the data object
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);

pool.getConnection(ar -> {
// Handling your connection
});
pool.getConnection()
.onComplete(ar -> {
// Handling your connection
});
}

public void configureFromUri(Vertx vertx) {
Expand All @@ -100,9 +102,10 @@ public void configureFromUri(Vertx vertx) {
DB2Pool pool = DB2Pool.pool(connectionUri);

// Create the connection from the connection URI
DB2Connection.connect(vertx, connectionUri, res -> {
// Handling your connection
});
DB2Connection.connect(vertx, connectionUri)
.onComplete(res -> {
// Handling your connection
});
}

public void connecting01() {
Expand Down Expand Up @@ -259,44 +262,45 @@ public void connectSsl(Vertx vertx) {
.setPath("/path/to/keystore.p12")
.setPassword("keystoreSecret"));

DB2Connection.connect(vertx, options, res -> {
if (res.succeeded()) {
// Connected with SSL
} else {
System.out.println("Could not connect " + res.cause());
}
});
DB2Connection.connect(vertx, options)
.onComplete(res -> {
if (res.succeeded()) {
// Connected with SSL
} else {
System.out.println("Could not connect " + res.cause());
}
});
}

public void generatedKeys(SqlClient client) {
client
.preparedQuery("SELECT color_id FROM FINAL TABLE ( INSERT INTO color (color_name) VALUES (?), (?), (?) )")
.execute(Tuple.of("white", "red", "blue"), ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows.");
for (Row row : rows) {
System.out.println("generated key: " + row.getInteger("color_id"));
.execute(Tuple.of("white", "red", "blue"))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows.");
for (Row row : rows) {
System.out.println("generated key: " + row.getInteger("color_id"));
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
});
}

public void typeMapping01(Pool pool) {
pool
.query("SELECT an_int_column FROM exampleTable")
.execute(ar -> {
RowSet<Row> rowSet = ar.result();
Row row = rowSet.iterator().next();
.execute().onSuccess(rowSet -> {
Row row = rowSet.iterator().next();

// Stored as INTEGER column type and represented as java.lang.Integer
Object value = row.getValue(0);
// Stored as INTEGER column type and represented as java.lang.Integer
Object value = row.getValue(0);

// Convert to java.lang.Long
Long longValue = row.getLong(0);
});
// Convert to java.lang.Long
Long longValue = row.getLong(0);
});
}

public void collector01Example(SqlClient client) {
Expand All @@ -309,17 +313,18 @@ public void collector01Example(SqlClient client) {
// Run the query with the collector
client.query("SELECT * FROM users")
.collecting(collector)
.execute(ar -> {
if (ar.succeeded()) {
SqlResult<Map<Long, String>> result = ar.result();
.execute()
.onComplete(ar -> {
if (ar.succeeded()) {
SqlResult<Map<Long, String>> result = ar.result();

// Get the map created by the collector
Map<Long, String> map = result.value();
System.out.println("Got " + map);
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
// Get the map created by the collector
Map<Long, String> map = result.value();
System.out.println("Got " + map);
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}

public void collector02Example(SqlClient client) {
Expand All @@ -331,7 +336,11 @@ public void collector02Example(SqlClient client) {
);

// Run the query with the collector
client.query("SELECT * FROM users").collecting(collector).execute(ar -> {
client
.query("SELECT * FROM users")
.collecting(collector)
.execute()
.onComplete(ar -> {
if (ar.succeeded()) {
SqlResult<String> result = ar.result();

Expand All @@ -353,36 +362,38 @@ enum Days {
* Using an enum as a string value in the Row and Tuple methods.
*/
public void enumStringValues(SqlClient client) {
client.preparedQuery("SELECT day_name FROM FINAL TABLE ( INSERT INTO days (day_name) VALUES (?), (?), (?) )")
.execute(Tuple.of(Days.FRIDAY, Days.SATURDAY, Days.SUNDAY), ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows");
for (Row row : rows) {
System.out.println("Day: " + row.get(Days.class, "day_name"));
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
client.preparedQuery("SELECT day_name FROM FINAL TABLE ( INSERT INTO days (day_name) VALUES (?), (?), (?) )")
.execute(Tuple.of(Days.FRIDAY, Days.SATURDAY, Days.SUNDAY))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows");
for (Row row : rows) {
System.out.println("Day: " + row.get(Days.class, "day_name"));
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}

/**
* Using an enum as an int value in the Row and Tuple methods.
* The row.get() method returns the corresponding enum's name() value at the ordinal position of the integer value retrieved.
*/
public void enumIntValues(SqlClient client) {
client.preparedQuery("SELECT day_num FROM FINAL TABLE ( INSERT INTO days (day_num) VALUES (?), (?), (?) )")
.execute(Tuple.of(Days.FRIDAY.ordinal(), Days.SATURDAY.ordinal(), Days.SUNDAY.ordinal()), ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows");
for (Row row : rows) {
System.out.println("Day: " + row.get(Days.class, "day_num"));
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
client.preparedQuery("SELECT day_num FROM FINAL TABLE ( INSERT INTO days (day_num) VALUES (?), (?), (?) )")
.execute(Tuple.of(Days.FRIDAY.ordinal(), Days.SATURDAY.ordinal(), Days.SUNDAY.ordinal()))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Inserted " + rows.rowCount() + " new rows");
for (Row row : rows) {
System.out.println("Day: " + row.get(Days.class, "day_num"));
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}

Expand Down
48 changes: 34 additions & 14 deletions vertx-db2-client/src/main/java/examples/SqlClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class SqlClientExamples {
public void queries01(SqlClient client) {
client
.query("SELECT * FROM users WHERE id='andy'")
.execute(ar -> {
.execute()
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> result = ar.result();
System.out.println("Got " + result.size() + " rows ");
Expand All @@ -59,7 +60,8 @@ public void queries01(SqlClient client) {
public void queries02(SqlClient client) {
client
.preparedQuery("SELECT * FROM users WHERE id=?")
.execute(Tuple.of("andy"), ar -> {
.execute(Tuple.of("andy"))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Got " + rows.size() + " rows ");
Expand All @@ -72,7 +74,8 @@ public void queries02(SqlClient client) {
public void queries03(SqlClient client) {
client
.preparedQuery("SELECT first_name, last_name FROM users")
.execute(ar -> {
.execute()
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
for (Row row : rows) {
Expand All @@ -87,7 +90,8 @@ public void queries03(SqlClient client) {
public void queries04(SqlClient client) {
client
.preparedQuery("INSERT INTO users (first_name, last_name) VALUES (?, ?)")
.execute(Tuple.of("Andy", "Guibert"), ar -> {
.execute(Tuple.of("Andy", "Guibert"))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println(rows.rowCount());
Expand Down Expand Up @@ -126,7 +130,8 @@ public void queries08(SqlClient client) {
// Execute the prepared batch
client
.preparedQuery("INSERT INTO USERS (id, name) VALUES (?, ?)")
.executeBatch(batch, res -> {
.executeBatch(batch)
.onComplete(res -> {
if (res.succeeded()) {

// Process rows
Expand All @@ -143,7 +148,8 @@ public void queries09(SqlClient client, SqlConnectOptions connectOptions) {
connectOptions.setCachePreparedStatements(true);
client
.preparedQuery("SELECT * FROM users WHERE id = ?")
.execute(Tuple.of("julien"), ar -> {
.execute(Tuple.of("julien"))
.onComplete(ar -> {
if (ar.succeeded()) {
RowSet<Row> rows = ar.result();
System.out.println("Got " + rows.size() + " rows ");
Expand All @@ -155,11 +161,13 @@ public void queries09(SqlClient client, SqlConnectOptions connectOptions) {

public void queries10(SqlConnection sqlConnection) {
sqlConnection
.prepare("SELECT * FROM users WHERE id = ?", ar -> {
.prepare("SELECT * FROM users WHERE id = ?")
.onComplete(ar -> {
if (ar.succeeded()) {
PreparedStatement preparedStatement = ar.result();
preparedStatement.query()
.execute(Tuple.of("julien"), ar2 -> {
.execute(Tuple.of("julien"))
.onComplete(ar2 -> {
if (ar2.succeeded()) {
RowSet<Row> rows = ar2.result();
System.out.println("Got " + rows.size() + " rows ");
Expand Down Expand Up @@ -272,20 +280,26 @@ public void transaction03(Pool pool) {
}

public void usingCursors01(SqlConnection connection) {
connection.prepare("SELECT * FROM users WHERE first_name LIKE ?", ar0 -> {
connection
.prepare("SELECT * FROM users WHERE first_name LIKE ?")
.onComplete(ar0 -> {
if (ar0.succeeded()) {
PreparedStatement pq = ar0.result();

// Cursors require to run within a transaction
connection.begin(ar1 -> {
connection
.begin()
.onComplete(ar1 -> {
if (ar1.succeeded()) {
Transaction tx = ar1.result();

// Create a cursor
Cursor cursor = pq.cursor(Tuple.of("julien"));

// Read 50 rows
cursor.read(50, ar2 -> {
cursor
.read(50)
.onComplete(ar2 -> {
if (ar2.succeeded()) {
RowSet<Row> rows = ar2.result();

Expand All @@ -305,7 +319,9 @@ public void usingCursors01(SqlConnection connection) {
}

public void usingCursors02(Cursor cursor) {
cursor.read(50, ar2 -> {
cursor
.read(50)
.onComplete(ar2 -> {
if (ar2.succeeded()) {
// Close the cursor
cursor.close();
Expand All @@ -314,12 +330,16 @@ public void usingCursors02(Cursor cursor) {
}

public void usingCursors03(SqlConnection connection) {
connection.prepare("SELECT * FROM users WHERE first_name LIKE ?", ar0 -> {
connection
.prepare("SELECT * FROM users WHERE first_name LIKE ?")
.onComplete(ar0 -> {
if (ar0.succeeded()) {
PreparedStatement pq = ar0.result();

// Streams require to run within a transaction
connection.begin(ar1 -> {
connection
.begin()
.onComplete(ar1 -> {
if (ar1.succeeded()) {
Transaction tx = ar1.result();

Expand Down
Loading