Search in sources :

Example 6 with Row

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]));
}
Also used : RowImpl(io.vertx.pgclient.impl.RowImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) Row(io.vertx.sqlclient.Row) RowDesc(io.vertx.sqlclient.impl.RowDesc) AESTest(org.folio.rest.security.AESTest) Test(org.junit.Test)

Example 7 with Row

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);
}
Also used : RowImpl(io.vertx.pgclient.impl.RowImpl) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Row(io.vertx.sqlclient.Row) RowDesc(io.vertx.sqlclient.impl.RowDesc) LinkedList(java.util.LinkedList)

Example 8 with Row

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();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 9 with Row

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);
                        }
                    });
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Example 10 with Row

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();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Aggregations

Row (io.vertx.sqlclient.Row)22 PgPool (io.vertx.pgclient.PgPool)12 ArrayList (java.util.ArrayList)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)10 IOException (java.io.IOException)10 PgConnection (io.vertx.pgclient.PgConnection)9 RowSet (io.vertx.sqlclient.RowSet)9 SqlConnection (io.vertx.sqlclient.SqlConnection)9 Transaction (io.vertx.sqlclient.Transaction)9 LinkedList (java.util.LinkedList)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Handler (io.vertx.core.Handler)8 RowImpl (io.vertx.pgclient.impl.RowImpl)8 PreparedStatement (io.vertx.sqlclient.PreparedStatement)8 Tuple (io.vertx.sqlclient.Tuple)8 RowDesc (io.vertx.sqlclient.impl.RowDesc)8 LocalRowSet (org.folio.rest.persist.helpers.LocalRowSet)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 AsyncResult (io.vertx.core.AsyncResult)7 Future (io.vertx.core.Future)7