Search in sources :

Example 11 with ResultSet

use of org.adbcj.ResultSet in project adbcj by mheath.

the class SelectTest method testBrokenSelect.

public void testBrokenSelect() throws Exception {
    Connection connection = connectionManager.connect().get();
    DbSessionFuture<ResultSet> future = connection.executeQuery("SELECT broken_query");
    try {
        future.get(5, TimeUnit.SECONDS);
        throw new AssertionError("Issues a bad query, future should have failed");
    } catch (DbException e) {
    // Pass
    } finally {
        connection.close(true).get();
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbException(org.adbcj.DbException)

Example 12 with ResultSet

use of org.adbcj.ResultSet in project adbcj by mheath.

the class SelectTest method testSimpleSelect.

public void testSimpleSelect() throws DbException, InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(callbacks.length);
    Connection connection = connectionManager.connect().get();
    try {
        ResultSet resultSet = connection.executeQuery("SELECT int_val, str_val FROM simple_values ORDER BY int_val").addListener(new DbListener<ResultSet>() {

            public void onCompletion(DbFuture<ResultSet> future) throws Exception {
                System.out.println("In callback");
                future.get().size();
                callbacks[0] = true;
                latch.countDown();
                System.out.println("Finished callback");
            }
        }).get();
        Assert.assertEquals(6, resultSet.size());
        Iterator<Row> i = resultSet.iterator();
        Row nullRow = null;
        Row row = i.next();
        if (row.get(0).isNull()) {
            nullRow = row;
            row = i.next();
        }
        Assert.assertEquals(row.get(0).getInt(), 0);
        Assert.assertEquals(row.get(1).getValue(), "Zero");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 1);
        Assert.assertEquals(row.get(1).getValue(), "One");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 2);
        Assert.assertEquals(row.get(1).getValue(), "Two");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 3);
        Assert.assertEquals(row.get(1).getValue(), "Three");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 4);
        Assert.assertEquals(row.get(1).getValue(), "Four");
        if (i.hasNext() && nullRow == null) {
            nullRow = i.next();
        }
        Assert.assertEquals(nullRow.get(0).getValue(), null);
        Assert.assertEquals(nullRow.get(1).getValue(), null);
        Assert.assertTrue(!i.hasNext(), "There were too many rows in result set");
        latch.await();
        Assert.assertTrue(callbacks[0], "Result set callback was not invoked");
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbListener(org.adbcj.DbListener) Row(org.adbcj.Row) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Aggregations

ResultSet (org.adbcj.ResultSet)12 Connection (org.adbcj.Connection)11 TimeoutException (java.util.concurrent.TimeoutException)3 DbFuture (org.adbcj.DbFuture)3 Result (org.adbcj.Result)3 Value (org.adbcj.Value)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ConnectionManager (org.adbcj.ConnectionManager)2 DbException (org.adbcj.DbException)2 DbListener (org.adbcj.DbListener)2 DbSessionFuture (org.adbcj.DbSessionFuture)2 Row (org.adbcj.Row)2 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Parameters (org.testng.annotations.Parameters)1 Test (org.testng.annotations.Test)1