use of io.vertx.sqlclient.Row in project raml-module-builder by folio-org.
the class PostgresClientTest method testPopulateExternalColumns.
@Test
public void testPopulateExternalColumns() throws Exception {
PostgresClient testClient = PostgresClient.testClient();
List<String> columnNames = new ArrayList<String>(Arrays.asList(new String[] { "id", "foo", "bar", "biz", "baz" }));
Map<String, Method> externalColumnSetters = new HashMap<>();
testClient.collectExternalColumnSetters(columnNames, TestPojo.class, false, externalColumnSetters);
externalColumnSetters.put("nonExistingColumn", TestPojo.class.getMethod("setBar", String.class));
TestPojo o = new TestPojo();
String foo = "Hello";
String bar = "World";
Double biz = 1.0;
String[] baz = new String[] { "This", "is", "a", "test" };
List<String> rowColumns = new LinkedList<>();
rowColumns.add("foo");
rowColumns.add("bar");
rowColumns.add("biz");
rowColumns.add("baz");
RowDesc desc = new RowDesc(rowColumns);
Row row = new RowImpl(desc);
row.addString(foo);
row.addString(bar);
row.addDouble(biz);
row.addArrayOfString(baz);
testClient.populateExternalColumns(externalColumnSetters, o, row);
assertThat(o.getFoo(), is(foo));
assertThat(o.getBar(), is(bar));
assertThat(o.getBiz(), is(biz));
assertThat(o.getBaz().size(), is(baz.length));
assertThat(o.getBaz().get(0), is(baz[0]));
assertThat(o.getBaz().get(1), is(baz[1]));
assertThat(o.getBaz().get(2), is(baz[2]));
assertThat(o.getBaz().get(3), is(baz[3]));
}
use of io.vertx.sqlclient.Row in project raml-module-builder by folio-org.
the class PostgresClientTest method getMockTestPojoResultSet.
private RowSet<Row> getMockTestPojoResultSet(int total) {
List<String> columnNames = new ArrayList<String>(Arrays.asList(new String[] { "id", "foo", "bar", "biz", "baz" }));
RowDesc rowDesc = new RowDesc(columnNames);
List<Row> rows = new LinkedList<>();
for (int i = 0; i < total; i++) {
Row row = new RowImpl(rowDesc);
row.addUUID(UUID.randomUUID());
row.addString("foo " + i);
row.addString("bar " + i);
row.addDouble((double) i);
row.addArrayOfString(new String[] { "This", "is", "a", "test" });
rows.add(row);
}
return new LocalRowSet(total).withColumns(columnNames).withRows(rows);
}
use of io.vertx.sqlclient.Row in project vertx-examples by vert-x3.
the class SqlClientExample method start.
@Override
public void start() throws Exception {
Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
// Uncomment for MySQL
// Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
// .setPort(5432)
// .setHost("the-host")
// .setDatabase("the-db")
// .setUser("user")
// .setPassword("secret"), new PoolOptions().setMaxSize(4));
pool.getConnection(res1 -> {
if (res1.failed()) {
System.err.println(res1.cause().getMessage());
return;
}
SqlConnection connection = res1.result();
// create a test table
connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
if (res2.failed()) {
connection.close();
System.err.println("Cannot create the table");
res2.cause().printStackTrace();
return;
}
// insert some test data
connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
// query some data with arguments
connection.query("select * from test").execute(rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
for (Row line : rs.result()) {
System.out.println("" + line);
}
// and close the connection
connection.close();
});
});
});
});
}
use of io.vertx.sqlclient.Row in project vertx-examples by vert-x3.
the class SqlClientExample method start.
@Override
public void start() {
Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
// Uncomment for MySQL
// Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
// .setPort(5432)
// .setHost("the-host")
// .setDatabase("the-db")
// .setUser("user")
// .setPassword("secret"), new PoolOptions().setMaxSize(4));
pool.begin(res1 -> {
if (res1.failed()) {
System.err.println(res1.cause().getMessage());
return;
}
Transaction tx = res1.result();
// create a test table
tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
if (res2.failed()) {
tx.close();
System.err.println("Cannot create the table");
res2.cause().printStackTrace();
return;
}
// insert some test data
tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
// rollback transaction
tx.rollback(res4 -> {
// query some data with arguments
pool.query("select * from test").execute(rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
for (Row line : rs.result()) {
System.out.println("" + line);
}
});
});
});
});
});
}
use of io.vertx.sqlclient.Row in project vertx-examples by vert-x3.
the class SqlClientExample method start.
@Override
public void start() {
Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
// Uncomment for MySQL
// Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
// .setPort(5432)
// .setHost("the-host")
// .setDatabase("the-db")
// .setUser("user")
// .setPassword("secret"), new PoolOptions().setMaxSize(4));
pool.begin(res1 -> {
if (res1.failed()) {
System.err.println(res1.cause().getMessage());
return;
}
Transaction tx = res1.result();
// create a test table
tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
if (res2.failed()) {
tx.close();
System.err.println("Cannot create the table");
res2.cause().printStackTrace();
return;
}
// insert some test data
tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
// query some data with arguments
tx.query("select * from test").execute(rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
for (Row line : rs.result()) {
System.out.println("" + line);
}
// and close the connection
tx.commit();
});
});
});
});
}
Aggregations