use of org.jdbi.v3.core.result.ResultIterable in project dropwizard by dropwizard.
the class DbMigrateCommandTest method testRun.
@Test
void testRun() throws Exception {
migrateCommand.run(null, new Namespace(Collections.emptyMap()), conf);
try (Handle handle = Jdbi.create(databaseUrl, "sa", "").open()) {
final ResultIterable<Map<String, Object>> rows = handle.select("select * from persons").mapToMap();
assertThat(rows).hasSize(1);
assertThat(rows.first()).isEqualTo(Maps.of("id", 1, "name", "Bill Smith", "email", "bill@smith.me"));
}
}
use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class SqlQueryHandler method configureReturner.
@Override
void configureReturner(Query q, SqlObjectStatementConfiguration cfg) {
UseRowMapper useRowMapper = getMethod().getAnnotation(UseRowMapper.class);
UseRowReducer useRowReducer = getMethod().getAnnotation(UseRowReducer.class);
if (useRowReducer != null && useRowMapper != null) {
throw new IllegalStateException("Cannot declare @UseRowMapper and @UseRowReducer on the same method.");
}
cfg.setReturner(() -> {
StatementContext ctx = q.getContext();
QualifiedType<?> elementType = magic.elementType(ctx.getConfig());
if (useRowReducer != null) {
return magic.reducedResult(q.reduceRows(rowReducerFor(useRowReducer)), ctx);
}
ResultIterable<?> iterable = useRowMapper == null ? q.mapTo(elementType) : q.map(rowMapperFor(useRowMapper));
return magic.mappedResult(iterable, ctx);
});
}
use of org.jdbi.v3.core.result.ResultIterable 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.ResultIterable in project jdbi by jdbi.
the class TestDocumentation method testFoo.
@Test
public void testFoo() {
try (Handle h = h2Extension.openHandle()) {
h.attach(BatchInserter.class).insert(new Something(1, "Brian"), new Something(3, "Patrick"), new Something(2, "Robert"));
QueryReturningResultIterable qrri = h.attach(QueryReturningResultIterable.class);
ResultIterable<String> iterable = qrri.findById(1);
assertThat(iterable.one()).isEqualTo("Brian");
}
}
use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class TestQueries method testMappedQueryObjectWithNulls.
@Test
public void testMappedQueryObjectWithNulls() {
Handle h = h2Extension.openHandle();
h.execute("insert into something (id, name, integerValue) values (1, 'eric', null)");
ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
List<Something> r = query.list();
Something eric = r.get(0);
assertThat(eric).isEqualTo(new Something(1, "eric"));
assertThat(eric.getIntegerValue()).isNull();
}
Aggregations