package examples;
import io.vertx.core.Vertx;
import io.vertx.docgen.Source;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLConnection;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.mysqlclient.MySQLSetOption;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.SqlClient;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.SqlResult;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.data.Numeric;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@Source
public class MySQLClientExamples {
public void gettingStarted() {
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost("the-host")
.setDatabase("the-db")
.setUser("user")
.setPassword("secret");
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
MySQLPool client = MySQLPool.pool(connectOptions, poolOptions);
client.query("SELECT * FROM users WHERE id='julien'", ar -> {
if (ar.succeeded()) {
RowSet result = ar.result();
System.out.println("Got " + result.size() + " rows ");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
client.close();
});
}
public void configureFromDataObject(Vertx vertx) {
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost("the-host")
.setDatabase("the-db")
.setUser("user")
.setPassword("secret");
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
MySQLPool pool = MySQLPool.pool(vertx, connectOptions, poolOptions);
pool.getConnection(ar -> {
});
}
public void configureConnectionAttributes() {
MySQLConnectOptions connectOptions = new MySQLConnectOptions();
connectOptions.addProperty("_java_version", "1.8.0_212");
Map<String, String> attributes = new HashMap<>();
attributes.put("_client_name", "myapp");
attributes.put("_client_version", "1.0.0");
connectOptions.setProperties(attributes);
}
public void configureFromUri(Vertx vertx) {
String connectionUri = "mysql://dbuser:secretpassword@database.server.com:3211/mydb";
MySQLPool pool = MySQLPool.pool(connectionUri);
MySQLConnection.connect(vertx, connectionUri, res -> {
});
}
public void connecting01() {
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost("the-host")
.setDatabase("the-db")
.setUser("user")
.setPassword("secret");
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
MySQLPool client = MySQLPool.pool(connectOptions, poolOptions);
}
public void connecting02(Vertx vertx) {
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost("the-host")
.setDatabase("the-db")
.setUser("user")
.setPassword("secret");
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);
}
public void connecting03(Pool pool) {
pool.close();
}
public void connecting04(Vertx vertx) {
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost("the-host")
.setDatabase("the-db")
.setUser("user")
.setPassword("secret");
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);
client.getConnection(ar1 -> {
if (ar1.succeeded()) {
System.out.println("Connected");
SqlConnection conn = ar1.result();
conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
if (ar2.succeeded()) {
conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
conn.close();
});
} else {
conn.close();
}
});
} else {
System.out.println("Could not connect: " + ar1.cause().getMessage());
}
});
}
public void booleanExample01(SqlClient client) {
client.query("SELECT graduated FROM students WHERE id = 0", ar -> {
if (ar.succeeded()) {
RowSet rowSet = ar.result();
for (Row row : rowSet) {
int pos = row.getColumnIndex("graduated");
Byte value = row.get(Byte.class, pos);
Boolean graduated = row.getBoolean("graduated");
}
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void booleanExample02(SqlClient client) {
client.preparedQuery("UPDATE students SET graduated = ? WHERE id = 0", Tuple.of(true), ar -> {
if (ar.succeeded()) {
System.out.println("Updated with the boolean value");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void numericExample(Row row) {
Numeric numeric = row.get(Numeric.class, 0);
if (numeric.isNaN()) {
} else {
BigDecimal value = numeric.bigDecimalValue();
}
}
public void collector01Example(SqlClient client) {
Collector<Row, ?, Map<Long, String>> collector = Collectors.toMap(
row -> row.getLong("id"),
row -> row.getString("last_name"));
client.query("SELECT * FROM users",
collector,
ar -> {
if (ar.succeeded()) {
SqlResult<Map<Long, String>> result = ar.result();
Map<Long, String> map = result.value();
System.out.println("Got " + map);
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void collector02Example(SqlClient client) {
Collector<Row, ?, String> collector = Collectors.mapping(
row -> row.getString("last_name"),
Collectors.joining(",", "(", ")")
);
client.query("SELECT * FROM users",
collector,
ar -> {
if (ar.succeeded()) {
SqlResult<String> result = ar.result();
String list = result.value();
System.out.println("Got " + list);
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void pingExample(MySQLConnection connection) {
connection.ping(ar -> {
System.out.println("The server has responded to the PING");
});
}
public void resetConnectionExample(MySQLConnection connection) {
connection.resetConnection(ar -> {
if (ar.succeeded()) {
System.out.println("Connection has been reset now");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void changeUserExample(MySQLConnection connection) {
MySQLConnectOptions authenticationOptions = new MySQLConnectOptions()
.setUser("newuser")
.setPassword("newpassword")
.setDatabase("newdatabase");
connection.changeUser(authenticationOptions, ar -> {
if (ar.succeeded()) {
System.out.println("User of current connection has been changed.");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void initDbExample(MySQLConnection connection) {
connection.specifySchema("newschema", ar -> {
if (ar.succeeded()) {
System.out.println("Default schema changed to newschema");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void statisticsExample(MySQLConnection connection) {
connection.getInternalStatistics(ar -> {
if (ar.succeeded()) {
System.out.println("Statistics: " + ar.result());
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void debugExample(MySQLConnection connection) {
connection.debug(ar -> {
if (ar.succeeded()) {
System.out.println("Debug info dumped to server's STDOUT");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
public void setOptionExample01(MySQLConnection connection) {
connection.setOption(MySQLSetOption.MYSQL_OPTION_MULTI_STATEMENTS_OFF, ar -> {
if (ar.succeeded()) {
System.out.println("CLIENT_MULTI_STATEMENTS is off now");
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
}