use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class CustomizingStatementHandler method invoke.
@Override
public Object invoke(Object target, Object[] args, HandleSupplier hs) throws Exception {
final Handle h = hs.getHandle();
final String locatedSql = locateSql(h);
final StatementType stmt = createStatement(h, locatedSql);
final SqlObjectStatementConfiguration cfg = stmt.getConfig(SqlObjectStatementConfiguration.class);
cfg.setArgs(args);
configureReturner(stmt, cfg);
applyCustomizers(stmt, args);
return cfg.getReturner().get();
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class SqlBatchHandler method invoke.
@Override
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);
ResultIterator<Object> result;
if (batchArgs.hasNext()) {
result = new ResultIterator<Object>() {
ResultIterator<?> batchResult;
boolean closed = false;
{
// Ensure our batchResult is prepared, so we can get its context
hasNext();
}
@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()) {
return false;
}
// execute a single chunk and buffer
PreparedBatch batch = handle.prepareBatch(sql);
for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
applyCustomizers(batch, batchArgs.next());
batch.add();
}
batchResult = executeBatch(handle, batch);
// recurse to ensure we actually got elements
return hasNext();
}
@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();
}
};
} else {
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.Handle in project jdbi by jdbi.
the class TestDocumentation method testMappingExampleChainedIterator2.
@Test
public void testMappingExampleChainedIterator2() throws Exception {
try (Handle h = dbRule.openHandle()) {
h.execute("insert into something (id, name) values (1, 'Brian')");
h.execute("insert into something (id, name) values (2, 'Keith')");
Iterator<String> rs = h.createQuery("select name from something order by id").mapTo(String.class).iterator();
assertThat(rs.next()).isEqualTo("Brian");
assertThat(rs.next()).isEqualTo("Keith");
assertThat(rs.hasNext()).isFalse();
}
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestDocumentation method testFiveMinuteFluentApi.
@Test
public void testFiveMinuteFluentApi() throws Exception {
try (Handle h = dbRule.openHandle()) {
h.execute("insert into something (id, name) values (?, ?)", 1, "Brian");
String name = h.createQuery("select name from something where id = :id").bind("id", 1).mapTo(String.class).findOnly();
assertThat(name).isEqualTo("Brian");
}
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestDocumentation method testAttachToObject.
@Test
public void testAttachToObject() throws Exception {
try (Handle h = dbRule.openHandle()) {
MyDAO dao = h.attach(MyDAO.class);
dao.insert(1, "test");
}
}
Aggregations