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();
}
}
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();
}
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());
}
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();
}
}
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();
}
}
Aggregations