Search in sources :

Example 1 with ResultIterator

use of org.jdbi.v3.core.result.ResultIterator in project jdbi by jdbi.

the class TestQueries method testIteratedResult.

@Test
public void testIteratedResult() {
    Handle h = h2Extension.openHandle();
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    try (ResultIterator<Something> i = h.createQuery("select * from something order by id").mapToBean(Something.class).iterator()) {
        assertThat(i.hasNext()).isTrue();
        Something first = i.next();
        assertThat(first.getName()).isEqualTo("eric");
        assertThat(i.hasNext()).isTrue();
        Something second = i.next();
        assertThat(second.getId()).isEqualTo(2);
        assertThat(i.hasNext()).isFalse();
    }
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 2 with ResultIterator

use of org.jdbi.v3.core.result.ResultIterator in project jdbi by jdbi.

the class TestQueries method testFetchSize.

@Test
public void testFetchSize() {
    Handle h = h2Extension.openHandle();
    h.createScript(findSqlOnClasspath("default-data")).execute();
    ResultIterable<Something> ri = h.createQuery("select id, name from something order by id").setFetchSize(1).mapToBean(Something.class);
    ResultIterator<Something> r = ri.iterator();
    assertThat(r.hasNext()).isTrue();
    r.next();
    assertThat(r.hasNext()).isTrue();
    r.next();
    assertThat(r.hasNext()).isFalse();
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 3 with ResultIterator

use of org.jdbi.v3.core.result.ResultIterator in project jdbi by jdbi.

the class SqlBatchHandler method invoke.

@Override
@SuppressWarnings("PMD.ExcessiveMethodLength")
public Object invoke(Object target, Object[] args, HandleSupplier h) {
    final Handle handle = h.getHandle();
    final String sql = locateSql(handle);
    final int chunkSize = batchChunkSize.call(args);
    final Iterator<Object[]> batchArgs = zipArgs(getMethod(), args);
    final class BatchChunkIterator implements ResultIterator<Object> {

        private ResultIterator<?> batchResult = null;

        private boolean closed = false;

        BatchChunkIterator() {
            if (batchArgs.hasNext()) {
                // if arguments are present, preload the next result chunk
                batchResult = loadChunk();
            }
        }

        private ResultIterator<?> loadChunk() {
            // execute a single chunk and buffer
            List<Object[]> currArgs = new ArrayList<>();
            for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
                currArgs.add(batchArgs.next());
            }
            Supplier<PreparedBatch> preparedBatchSupplier = () -> createPreparedBatch(handle, sql, currArgs);
            return executeBatch(handle, preparedBatchSupplier);
        }

        private PreparedBatch createPreparedBatch(Handle handle, String sql, List<Object[]> currArgs) {
            PreparedBatch batch = handle.prepareBatch(sql);
            for (Object[] currArg : currArgs) {
                applyCustomizers(batch, currArg);
                batch.add();
            }
            return batch;
        }

        @Override
        public boolean hasNext() {
            if (closed) {
                throw new IllegalStateException("closed");
            }
            // first, any elements already buffered?
            if (batchResult != null) {
                if (batchResult.hasNext()) {
                    return true;
                }
                // no more in this chunk, release resources
                batchResult.close();
            }
            // more chunks?
            if (batchArgs.hasNext()) {
                // preload the next result chunk
                batchResult = loadChunk();
                // recurse to ensure we actually got elements
                return hasNext();
            }
            return false;
        }

        @Override
        public Object next() {
            if (closed) {
                throw new IllegalStateException("closed");
            }
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return batchResult.next();
        }

        @Override
        public StatementContext getContext() {
            return batchResult.getContext();
        }

        @Override
        public void close() {
            closed = true;
            batchResult.close();
        }
    }
    ResultIterator<Object> result;
    if (batchArgs.hasNext()) {
        result = new BatchChunkIterator();
    } else {
        // only created to get access to the context.
        PreparedBatch dummy = handle.prepareBatch(sql);
        result = new ResultIterator<Object>() {

            @Override
            public void close() {
            // no op
            }

            @Override
            public StatementContext getContext() {
                return dummy.getContext();
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Object next() {
                throw new NoSuchElementException();
            }
        };
    }
    ResultIterable<Object> iterable = ResultIterable.of(result);
    return magic.mappedResult(iterable, result.getContext());
}
Also used : ResultIterator(org.jdbi.v3.core.result.ResultIterator) ArrayList(java.util.ArrayList) Handle(org.jdbi.v3.core.Handle) StatementContext(org.jdbi.v3.core.statement.StatementContext) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with ResultIterator

use of org.jdbi.v3.core.result.ResultIterator in project jdbi by jdbi.

the class TestQueries method testIteratorBehavior2.

@Test
public void testIteratorBehavior2() {
    Handle h = h2Extension.openHandle();
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    try (ResultIterator<Something> i = h.createQuery("select * from something order by id").mapToBean(Something.class).iterator()) {
        Something first = i.next();
        assertThat(first.getName()).isEqualTo("eric");
        Something second = i.next();
        assertThat(second.getId()).isEqualTo(2);
        assertThat(i.hasNext()).isFalse();
    }
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 5 with ResultIterator

use of org.jdbi.v3.core.result.ResultIterator in project jdbi by jdbi.

the class TestQueries method testIteratorBehavior.

@Test
public void testIteratorBehavior() {
    Handle h = h2Extension.openHandle();
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    try (ResultIterator<Something> i = h.createQuery("select * from something order by id").mapToBean(Something.class).iterator()) {
        assertThat(i.hasNext()).isTrue();
        Something first = i.next();
        assertThat(first.getName()).isEqualTo("eric");
        assertThat(i.hasNext()).isTrue();
        Something second = i.next();
        assertThat(second.getId()).isEqualTo(2);
        assertThat(i.hasNext()).isFalse();
    }
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Aggregations

Handle (org.jdbi.v3.core.Handle)5 Something (org.jdbi.v3.core.Something)4 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 ResultIterator (org.jdbi.v3.core.result.ResultIterator)1 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)1 StatementContext (org.jdbi.v3.core.statement.StatementContext)1